imagecreatefromjpegAllowed memory size of 13421772

错误

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 3200 bytes) inphp

$image_path = 'E:\www\01.jpg';
echo '1: ' . memory_get_usage() . '<br>';
$img1 = imagecreatefromjpeg($image_path);
echo '2: ' . memory_get_usage() . '<br>';
$img2 = imagecreatetruecolor(800, 23277);
echo '3: ' . memory_get_usage() . '<br>';

输出以下:函数

1: 1117840code

2: 95350704图片

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 3200 bytes) in E:\www\Demo.php on line 88内存


能够看出,imagecreatefromjpeg用了94M内存,下面的imagecreatetruecolor也须要94M内存,二者加上就超过默认的128M了,因此,imagecreatetruecolor函数报错,缘由在哪里,由于这个图片的高度实在过高了,23227个像素get

解决办法

先获取图片的高度,若是高度大于10000,那就就设置php可用内存ini_set('memory_limit', '256M');it

$image_path = 'E:\www\01.jpg';
$image_info = getimagesize($image_path);
if ($image_info[1] > 10000) {
    ini_set('memory_limit', '256M');
}
echo '1: ' . memory_get_usage() . '<br>';
$img1 = imagecreatefromjpeg($image_path);
echo '2: ' . memory_get_usage() . '<br>';
$img2 = imagecreatetruecolor(800, 23277);
echo '3: ' . memory_get_usage() . '<br>';

输出以下:im

1: 1120504error

2: 95353368img

3: 135840936

相关文章
相关标签/搜索