【问题标题】:How to deal this memory leak when try to resize image in php?尝试在 php 中调整图像大小时如何处理此内存泄漏?
【发布时间】:2015-03-14 18:54:47
【问题描述】:

我尝试调整一个目录中所有图像的大小。在我尝试调整包含 70 张图片的目录大小之前,我的代码一直有效。我已经使用 imagedestroy 和 unset。但我仍然失败,“致命错误:允许的内存大小为 67108864 字节用尽(试图分配 17232 字节)”。(我的 php 内存设置为 64M,无法修改)

$file = scandir ( $dir );
    foreach ( $file as $key => $filename ) {
    $src = $dir . '/' . $filename;
    $dst = $dir . '/' . $filename;
    $width = 660; // max width
    $height = 2000; // max height
    $message = image_resize ( $src, $dst, $width, $height, $crop = 0 );
    if ($message == 1)
        echo "$filename resize success!</br>";
    else
        echo "$filename $message</br>";
}

function image_resize($src, $dst, $width, $height, $crop = 0) {
    if (! list ( $w, $h ) = getimagesize ( $src ))
        return "Unsupported picture type!";

    $type = strtolower ( substr ( strrchr ( $src, "." ), 1 ) );
    if ($type == 'jpeg')
        $type = 'jpg';
    switch ($type) {
        case 'bmp' :
            $img = imagecreatefromwbmp ( $src );
            break;
        case 'gif' :
            $img = imagecreatefromgif ( $src );
            break;
        case 'jpg' :
            $img = imagecreatefromjpeg ( $src );
            break;
        case 'png' :
            $img = imagecreatefrompng ( $src );
            break;
        default :
            return "Unsupported picture type!";
    }

    // resize
    if ($crop) {
        if ($w < $width or $h < $height) {
            imagedestroy ( $img );
            unset ( $img );
            return "Picture is too small!";
        }
        $ratio = max ( $width / $w, $height / $h );
        $h = $height / $ratio;
        $x = ($w - $width / $ratio) / 2;
        $w = $width / $ratio;
    } else {
        if ($w < $width and $h < $height) {
            imagedestroy ( $img );
            unset ( $img );
            return "Picture is too small!";
        }
        $ratio = min ( $width / $w, $height / $h );
        $width = $w * $ratio;
        $height = $h * $ratio;
        $x = 0;
    }

    $new = imagecreatetruecolor ( $width, $height );

    // preserve transparency
    if ($type == "gif" or $type == "png") {
        imagecolortransparent ( $new, imagecolorallocatealpha ( $new, 0, 0, 0, 127 ) );
        imagealphablending ( $new, false );
        imagesavealpha ( $new, true );
    }

    imagecopyresampled ( $new, $img, 0, 0, $x, 0, $width, $height, $w, $h );

    switch ($type) {
        case 'bmp' :
            imagewbmp ( $new, $dst );
            break;
        case 'gif' :
            imagegif ( $new, $dst );
            break;
        case 'jpg' :
            imagejpeg ( $new, $dst );
            break;
        case 'png' :
            imagepng ( $new, $dst );
            break;
    }
    imagedestroy ( $img );
    imagedestroy ( $new );
    unset ( $img );
    unset ( $new );
    return true;
}

【问题讨论】:

  • 你几个小时前问过这个问题,然后删除了这个问题。你怎么又问了?
  • 我看不到其他人关于那个问题的 cmets。我不知道为什么。
  • 您的图片可能对您来说太大了吗?
  • 对于 php 有 200k 大吗?

标签: php image memory-leaks


【解决方案1】:

你有这个问题,因为你必须改变 php.ini max_execution_time 和 memory_limit 您可以在 php.ini 中更改或使用 ini_set 来使用您想要的内存限制

在 php.ini 中

memory_limit = 128mb;

在你的 php 脚本中

ini_set('memory_limit','16M');

在 .htaccess 中

php_value memory_limit 8M;

注意:此方法仅在 PHP 作为 apache 模块执行时才有效。

【讨论】:

  • 确实,但他没有说他是如何尝试改变的,因为我知道有些主机不允许访问 php.ini。为什么我得到 -1 我的帖子有什么问题?如果上面没有提到不能改变内存,那么他唯一能做的就是对前 20 张图片使用 for 循环将数据分成块,然后继续接下来的 20 张,依此类推。
猜你喜欢
  • 1970-01-01
  • 2016-04-12
  • 1970-01-01
  • 1970-01-01
  • 2014-02-15
  • 2018-02-14
  • 1970-01-01
  • 2016-02-25
  • 1970-01-01
相关资源
最近更新 更多