if ( $_FILES["ufile"]["type"] [$i] == "image/jpeg" || $_FILES["ufile"]["type"] [$i] == "image/jpg" || $_FILES["ufile"]["type"] [$i] == "image/png" || $_FILES["ufile"]["type"] [$i] == "image/gif" )
{
if ($_FILES["ufile"]["type"] [$i] == "image/png" || $_FILES["ufile"]["type"] [$i] == "image/gif" || $_FILES["ufile"]["type"] [$i] == "image/jpeg" || $_FILES["ufile"]["type"] [$i] == "image/jpg" )
{
$file=$_FILES["ufile"]["tmp_name"];
list($width, $height) = getimagesize($file);
$new_width = 200;
$new_height = 500;
$dst_image = imagecreatetruecolor($new_width,$new_height);
$src_image = imagecreatefromjpeg($_FILES["ufile"]["tmp_name"]);
imagecopyresized ($dst_image, $src_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg(imagecreatefromstring(file_get_contents($dst_image)), "convert.jpg");
$actual_image_name = $name[$i].'.'.'jpg';
move_uploaded_file("convert.jpg", $path.$actual_image_name);
}
}
-
Is the question anywhere hidden in the code? Does i have to search for it? – Rizier123 Jan 06 '15 at 16:12
-
**NEVER** use the `['type']` field for validation. it's trivial to forget. Since you're using getimagesize(), just use its `['mime']` parameter to see what the file really is. – Marc B Jan 06 '15 at 16:12
-
first move uploaded file and then apply dimension changes – Bhavya Shaktawat Jan 06 '15 at 16:15
1 Answers
You are totally mis-using GD. $dst_image
is a GD image handle. It is not a file. So... file_get_contents()
will return boolean FALSE, since you're trying to fetch from something that is not a filename. imagecreatefromstring()
will return boolean false, since a boolean false is not an image string, and imagejpeg()
will return boolean FALSE since you're not passing it a GD Handle.
In short, your code could do with some improvement. You simply assume nothing could ever go wrong, don't ever check for failure, and those failures propagate onwards through the code.
All you really needed was
image_jpeg($dst_image, 'convert.jpg');
And then your move_uploaded_file
will also fail, since "convert.jpg" isn't an uploaded file. It's a file you've created yourself on the server. It may have been based on an uploaded file, but in and of itself is not an uploaded file. Why not just
image_jpeg($dst_image, "$path/$actual_image_name");
-
I thought the commentary in this answer was a bit sharp, so I've made it a bit softer, whilst hopefully conveying the same meaning. The OP will be long gone, but I think there's value in being kind to future readers who may also be beginner devs! – halfer May 14 '16 at 12:39