3

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?

LoneWolfPR
  • 3,978
  • 12
  • 48
  • 84
  • 2
    what is $IM and have you done error checking on $_FILES['newsImg']['tmp_name']? – Jose Vega Dec 12 '10 at 20:25
  • Using the PHP imagick extension will make it easier to debug errors (because problems are then reported using the normal PHP error checker) and better performance (because it doesn't have to launch a new process to convert images). It doesn't directly solve your problem, but the debugging will probably help. – El Yobo Dec 12 '10 at 21:48
  • I can't add any extensions to php unfortunately. It's shared hosting, so I don't have access to the php.ini. – LoneWolfPR Dec 13 '10 at 02:58

2 Answers2

2

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.

bart
  • 7,640
  • 3
  • 33
  • 40
1

Start by doing some error checking on the $_FILES['newsImg']['name'] and follow it up by making sure the move_uploaded_file(...) succeeded.

Jose Vega
  • 10,128
  • 7
  • 40
  • 57
  • I've taken a lot of steps. $IM is just the path to imagemagick. I know that is fine otherwise it wouldn't even create the blank file. I'm also sure the move_uploaded_file succeeded. I've checked the folder. I've even broken it into 2 steps. I had this script simply handle the upload. Then, when it was done. I ran another script that handled the conversion just so I knew it was working with a fully uploaded image. I get the same blank image. I'm just wondering if there's some weird setting in PHP that is keeping this from working. – LoneWolfPR Dec 12 '10 at 20:46
  • permissions perhaps? Does the folder that you are moving the file to have write permission to the user running the exec()? – Jose Vega Dec 13 '10 at 02:05
  • Yep. Double checked all permissions. – LoneWolfPR Dec 13 '10 at 02:58