Reputation: 359
I've been working on writing a form where users provide the path to an image, and then the script resizes the image and then saves the resized image on the server. There's some other stuff going on too, but that's where I'm having my issue. I've read probably 20 different articles on how to do this and looked at other responses on here, but even when I have something that looks identical it doesn't work. Here's my latest attempt:
function upload($rowID, $fName, $lName)
{
/*** get the original file name & path **/
$thumb_data = $_FILES['image_name']['name'];
$imgpath = "../images/Employee_Photos/".$thumb_data;
/*** Get the size of the source image and set ***/
/*** the dimensions for the scaled image ***/
$size = getimagesize($imgpath);
$image_width = $size[0];
$image_height = $size[1];
$aspect_ratio = (float)($size[0] / $size[1]);
$thumb_height = 50;
$thumb_width = $thumb_height * $aspect_ratio;
/*** set the thumb file name & path ***/
$thumb_name = $fName."_".$lName."_thumb.jpg";
$savepath = "../images/Employee_Photos/thumbs".$thumb_name;
/*** get the image source ***/
$src = ImageCreateFromjpeg($imgpath);
/*** create the destination image ***/
$destImage = ImageCreateTrueColor($thumb_width, $thumb_height);
/*** copy and resize the src image to the dest image ***/
ImageCopyResampled($destImage, $src, 0,0,0,0, $thumb_width, $thumb_height, $image_width, $image_height);
$thumbnail = $destImage;
/*** given the image thumb and file location, ***/
/*** save the created image at that location ***/
imageJPEG($thumbnail, $savepath,100);
...
}
Now, everything else in this file works. All of the database operations that are found further down in the script work as expected. All of the paths to the image folder are correct. Does anyone see something that I should be doing differently? The dimensions of the thumb being created are calculated properly, and the only other thing I can think of is something's being set wrong for the destination path--although I don't see anything that pops out at me.
Thanks in advance for the help!
/// Updated with minor code changes to reflect more recent attempts ///
Upvotes: 0
Views: 356
Reputation: 102
Try Imagick php extension. In your code, probably you not understand how to calculate the correct ratio?
My imagick code for thumbs:
$image = 'path/to/image.jpg';
$maxsize=450;
$imagick = new \Imagick($image);
$height = $imagick->getImageHeight();
$width = $imagick->getImageWidth();
if($height > $maxsize || $width > $maxsize){
if($height <= $width){
$imagick->resizeImage($maxsize,0, \Imagick::FILTER_LANCZOS, 1);
}
else{
$imagick->resizeImage(0,$maxsize, \Imagick::FILTER_LANCZOS, 1);
}
}
$imagick->setImageCompression(\Imagick::COMPRESSION_JPEG);
$imagick->setImageCompressionQuality(75);
$imagick->stripImage();
if($imagick->writeimage($savePath.$uniqueName.'-thumb.jpg')){
$imagick->destroy();
return $uniqueName.'-thumb.jpg';
}
Upvotes: 1