错误
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 3200 bytes) in
$image_path = 'E:\www\01.jpg';echo '1: ' . memory_get_usage() . '';$img1 = imagecreatefromjpeg($image_path);echo '2: ' . memory_get_usage() . '';$img2 = imagecreatetruecolor(800, 23277);echo '3: ' . memory_get_usage() . '';
输出如下:
1: 1117840
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个像素
解决办法
先获取图片的高度,如果高度大于10000,那就就设置php可用内存ini_set('memory_limit', '256M');
$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() . '';$img1 = imagecreatefromjpeg($image_path);echo '2: ' . memory_get_usage() . '';$img2 = imagecreatetruecolor(800, 23277);echo '3: ' . memory_get_usage() . '';
输出如下:
1: 1120504
2: 95353368
3: 135840936