我一直理解使用gd处理图像需要大量的内存。
然而,在我们的ProductionServers上,我发现使用的内存量有点太高了,所以我深入研究代码,以了解是否至少可以进行一些改进。
在调试时,我没有得到预期的结果,所以我创建了一个小的testscript,这让我很困惑。我希望有人对此有新的见解。
该脚本只是用于测试的普通脚本:
ini_set('memory_limit', '1M');
$src = __DIR__ . '/../images/image-15M.jpg';
$dest = __DIR__ . '/../images/output/resized-image-15M.jpg';
@unlink($dest);
list($srcWidth, $srcHeight, $srcType) = getImageSize($src);
$destImage = imageCreateTrueColor(1000, 1000);
$srcImage = imageCreateFromJpeg($src);
imageCopyResampled($destImage, $srcImage, 0, 0, 0, 0, 1000, 1000, $srcWidth, $srcHeight);
imageJpeg($destImage, $dest, 80);
if (file_exists($dest)) {
die('Image created succesfully in output-directory');
}else{
die('Failed creating image');
}
现在,正如您在这个场景中看到的,我将内存限制设置为1米。脚本仍然可以正常运行。
在生产服务器上,脚本将按预期耗尽内存。
现在我还用500kb的图像测试了这个。它在我的开发环境中运行良好,但是在生产服务器上运行它需要高达16M。
我的第一个结论是生产服务器出了问题。与我相关的主要区别(实际上包括操作系统)是gd的区别:
发展:
'GD Version' => '2.2.5',
'FreeType Support' => true,
'FreeType Linkage' => 'with freetype',
'T1Lib Support' => true,
'GIF Read Support' => true,
'GIF Create Support' => true,
'JPEG Support' => true,
'PNG Support' => true,
'WBMP Support' => true,
'XPM Support' => true,
'XBM Support' => true,
'WebP Support' => true,
'JIS-mapped Japanese Font Support' => false,
生产:
'GD Version' => 'bundled (2.1.0 compatible)',
'FreeType Support' => true,
'FreeType Linkage' => 'with freetype',
'T1Lib Support' => true,
'GIF Read Support' => true,
'GIF Create Support' => true,
'JPEG Support' => true,
'PNG Support' => true,
'WBMP Support' => true,
'XPM Support' => false,
'XBM Support' => true,
'WebP Support' => false,
'JIS-mapped Japanese Font Support' => false,
GD的捆绑版本会有这样的区别吗?
然后我意识到很奇怪,我的开发脚本似乎只能用一百万内存处理一个16M图像。我认为这可能与有效的内存使用,但我不确定这是否是GD的工作方式。
我的测试用例有什么问题吗?
我已经测试了
ini_get('memory_limit')
设置好后,上面写着“1米”,所以看起来是对的。
有什么想法吗?