PHP libgd crash without feedback

孤街醉人 提交于 2019-12-13 01:42:38

问题


I'm working on a very large number of images. I have to create four JPEG versions of each image:

  • original size JPEG
  • 700x430 version
  • 57x57 thumbnail
  • 167xN thumbnail (variable height, maintain aspect ratio)

I used the following two helper functions:

function thumbFixed($srcfile, $dstfile, $dstWidth, $dstHeight) {
    $srcImg = imagecreatefromstring(file_get_contents($srcfile));
    list($srcWidth, $srcHeight) = getimagesize($srcfile);
    $srcAR = $srcWidth / $srcHeight;

    $dstAR = $dstWidth / $dstHeight;

    $dstImg = imagecreatetruecolor($dstWidth, $dstHeight);

    if($srcAR >= $dstAR) {
        $cropHeight = $dstHeight;
        $cropWidth = (int)($dstHeight * $srcAR);
    } else {
        $cropWidth = $dstWidth;
        $cropHeight = (int)($dstWidth / $srcAR);
    }

    $cropImg = imagecreatetruecolor($cropWidth, $cropHeight);
    imagecopyresampled($cropImg, $srcImg, 0, 0, 0, 0, $cropWidth, $cropHeight, $srcWidth, $srcHeight);

    $dstOffX = (int)(($cropWidth - $dstWidth) / 2);
    $dstOffY = (int)(($cropHeight - $dstHeight) / 2);

    imagecopy($dstImg, $cropImg, 0, 0, $dstOffX, $dstOffY, $dstWidth, $dstHeight);
    imagejpeg($dstImg, $dstfile);
}

function thumbGrid($srcfile, $dstfile, $dstWidth) {
    $srcImg = imagecreatefromstring(file_get_contents($srcfile));
    list($srcWidth, $srcHeight) = getimagesize($srcfile);
    $srcAR = $srcWidth / $srcHeight;

    $dstHeight = $dstWidth / $srcAR;

    $dstImg = imagecreatetruecolor($dstWidth, $dstHeight);

    imagecopyresampled($dstImg, $srcImg, 0, 0, 0, 0, $dstWidth, $dstHeight, $srcWidth, $srcHeight);
    imagejpeg($dstImg, $dstfile);
}

Those function are used in this context:

$img = imagecreatefromstring(file_get_contents($local));
imagejpeg($img, 'temp.jpg', 100);
thumbFixed($local, 'gallery.jpg', 700, 430);
thumbFixed($local, 'thumbsmall.jpg', 57, 57);
thumbGrid($local, 'grid.jpg', 167);

Where $local holds the pathname of the image.

The code works okay for most of the pictures, but misbehaves in one particular case, which is a 1792x1198, 356Kb JPEG file. I'm quite worried by the misbehavior, though, because I can't seem to trace it in any way: running gdb on PHP interpreter results in Program exited with code 0377. (which means PHP has exit(-1)ed) and nothing else - no stack trace, no errors, no feedback whatsoever.

I figured out, by inserting various echos before every libgd call, that the script "crashes" PHP when executing $cropImg = imagecreatetruecolor($cropWidth, $cropHeight); in the thumbFixed function, but I can't seem to figure out a way around this since I can't control it from my code, nor backtrace it from a debugger. Has anybody had similar experiences with this and can give me some pointers?

来源:https://stackoverflow.com/questions/13497700/php-libgd-crash-without-feedback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!