PHP内存不足会改变内存大小无济于事

时间:2014-10-13 12:38:26

标签: php out-of-memory

我尝试使用imagecopyresampled更改调整大小图像。问题我有非常大的图像,它是25 000px x 25 000px。我尝试在localhost上启动,我的电脑安装了8GB内存。 PHP我得到错误:

Fatal error: Out of memory (allocated 1844969472) (tried to allocate 86400 bytes)

我尝试在php.ini文件中增加内存大小:memory_limit=6144M和php文件:ini_set('memory_limit', '-1');但它没有帮助

我的代码:

public function copyResample($pasteStartX, $pasteStartY, $startCopyX, $startCopyY, $dstWidth, $dstHeight, $srcWidth, $srcHeight, $outputPathToFileWOExt) {
    $dstImage = imagecreatetruecolor($dstWidth, $dstHeight);

    switch ($this->getMimeType()) {

        case 'image/jpg':
        case 'image/jpeg':
            $srcImage = imagecreatefromjpeg($this->getPathToFile());
            break;

        case 'image/png':
            $srcImage = imagecreatefrompng($this->getPathToFile());
            break;

        case 'image/gif':
            $srcImage = imagecreatefromgif($this->getPathToFile());
            break;

        default:
            throw new Exception("File is not an image, please use another file type.", 1);
    }

    imagecopyresampled($dstImage, $srcImage, $pasteStartX, $pasteStartY, $startCopyX, $startCopyY, $dstWidth, $dstHeight, $srcWidth, $srcHeight);

    switch ($this->getMimeType()) {

        case 'image/jpg':
        case 'image/jpeg':
            if (imagetypes() && IMG_JPG) {
                imagejpeg($dstImage, $outputPathToFileWOExt . "." . $this->getExt());
            }
            break;

        case 'image/png':
            if (imagetypes() && IMG_PNG) {
                imagepng($dstImage, $outputPathToFileWOExt . "." . $this->getExt());
            }
            break;

        case 'image/gif':
            if (imagetypes() && IMG_GIF) {
                imagegif($dstImage, $outputPathToFileWOExt . "." . $this->getExt());
            }
            break;
    }
}

1 个答案:

答案 0 :(得分:1)

简单的事实是你的内存仍然不足。 25,000像素x 25,000像素的像素图片需要大量的内存 - 如果您正在进行图像重采样,则需要加倍。