son Posted March 13, 2012 Share Posted March 13, 2012 I am trying to use the function: function resize_image($opts){ $src = isset($opts['source']) ? $opts['source'] : ''; $dest = isset($opts['dest']) ? $opts['dest'] : ''; $w = isset($opts['w']) ? intval($opts['w']) : 0; $h = isset($opts['h']) ? intval($opts['h']) : 0; if ($src == '') { return; } if ($w == 0 && $h == 0) { return; } if ($dest == '') $dest = $src; // resize in place // open the image $ext = strtolower(array_pop(explode('.', $src))); switch ($ext) { case 'jpg': case 'jpeg': $i = imagecreatefromjpeg($src); break; case 'gif': $i = imagecreatefromgif($src); break; case 'png': $i = imagecreatefrompng($src); break; default: return; } $new_w = imagesx($i); $new_h = imagesy($i); if (($w != 0 && $new_w <= $w && $h == 0) || ($w == 0 && $h != 0 && $new_h <= $h) || ($w != 0 && $new_w <= $w && $h != 0 && $new_h <= $h)) { // image is small enough if ($dest != $src) copy($src, $dest); return; } // determine new size if ($w != 0 && $new_w > $w) { $new_h = ($w / $new_w) * $new_h; $new_w = $w; } if ($h != 0 && $new_h > $h) { $new_w = ($h / $new_h) * $new_w; $new_h = $h; } // resize $new = imagecreatetruecolor($new_w, $new_h); if ($ext == 'png' || $ext == 'gif') { $trnprt_indx = imagecolortransparent($i); // If we have a specific transparent color if ($trnprt_indx >= 0) { // Get the original image's transparent color's RGB values $trnprt_color = imagecolorsforindex($i, $trnprt_indx); // Allocate the same color in the new image resource $trnprt_indx = imagecolorallocate($new, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']); // Completely fill the background of the new image with allocated color. imagefill($new, 0, 0, $trnprt_indx); // Set the background color for new image to transparent imagecolortransparent($new, $trnprt_indx); } // Always make a transparent background color for PNGs that don't have one allocated already elseif ($ext == 'png') { // Turn off transparency blending (temporarily) imagealphablending($new, false); // Create a new transparent color for image $color = imagecolorallocatealpha($new, 0, 0, 0, 127); // Completely fill the background of the new image with allocated color. imagefill($new, 0, 0, $color); // Restore transparency blending imagesavealpha($new, true); } } imagecopyresampled($new, $i, 0, 0, 0, 0, $new_w, $new_h, imagesx($i), imagesy($i)); imagedestroy($i); // save the image switch ($ext) { case 'jpg': case 'jpeg': imagejpeg($new, $dest); break; case 'gif': imagegif($new, $dest); break; case 'png': imagepng($new, $dest); break; } imagedestroy($new);} by calling it via: resize_image($absMainPathImgNew, $absMainPathImgNew, 550, 550);resize_image($absMainPathImgNew, $absPreviewPathImgNew, 150, 150); It does move the first image ok, but does not create the smaller file. It also does not resize (in both cases). Where am I going wrong? Son Link to comment Share on other sites More sharing options...
justsomeguy Posted March 13, 2012 Share Posted March 13, 2012 That function uses an array as input. resize_image(array( 'source' => $absMainPathImgNew, 'w' => 550, 'h' => 550)); resize_image(array( 'source' => $absMainPathImgNew, 'dest' => $absPreviewPathImgNew, 'w' => 150, 'h' => 150)); If you leave out dest, like for the first one, it overwrites the image with the resized one. Adding the dest in the second call makes it save the resized image as a new file. You can also leave out either the width or height, if you leave out one of them it will use the one value you gave and calculate the other to be the correct ratio. That function also doesn't resize larger, only smaller. If the image is already small enough it won't resize it to make it larger. It will maintain the correct aspect ratio and make sure both the width and height are smaller or equal to what you tell it. Link to comment Share on other sites More sharing options...
son Posted March 13, 2012 Author Share Posted March 13, 2012 Thanks a lot. Have it working now:-) Son Link to comment Share on other sites More sharing options...
son Posted March 20, 2012 Author Share Posted March 20, 2012 Actually, just an important improvement for this great funtion. Use: $extVar = explode('.',$src);$ext = strtolower(array_pop($extVar)); instead of: $ext = strtolower(array_pop(explode('.', $src))); I got "Only variables should be passed by reference" when I used the function on another page with image upload and the small change solved the issue... Son Link to comment Share on other sites More sharing options...
justsomeguy Posted March 20, 2012 Share Posted March 20, 2012 Right, that's a strict error. Array_pop uses a parameter passed by reference. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.