在不超出内存限制的情况下调整php中​​的多个图像

tdh*_*h87 4 php image-resizing memory-limit

我目前正在尝试获取一个表单,允许使用PHP在服务器上上传和调整大小的图像.客户端上传的每张图片大小约为2.5mb.

我目前正在使用该move_uploaded_file()功能.

将文件移动到服务器上没有问题.当我尝试裁剪时出现问题.在我的主机上没有ImageMagick我正在使用这个设置(不是所有的代码只是相关的,这是在循环中$width等等改变不同的裁剪尺寸)

$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
imagejpeg($image_p, $output_filename, 80);
Run Code Online (Sandbox Code Playgroud)

目前,这只适用于2张图片.如果提交了3个或更多,我会收到"内存耗尽"错误.我研究了这个,因为我的内存限制是120mb.显然,该imagecreatefromjpeg函数使用了大量内存,特别是如果文件具有较大的分辨率(我的确如此 - 因此我需要裁剪/调整大小).

有谁知道更有效的方法来完成这项任务?我在google上进行了研究,但每个人都使用相同的技术.

Zul*_*rul 5

使用imagedestroy清除与$ image和$ image_p相关的任何内存:

$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
imagejpeg($image_p, $output_filename, 80);
imagedestroy($image);
imagedestroy($image_p);
Run Code Online (Sandbox Code Playgroud)