I'm trying to create thumbnails with PHP on a Windows Apache server using the folowing code:

<?
$img = "test.jpg";
$w=200;

header("Content-type: image/jpeg");
$im = @imagecreatefromjpeg("$img");
list($width, $height, $type, $attr) = getimagesize("$img");
$h = $height * ($w/$width);
$dim = imagecreatetruecolor($w, $h);
imagecopyresized($dim, $im, 0, 0, 0, 0, $w, $h, $width, $height);
imagejpeg($dim, '', 85);
imagedestroy($dim);
imagedestroy($im);
?>

it works perfect on small images (600x450, 41.7KšŸ˜Ž but on large images (2736x3645, 4.11MšŸ˜Ž it fails.

If I comment out the line "header("Content-type: image/jpeg");" I get the error:

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 2736 bytes) in D:\xampp\htdocs\thumb.php on line 6

What is the problem?

    The problem is that you exceeded the amount of memory currently allowed for your script. (The PHP image functions may need significantly more memory allocated for an image that the size of the source image file, as the functions deal with a complete bitmap of the image, not a compressed version such as JPEG compression creates. The only work-around would be to increase the max memory limit.

    ini_set('memory_limit', '100M'); // 100 megs
    // ...or...
    ini_set('memory_limit', -1); // no limit, but don't complain to me if you crash something
    

      It works this way but I already set the memory_limit = 128M in the php.ini shouldn't this work too?

        Write a Reply...