PHP - 调整 PNG 图像大小时出现内存错误
我有一个脚本可以根据上传的图像创建缩略图。它适用于 jpg,但给我一个错误
致命错误:允许的内存大小 67108864 字节已耗尽(尝试分配 26250000 字节)
致命错误:当我上传 png 图像时,
。脚本是:
//create thumbnail; $modwidth and height are calculated in another part of the script
//$original is the path to the full sized image
$tn = imagecreatetruecolor($modwidth, $modheight);
switch (strrchr($new_image_name,'.')) {
case ".jpg":
$image = imagecreatefromjpeg($original);
break;
case ".jpeg":
$image = imagecreatefromjpeg($original);
break;
case ".png":
$image = imagecreatefrompng($original);
break;
case ".gif":
$image = imagecreatefromgif($original);
break;
}
imagecopyresampled($tn, $image, 0, 0, $x_pos, $y_pos, $modwidth, $modheight, $width, $height);
switch (strrchr($new_image_name,'.')) {
case ".jpg":
imagejpeg($tn, $target_path, 100);
break;
case ".jpeg":
imagejpeg($tn, $target_path, 100);
break;
case ".png":
imagepng($tn, $target_path, 0);
break;
case ".gif":
imagegif($tn, $target_path);
break;
}
正如我所说,它与 JPG 和 GIF 完美配合。 该内存错误仅出现在 PNG 中,而且我只使用了 1.2Mb 的图像。
我该如何解决这个问题? 谢谢 帕特里克
I have a script that creates a thumbnail out of an uploaded image. it works fine with jpgs, but gives me an error
Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 26250000 bytes)
when I upload a png image.
The script is:
//create thumbnail; $modwidth and height are calculated in another part of the script
//$original is the path to the full sized image
$tn = imagecreatetruecolor($modwidth, $modheight);
switch (strrchr($new_image_name,'.')) {
case ".jpg":
$image = imagecreatefromjpeg($original);
break;
case ".jpeg":
$image = imagecreatefromjpeg($original);
break;
case ".png":
$image = imagecreatefrompng($original);
break;
case ".gif":
$image = imagecreatefromgif($original);
break;
}
imagecopyresampled($tn, $image, 0, 0, $x_pos, $y_pos, $modwidth, $modheight, $width, $height);
switch (strrchr($new_image_name,'.')) {
case ".jpg":
imagejpeg($tn, $target_path, 100);
break;
case ".jpeg":
imagejpeg($tn, $target_path, 100);
break;
case ".png":
imagepng($tn, $target_path, 0);
break;
case ".gif":
imagegif($tn, $target_path);
break;
}
As I said it works perfectly with JPGs and also with GIFs.
That memory error appears only with PNGs, and I have only used a 1.2Mb image.
How can i solve this?
thanks
Patrick
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在脚本之前使用
ini_set('memory_limit', '256M');
。Use
ini_set('memory_limit', '256M');
before script.您需要将 php.ini 中的 memory_limit 设置增加到类似这样的值
You need to increase memory_limit setting in php.ini to something like this