I have a script that is supposed to take an image and convert it to a .jpg. This is the code that makes it happen:
$uploadDir = $_SERVER['DOCUMENT_ROOT'] . "/blogimages/";
$tempFile = ereg_replace("'", "_", basename($_FILES['newsImg']['name']));
$uploadFile = $uploadDir . $tempFile;
move_uploaded_file($_FILES['newsImg']['tmp_name'], $uploadFile);
$newPic = $uploadDir . $blogID . ".jpg";
if(file_exists($newPic)){
unlink($newPic);
}
$convertString = "$IM -strip $uploadFile $newPic";
echo "<!-- $convertString -->";
exec($convertString);
as can be seen I put the final string in an HTML comment so I can see what is being executed. What happens is that the converted image is created, but it is a 0 byte image. So no data is actually written to the file. Just to make sure convert is actually working like it normally should I copy and pasted the convert string from the html comment to a command line and it works just fine. It only seems to be having problems within the PHP exec. Any thoughts on why this might be?
I'm thinking perhaps the upload file handle isn't yet closed when you try to execute the command? That way Imagemagick would see an incomplete file.
Looking at the code I don't see how it could happen, especially since the file is moved around, but it would explain the behavior.
Start by doing some error checking on the $_FILES['newsImg']['name'] and follow it up by making sure the move_uploaded_file(...) succeeded.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With