PHP - 调整 PNG 图像大小时出现内存错误

发布于 2024-08-10 19:31:03 字数 1194 浏览 2 评论 0原文

我有一个脚本可以根据上传的图像创建缩略图。它适用于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫九 2024-08-17 19:31:03

在脚本之前使用 ini_set('memory_limit', '256M');

Use ini_set('memory_limit', '256M'); before script.

葬シ愛 2024-08-17 19:31:03

您需要将 php.ini 中的 memory_limit 设置增加到类似这样的值

memory_limit = 128M

You need to increase memory_limit setting in php.ini to something like this

memory_limit = 128M
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文