0

I have created a simple PHP script to crop an image that has been previously uploaded to the server by the user and save it in another folder as some kind of a thumbnail.

$src_x    = $_POST['left']; // Crop start x
$src_y    = $_POST['top'];  // Crop start y
$dst_w    = $_POST['dim'];  // Thumb width
$dst_h    = $_POST['dim'];  // Thumb height
$src_w    = $_POST['dim'];  // $src_x + $dst_w
$src_h    = $_POST['dim'];  // $src_y + $dst_h
$contact  = $_POST['contact'];
$ratio    = $_POST['ratio'];
$file_tmp = $_POST['file_tmp'];
$file_ext  = strtolower(end(explode('.', $file_tmp)));

$img_info = getimagesize($file_tmp);

if ($file_ext == 'png') {
    $src = imagecreatefrompng($file_tmp);
}
else if ($file_ext == 'jpeg' || $file_ext == 'jpg') {
    $src = imagecreatefromjpeg($file_tmp);
}
else if ($file_ext == 'gif') {
    $src = imagecreatefromgif($file_tmp);
}

$dst = imagecreatetruecolor(154, 154);

imagecopyresampled($dst, $src, 0, 0, $src_x * $ratio, $src_y * $ratio, 154, 154, $src_w * $ratio, $src_h * $ratio);

$img_name = $contact.'.png';
imagepng ($dst, '../images/invitados/'.$img_name);

The script works 100% fine with all jpeg / jpg / gif / png's EXCEPT for those images that have been imported by the user with iPhoto... Does anyone know what is going on??? I am going crazy 'cause I have no idea where the problem might be... The script doesn't even return a black image, so it doesn't even get to create the png...

Please help!

Many thanks

  • 2
    Imported where, using what format? Can you clarify what goes wrong? Is error reporting activated so you'll see when an image is, say, too big? – Pekka Jun 30 '13 at 12:56
  • The user uploads an image to a folder called 'images/tmp' - there is error handling in the script that uploads the pic so that it only accepts images with size up to 5MB. Once the file is uploaded, it is displayed on the screen with a draggable div over it to select the part of the image that will be cropped and saved. When a button is clicked, the coordinates and dimension of this div are passed onto the script I posted above and the thumbnail is created by cropping the image in 'images/tmp'. This works fine with any photo EXCEPT for those where the user has imported the pic with iPhoto... – user2536467 Jun 30 '13 at 14:23
  • ok, but you're not catching errors e.g. when calling `imagecreatefrompng()` (like when they're too big memory-wise). Is error reporting on for those? – Pekka Jun 30 '13 at 14:25
  • no it's not. Should this be an issue? I don't understand what iPhoto does to pics that creates this mess... The script works fine with any other pic!!! – user2536467 Jun 30 '13 at 14:29
  • It could be an issue, yeah. You should check for those errors in any case. What format are the iPhoto pics in and what size are they? – Pekka Jun 30 '13 at 14:32
  • it's weird... I download a jpg from internet and run it through the script. This works fine. I import that pic into iPhoto and run it through the script and it does not work! But the format is still jpg! – user2536467 Jun 30 '13 at 14:35
  • Well, as long as you are unwilling to provide any more info about the images, and start having your script output error messages, I don't think we can help you any further. – Pekka Jun 30 '13 at 14:36
  • 1
    sorry Pekka... The iphoto pic is jpg and 3MB in size – user2536467 Jun 30 '13 at 14:37
  • What are its dimensions? Those are going to count when the image gets processed. My bet is the script is simply running out of memory. – Pekka Jun 30 '13 at 14:40
  • Why would they? It doesn't matter when the same pic is run through the script without having gone through iPhoto before :-( Thanks for the help BTW! – user2536467 Jun 30 '13 at 14:44
  • No problem. Re why: iPhoto might be resizing the image. Either way, activate error reporting to find out for sure. It's probably failing at `$src = imagecreatefromjpeg($file_tmp);` – Pekka Jun 30 '13 at 14:46
  • Just checked... iPhoto is not resizing... How would you do the error reporting? I'm a bit new to this! – user2536467 Jun 30 '13 at 14:48
  • Add `error_reporting(E_ALL);` to the top of the script – Pekka Jun 30 '13 at 14:49
  • And where can I see the output of the error_reporting(E_ALL)??? My script does not output anything as HTML, it only creates a file on the server... – user2536467 Jun 30 '13 at 14:57
  • You should see the output in the browser window, depending on how you run it. – Pekka Jun 30 '13 at 14:58
  • The only thing I see in safari is "Failed to load resource: the server responded with a status of 500 (Internal Server Error)" – user2536467 Jun 30 '13 at 14:59
  • Then you'll need to look into the server's error logs. Those are going to contain the exact error message issued by PHP. – Pekka Jun 30 '13 at 15:01
  • BTW I am on a mac and using MAMP Pro (don't know if it is relevant but just in case...) – user2536467 Jun 30 '13 at 15:02
  • OK found it! PHP Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 14592 bytes) in /Users/David/localhost/bodadavidyleti/confirma/crop.php on line 23 – user2536467 Jun 30 '13 at 15:04
  • Wait! The full thing seems to be the following: [30-Jun-2013 16:59:32 Europe/Madrid] PHP Strict Standards: Only variables should be passed by reference in /Users/David/localhost/bodadavidyleti/confirma/crop.php on line 13 [30-Jun-2013 16:59:32 Europe/Madrid] PHP Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 14592 bytes) in /Users/David/localhost/bodadavidyleti/confirma/crop.php on line 23 – user2536467 Jun 30 '13 at 15:06
  • Line 23 is the $src = imagecreatefromjpeg($file_tmp) which seems to make sense because the file is a jpg... We're almost there Pekka! – user2536467 Jun 30 '13 at 15:08
  • Yup, that's the reason then. Resizing the image takes more memory than your script has at its disposal. Try raising the memory limit, using smaller images, or check out [Efficient JPEG Image Resizing in PHP](http://stackoverflow.com/q/12661) – Pekka Jun 30 '13 at 15:24
  • cool!!! how can I raise the memory limit??? – user2536467 Jun 30 '13 at 15:52
  • See https://drupal.org/node/207036 – Pekka Jun 30 '13 at 16:34
  • awesome pekka... I owe you big time! Thanks so much :-) – user2536467 Jul 01 '13 at 00:22

1 Answers1

1

Check the image format that has been exported from iPhoto.Photos from iPhoto can be exported as JPEG | PNG | TIFF.

Maybe the images that you are trying to crop is in a TIFF format which is not included on condition on your script.

junerey
  • 120
  • 2
  • 7
  • Thanks for the answer! I download a jpg from internet and run it through the script. This works fine. I import that pic into iPhoto and run it through the script and it does not work! The image from iPhoto is in the same format, has the same name and same size as before... :-( – user2536467 Jun 30 '13 at 14:40
  • Oh. So you're trying to crop those images that has been imported to iPhoto. Make sure your script is really finding the image which has been imported to iPhoto. – junerey Jun 30 '13 at 14:59