Page 1 of 1

Problems with scaleImage

Posted: 2011-07-20T18:28:47-07:00
by gshumway
I wrote many years ago a script we use to display images pulled from a directory structure. This script also resizes to what we call detail image and thumb nail from the full size image. The imagick module used was 0.9.x.

Trying to rebuild servers with more modern OS versions like EL5 or EL6, I get all kind of issues, so I tried to upgrade imagick to a current version (last try today with version 3.0.1). Due to the API changes I rewrote part of my script. One of the functions we use is scaleImage. And here is where the problem starts. Many of our images are 640x480 or 800x600, but we also have all kind of other sizes. The function gets used to resize to 210x158 or 100x75. And on many of the images all I get is 10 bytes (JPEG header for example) but nothing else. No error anywhere.

How can I try to debug this?

Re: Problems with scaleImage

Posted: 2011-07-20T19:11:33-07:00
by fmw42
Have you reviewed this page: http://us3.php.net/manual/en/function.i ... eimage.php

Perhaps you should post your code or at least that part of the code? Also what version of IM are you using?

Re: Problems with scaleImage

Posted: 2011-07-20T22:39:17-07:00
by gshumway
The code is pretty simple:

Code: Select all

$imghandle = new Imagick();
if (!$imghandle->readImage($pathimage)) {
  error_log("ImageRead: Failed to read image");
  exit;
}
if (!$imghandle->scaleImage($resize_x, $resize_y)) {
  error_log("ImageScale(".$image."): scaling image failed");
  exit;
}
$image_data = $imghandle->getImageBlob();
header("Content-Length: ".strlen(image_data));
print $image_data;
The version of ImageMagick is 6.2.8.0-4.el5_1.1

The output sometimes is 10 bytes only for a JPEG image, but sometimes it works. I have images where resizing to 100x75 works, but not to 210x158, some the 210x158 works but the 100x75 doesn't, some images doesn't work for either sizes, some it works both. Nothing is in the Apache error log.

Re: Problems with scaleImage

Posted: 2011-07-20T22:53:11-07:00
by anthony
That is extremely old. But then you need an old version for IMagick which does not seem to be in active development.

Re: Problems with scaleImage

Posted: 2011-07-20T23:52:51-07:00
by gshumway
The PECL version which was installed is 3.0.1, which is the current version. ImageMagick is the available version for EL5.

The old server was using older version of the PECL module (< 1.0) and the EL4 version of ImageMagick.

Re: Problems with scaleImage

Posted: 2011-07-21T09:15:14-07:00
by fmw42
Some IMagick functions, such as distortImage, require IM 6.3.6 or higher.

I would suggest you try creating the simplest script possible with fixed number arguments (not variables) for scaleImage and test that.

Re: Problems with scaleImage

Posted: 2011-07-21T16:56:53-07:00
by DJ Mike
Post a link to an image that isn't working for you for us to test. I don't think this will help but you don't have a content header:

header('Content-Type: image/jpeg');

Re: Problems with scaleImage

Posted: 2011-07-21T22:21:32-07:00
by gshumway
So I installed another server, this one with EL6, which comes with ImageMagick 6.5.4.7-5.el6. The php version is 5.3.6 (own build) and Apache is 2.2.19 (own build). PECL imagick module is 3.0.1

Same problems: Some images work fine in both resizes, some only of them works, some none work.

Re: Problems with scaleImage

Posted: 2011-07-21T22:25:05-07:00
by gshumway
DJ Mike wrote:Post a link to an image that isn't working for you for us to test. I don't think this will help but you don't have a content header:

header('Content-Type: image/jpeg');
I do, I just didn't post that part of the script. The total script is longer, as it has to calculate where on the disk it is, potential fetch it via http or generate a white image if the file doesn't exist.

Here is one example from our current working server based on EL4, ImageMagick 6.0.7-1-17, Apache 2.0.x, imagick 0.9.11.

http://img.autc.com/display.php?img=169 ... 218993.jpg
http://img.autc.com/display.php?img=169 ... 993_dt.jpg
http://img.autc.com/display.php?img=169 ... 993_th.jpg

The above image works as full size (which just gets passed through) and as _th but not as _dt. The servers I am working on are not exposed to the Internet.

*edit* Ok, I exposed one of the servers to the Internet and put a sample of images up at: http://imgarchive.autc.com/test/

Re: Problems with scaleImage

Posted: 2011-07-26T18:15:43-07:00
by DJ Mike
*edit* Ok, I exposed one of the servers to the Internet and put a sample of images up at: http://imgarchive.autc.com/test/
The images you posted above the edit look OK but from http://imgarchive.autc.com/test/ I got http://imgarchive.autc.com/display.php? ... 275_dt.jpg which shows me an error message:

The image “http://imgarchive.autc.com/display.php? ... 275_dt.jpg” cannot be displayed because it contains errors.

I saw that a lot on I project last week. If you comment out the image header the output will be text and should give you the PHP error message. If not you have to enable error reporting.

Re: Problems with scaleImage

Posted: 2011-07-26T18:53:56-07:00
by DJ Mike
I copied one of your images that didn't work for you and tested using only scaleImage() with the sizes 100x75 and 210x158. Both worked. Looks like the error is before scaleImage().

http://eclecticdjs.com/mike/temp/11/07/ ... 890275.php

Code: Select all

<?php
$file = "10571930_74890275.png";
$image = new Imagick(); 
$image->readimage($file);
$image->scaleImage(210, 158);
header("Content-Type: image/png");
echo $image;
?>

Re: Problems with scaleImage

Posted: 2011-07-26T19:42:47-07:00
by gshumway
DJ Mike wrote:
*edit* Ok, I exposed one of the servers to the Internet and put a sample of images up at: http://imgarchive.autc.com/test/
The images you posted above the edit look OK but from http://imgarchive.autc.com/test/ I got http://imgarchive.autc.com/display.php? ... 275_dt.jpg which shows me an error message:

The image “http://imgarchive.autc.com/display.php? ... 275_dt.jpg” cannot be displayed because it contains errors.

I saw that a lot on I project last week. If you comment out the image header the output will be text and should give you the PHP error message. If not you have to enable error reporting.
It can not be displayed because it is a corrupt image. All the server is sending is 10 byte (the start of a jpeg file). And the code for this is still:

Code: Select all

      $imghandle = new Imagick();
      if (!$imghandle->readImage($pathimage)) {
        error_log("ImageRead: Failed to read image");
        exit;
      }
      if (!$imghandle->scaleImage($resize_x, $resize_y)) {
        error_log("ImageScale(".$image."): scaling image failed");
        exit;
      }
      $image_data = $imghandle->getImageBlob();
      header("Content-Length: ".strlen(image_data));
      print $image_data;

Re: Problems with scaleImage

Posted: 2011-07-26T19:53:24-07:00
by gshumway
DJ Mike wrote:I copied one of your images that didn't work for you and tested using only scaleImage() with the sizes 100x75 and 210x158. Both worked. Looks like the error is before scaleImage().

http://eclecticdjs.com/mike/temp/11/07/ ... 890275.php

Code: Select all

<?php
$file = "10571930_74890275.png";
$image = new Imagick(); 
$image->readimage($file);
$image->scaleImage(210, 158);
header("Content-Type: image/png");
echo $image;
?>
Ok, I grabbed this particular image "10571930_74890275.jpg". Running from a command line the following code:

Code: Select all

<?php
$file = "10571930_74890275.jpg";
$image = new Imagick();
$image->readimage($file);
$image->scaleImage(210, 158);
echo $image;
?>
or

Code: Select all

<?php
$file = "10571930_74890275.jpg";
$image = new Imagick();
$image->readimage($file);
$image->scaleImage(210, 158);
$image_data = $image->getImageBlob();
echo $image_data;
?>
works correctly. The resized image is 23,972 bytes large. When I run it through Apache/mod_php/imagick I only get a 10 byte jpg header returned:

Code: Select all

$ hexdump -C 10571930_74890275_dt_current_code.jpg 
00000000  ff d8 ff e0 00 10 4a 46  49 46                    |ÿØÿà..JFIF|
0000000a
So the problem is somewhere when run under Apache. Hmmm.

Re: Problems with scaleImage

Posted: 2011-07-26T20:02:37-07:00
by gshumway
gshumway wrote: It can not be displayed because it is a corrupt image. All the server is sending is 10 byte (the start of a jpeg file). And the code for this is still:

Code: Select all

      $imghandle = new Imagick();
      if (!$imghandle->readImage($pathimage)) {
        error_log("ImageRead: Failed to read image");
        exit;
      }
      if (!$imghandle->scaleImage($resize_x, $resize_y)) {
        error_log("ImageScale(".$image."): scaling image failed");
        exit;
      }
      $image_data = $imghandle->getImageBlob();
      header("Content-Length: ".strlen(image_data));
      print $image_data;
[/quote]

Ok, found one error, the line "header("Content-Length: ".strlen(image_data));" is missing the $ in front of image_data. Fixing that I do get the correct size returned for image "10571930_74890275.jpg" when requested as _dt (210x175) and it works from command line (Perl-GET 'http://imgarchive.autc.com/display.php?img=10571930_74890275_dt.jpg' > 10571930_74890275_dt_fixed_strlen.jpg), although the size of the file is less (23,950 instead of 23,972). Checking more images now.

Re: Problems with scaleImage

Posted: 2011-07-26T20:06:22-07:00
by gshumway
All images I tested are now working, so I think that the problem was the missing $. Good. Now I can rebuild the second server also with EL6 :D