Reputation: 882
I am trying to resize an image on the server side. I am getting the image but when I am trying to resize the image; it is throwing some error. Here is what I have done:
imgresize.php: This file creates the resized image.
<?php
function compressImage($ext, $uploadedfile, $actual_image_name, $newwidth)
{
if($ext=="jpg" || $ext=="jpeg" ){
$src = imagecreatefromjpeg($uploadedfile);
list($width,$height)=getimagesize($uploadedfile);
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
// $filename = $newwidth.'_'.$actual_image_name; //PixelSize_TimeStamp.jpg
$filename = $actual_image_name; //PixelSize_TimeStamp.jpg
imagejpeg($tmp,$filename,100);
imagedestroy($tmp);
}
else if($ext=="png"){
$src = imagecreatefrompng($uploadedfile);
list($width,$height)=getimagesize($uploadedfile);
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
// $filename = $newwidth.'_'.$actual_image_name; //PixelSize_TimeStamp.jpg
$filename = $actual_image_name; //PixelSize_TimeStamp.jpg
imagepng($tmp,$filename,100);
imagedestroy($tmp);
}
else{
}
return $filename;
}
?>
Here is my code to get it in my server-side:
include 'imgresize.php';
if($_FILES['img']['tmp_name'] != ''){
$tmp_name_array = $_FILES['img']['tmp_name'];
$uploadedImageName = $_FILES['img']['name'][0];
$imageName = htmlspecialchars($_FILES['img']['tmp_name']);
$imgSize = $_FILES['img']['size'];
//$name= htmlspecialchars($_FILES['img']['name']);
print_r($_FILES['img']);
$tempImgName = $_FILES['img']['tmp_name'];
if(strlen($tempImgName)){
$ext = pathinfo($tempImgName, PATHINFO_EXTENSION);
if(in_array($ext, $valid_formats)){
if($imgSize < 2*1024*1024){
$imageName = compressImage($ext, $tempImgName, $tempImgName, 200);
$imageContents = file_get_contents($imageName);
$encodedImage = base64_encode($imageContents);
array_push($uploadImage, $encodedImage);
// database query
} else{
echo "<script type=\"text/javascript\">".
"window.alert('Image size should be less than 2MB.');".
"window.location.href='edit-profile.php?id=".$id."';".
"</script>";
}
} else{
echo "<script type=\"text/javascript\">".
"window.alert('Invalid image extension found. (Image should be of jpg, jpeg, png extension only) ');".
"window.location.href='edit-profile.php?id=".$id."';".
"</script>";
}
}
It gives me the following error:
[24-Feb-2016 18:47:50 Etc/GMT] PHP Warning: imagecreatefromjpeg(11150825_10153266879053764_3215821412576026934_n.jpg): failed to open stream: No such file or directory in /home/user/public_html/utils/imgresize.php on line 6
[24-Feb-2016 18:47:50 Etc/GMT] PHP Warning: getimagesize(11150825_10153266879053764_3215821412576026934_n.jpg): failed to open stream: No such file or directory in /home/user/public_html/utils/imgresize.php on line 15
[24-Feb-2016 18:47:50 Etc/GMT] PHP Warning: Division by zero in /home/user/public_html/utils/imgresize.php on line 17
[24-Feb-2016 18:47:50 Etc/GMT] PHP Warning: imagecreatetruecolor(): Invalid image dimensions in /home/prernnys/public_html/utils/imgresize.php on line 19
[24-Feb-2016 18:47:50 Etc/GMT] PHP Warning: imagecopyresampled() expects parameter 1 to be resource, boolean given in /home/prernnys/user/utils/imgresize.php on line 20
[24-Feb-2016 18:47:50 Etc/GMT] PHP Warning: imagejpeg() expects parameter 1 to be resource, boolean given in /home/user/public_html/utils/imgresize.php on line 25
[24-Feb-2016 18:47:50 Etc/GMT] PHP Warning: imagedestroy() expects parameter 1 to be resource, boolean given in /home/user/public_html/utils/imgresize.php on line 27
[24-Feb-2016 18:47:50 Etc/GMT] PHP Warning: file_get_contents(11150825_10153266879053764_3215821412576026934_n.jpg): failed to open stream: No such file or directory in /home/user/public_html/update.php on line 916
The image is not getting resized as expected. Please correct me where I am going wrong.
Upvotes: 1
Views: 548
Reputation: 1213
I think the problem is generated because the $tempImgName you're passing inside compressImage function is not a real file in your server folder but just a cache file (11150825_10153266879053764_3215821412576026934_n.jpg).
To fix this error, just use $imageName = compressImage($ext, $tempImgName, $tempImgName, 200);
passing the real path of the image. Probably $uploadedImageName is the correct one to use
Upvotes: 1