简体   繁体   中英

PHP GD imagecreatefromjpeg cant handle big size image?

my project is when im uploading an image automatically my program will create the thumb size.
My program work normally if the size of picture about 1024x768 But when i'm uploading image with size 1576x2379 showing error like this:

Allowed memory size of 8388608 bytes exhausted (tried to allocate 1576 bytes)

I'm using method imagcreatefromjpeg().
How can I create thumb version from big size image using PHP???

thanks

You have to edit your php.ini
Find the line with Memory limit statement and change its default value for something bigger - for example 128M

For me this problem solved with next peace of code:

  1. first of all you need imagemagick on your server;
  2. and install it also as part of php (php5-imagick)

  3. my part of code (for Smart Image Resizer 1.4.1 )

i found line "$src = $creationFunction($docRoot . $image);" and replace with

if ( $width >= 1900 )
{
// Read original image and create Imagick object
$thumb = new Imagick($docRoot . $image);

$newX = 1600;
$newY = 1200;

// Scale the image
$thumb->thumbnailImage($newX,$newY);
#$thumb->cropThumbnailImage(600,600);

// make new file-name
$_ext_pos = strrpos($image,'.');
$_image_name_p1 = substr($image, 0, $_ext_pos);
$_image_name_p2 = substr($image, $_ext_pos);

$thumbnailFilename = $_image_name_p1.'_s600'.$_image_name_p2;

// Write the new image to a file
$thumb->writeImage($docRoot . $thumbnailFilename);
$thumb->destroy();

// Read in the original image
$src    = $creationFunction($docRoot . $thumbnailFilename);

// reset w-h

$size    = GetImageSize($docRoot . $thumbnailFilename);

$width         = $size[0];
$height        = $size[1];

// Setting up the ratios needed for resizing.
// resize the image (based on height or based on width)
$xRatio        = 1;#$maxWidth / $width;
$yRatio        = 1;#$maxHeight / $height;

if ($xRatio * $height < $maxHeight)
{ // Resize the image based on width
    $tnHeight    = ceil($xRatio * $height);
    $tnWidth    = $maxWidth;
}
else // Resize the image based on height
{
    $tnWidth    = ceil($yRatio * $width);
    $tnHeight    = $maxHeight;
}

}
else
{
// Read in the original image
$src    = $creationFunction($docRoot . $image);
}

so I replace the "ImageCreateFromJpeg" for large images with imagick-workflow

good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM