开发者

Resizing image and running out of memory

I'm currently building an import-module for Prestashop. This module is importing about 3000 products and needs to resize every product-image to 5 different thumbnail-formats. The problem is: the script consumes A LOT of memory. I'm talking about 100 MB peak and up to 27 MB during the process. I'm kind of ashamed to say I'm not very familiar with the whole memory-managing stuff, so any help is welcome!

The code I'm using is the following. The resizeImg method should be the most interesting, the other methods are just for illustration on how I'm processing the tasks. Does anyone know why I get a memory peak of over 100MB?

public function main() {
    foreach( $products as $product ) {
        self::copyImg( $imageURL );
    }
}

static private function copyImg( $imageURL ) {
    // Copy image from $imageURL to temp folder

    foreach( $imageTypes as $type ) {
        self::resizeImg( $tempImage, $destination, $type['width'], $type['height']);
    }
}

static private function resizeImg( $sourceFile, $destFile, $destWidth, $destHeight )
{
    list( $sourceWidth, $sourceHeight, $type, $attr ) = getimagesize( $sourceFile );
    if ( is_null( $destWidth ) ) $destWidth = $sourceWidth;
    if ( is_null( $destHeight ) ) $destHeight = $sourceHeight;

    $sourceImage = imagecreatefromjpeg( $sourceFile );
    $widthDiff = $destWidth / $sourceWidth;
    $heightDiff = $destHeight / $sourceHeight;

    if ( $widthDiff > 1 && $heightDiff > 1 ) {

        $nextWidth = $sourceWidth;
        $nextHeight = $sourceHeight;

    } else {

        if ( Configuration::get( 'PS_IMAGE_GENERATION_METHOD' ) == 2 || ( ! Configuration::get( 'PS_IMAGE_GENERATION_METHOD' ) AND $widthDiff > $heightDiff开发者_JS百科 ) ) {

            $nextHeight = $destHeight;
            $nextWidth = round( $sourceWidth * $nextHeight / $sourceHeight );
            $destWidth = ( ! Configuration::get( 'PS_IMAGE_GENERATION_METHOD' ) ? $destWidth : $nextWidth );

        } else {

            $nextWidth = $destWidth;
            $nextHeight = round( $sourceHeight * $destWidth / $sourceWidth );
            $destHeight = ( ! Configuration::get( 'PS_IMAGE_GENERATION_METHOD' ) ? $destHeight : $nextHeight );

        }

    }

    $destImage = imagecreatetruecolor( $destWidth, $destHeight );
    $white = imagecolorallocate( $destImage, 255, 255, 255 );
    imagefilledrectangle( $destImage, 0, 0, $destWidth, $destHeight, $white );
    imagecopyresampled( $destImage, $sourceImage, ( ( $destWidth - $nextWidth ) / 2 ), ( ( $destHeight - $nextHeight ) / 2 ), 0, 0, $nextWidth, $nextHeight, $sourceWidth, $sourceHeight );
    imagecolortransparent( $destImage, $white );
    imagejpeg( $destImage, $destFile, 90 );

    imagedestroy( $sourceImage );
    imagedestroy( $destImage );
}


Just how big are these images? Remember that once they're loaded into GD, they're uncompressed into 3 (or 4) bytes per pixel. 100mb at 24bit is enough for a 6666x5000 or so image. Have you checked that the resizing calculations are working correctly? If they're wrong, you could be trying to create a massively large 'dest' image by mistake.

I don't see where you're writing out the resized image, either. There's lots of resizing/copying, but nowhere do you have imagejpeg() or imagepng() etc... to write out the resized image.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜