简体   繁体   English

调整图像大小并用完 memory

[英]Resizing image and running out of memory

I'm currently building an import-module for Prestashop.我目前正在为 Prestashop 构建一个导入模块。 This module is importing about 3000 products and needs to resize every product-image to 5 different thumbnail-formats.该模块正在导入大约 3000 个产品,需要将每个产品图像的大小调整为 5 种不同的缩略图格式。 The problem is: the script consumes A LOT of memory.问题是:脚本消耗了很多 memory。 I'm talking about 100 MB peak and up to 27 MB during the process.我说的是在此过程中峰值为 100 MB,最高为 27 MB。 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. resizeImg 方法应该是最有趣的,其他方法只是为了说明我如何处理任务。 Does anyone know why I get a memory peak of over 100MB?有谁知道为什么我的 memory 峰值超过 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 ) ) {

            $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.请记住,一旦将它们加载到 GD 中,它们就会被解压缩为每个像素 3(或 4)个字节。 100mb at 24bit is enough for a 6666x5000 or so image.对于 6666x5000 左右的图像,24 位的 100mb 就足够了。 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.如果他们错了,您可能会错误地尝试创建一个巨大的“dest”图像。

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.有很多调整大小/复制,但你没有imagejpeg()imagepng()等...写出调整大小的图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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