gd-png无法分配图像数据 [英] gd-png cannot allocate image data

查看:61
本文介绍了gd-png无法分配图像数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个古老的代码库,该代码库大量使用了GD(不,不可以切换到imagemagick).它已经运行了好几年了,已经有多个版本了.但是,当我在当前的开发环境中运行它时,我遇到了一个神秘的gd-png错误:调用 imagecreatefrompng()时无法分配图像数据错误.我正在使用的PNG文件与我一直使用的PNG文件相同,所以我知道它可以工作.

I have an old code base that makes extensive use of GD (no, switching to imagemagick is not an option). It's been running for several years, through several versions. However, when I run it in my current development environment, I'm running into a mysterious gd-png error: cannot allocate image data errors when calling imagecreatefrompng(). The PNG file I'm using is the same as I've been using, so I know it works.

当前设置:

Ansible-provisioned vagrant box  
Ubuntu 14.04  
PHP 5.5.9  
GD 2.1.1  
libPNG 1.2.50

运行脚本时,PHP的内存限制为650M,尽管最终最终是内核本身终止了该脚本,并且更改PHP的内存限制似乎没有效果.

PHP's memory limit at the time the script is run is 650M, though it's ultimately the kernel itself that ends up killing the script, and changing PHP's memory limit doesn't seem to have an effect.

图像尺寸为7200x6600,在磁盘上约为500KiB.在我的其他环境中这不是问题,而在我的开发环境中只是新出现的问题.不幸的是,我无法再访问其他环境进行比较了,尽管上一个工作环境中的设置是相似的-Ubuntu 14.04,PHP 5.5,足够的内存分配.

The image dimensions are 7200x6600 and are about 500KiB on disk. This hasn't been a problem in my other environments and is only newly-occurring in my development environment. Unfortunately, I don't have access to the other environments anymore to do a comparison, though the setup was similar in the last working one -- Ubuntu 14.04, PHP 5.5, sufficient memory allocations.

此设置中可能会发生什么,而以前的设置中没有发生?我该如何解决?

What could be happening in this setup that wasn't happening in my previous setups? How can I fix this?

推荐答案

我正在浏览PHP 5.5.9源,试图找到您的特定错误字符串无法分配图像数据",但我能找到的最接近的字符串是"gd-png错误:无法分配gdImage结构".GD for PHP 5.5.9的捆绑版本(一直到7.0.0)一直是2.0.35版,因此看来您正在寻找自定义的build(?).调试PHP专家可能对这里没有帮助.

I was browsing a bit through the PHP 5.5.9 source to try and find your specific error string "cannot allocate image data", but the closest I could find is "gd-png error: cannot allocate gdImage struct". The bundled version of GD for PHP 5.5.9 (all the way up to 7.0.0) is version 2.0.35, so it seems you're looking at a custom build(?). Bugging the PHP guys might not help here.

在查看GD源(2.1.2,似乎找不到2.1.1)时,发生此错误的位置是: https://github.com/libgd/libgd/blob/master/src/gd_png.c#L435

Looking at the GD source (2.1.2, can't seem to find 2.1.1), the only place this error occurs is: https://github.com/libgd/libgd/blob/master/src/gd_png.c#L435

image_data = (png_bytep) gdMalloc (rowbytes * height);
if (!image_data) {
    gd_error("gd-png error: cannot allocate image data\n");
    png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
    if (im) {
        gdImageDestroy(im);
    }
    if (palette_allocated) {
        gdFree (palette);
    }
    return NULL;
}

gdMalloc在哪里:

Where gdMalloc is:

https://github.com/libgd/libgd/blob/GD-2.1/src/gdhelpers.c#L73

 void *
 gdMalloc (size_t size)
 {
      return malloc (size);
 }

恐怕这是我侦探工作所能做到的.据我所知,PHP中捆绑的2.0.35 GD版本也仅使用malloc,因此乍看之下并没有真正的区别.我还尝试在捆绑版本中找到一些等效的代码,,但到目前为止我还没有找到,它似乎在这里:

I'm afraid this is as far as my detective work goes. As far as I can tell the bundled 2.0.35 GD versions in PHP also just use malloc, so at first glance there's no real difference there. I also tried to find some equivalent piece of code in the bundled versions, but so far I haven't found it and it seems to be here:

https://github.com/php/php-src/blob/PHP-5.5.9/ext/gd/libgd/gd_png.c#L323

 image_data = (png_bytep) safe_emalloc(rowbytes, height, 0);

我似乎找不到safe_emalloc,但是似乎旧的PHP捆绑版本在这里使用的内存分配与您的环境使用的版本不同.也许最好的选择是与GD开发人员联系?.为避免追逐快乐的鹅,我认为尝试另一种环境是最好的选择-在确认它们确实使用了非标准GD版本之后.

I can't seem to find safe_emalloc, but it seems the old PHP bundled versions did use a different memory allocation here than the version your environment uses. Perhaps your best bet is to check with the GD devs?. To avoid a merry goose chase, I think trying another environment is your best bet -after confirming they are indeed using a non-strandard GD version.

在再进行一些PHP源代码浏览之后,似乎在所有扩展中都使用了safe_emalloc,所以(猜测),我猜这是为PHP扩展分配内存的首选方法(

After some more PHP source safari, it seems safe_emalloc is used throughout all extensions, so I guess (guess mind you) that this is the preferred way to allocate memory for PHP extensions (looks like it). If your environment is in fact using an unaltered GD 2.1.1, it's likely it is ignoring any PHP memory settings/limits. You might be able to find some other way of specifying the ('stand-alone') GD memory limit?

编辑:查看官方libgd常见问题解答,在底部附近,它表示PHP扩展应该确实遵守内存限制,并指定8000x8000像素的图像大约需要256MB,因此您的650MB限制应该足够了(除非您正在使用同一运行中有多个副本?),并且事实证明它不起作用,这证明内存分配正在发生混乱.

Looking at the official libgd faq, near the bottom it states that the PHP extension should indeed respect the memory limit and it specifies that an 8000x8000 pixel image would take about 256MB, so your 650MB limit should really be enough (unless you're working with multiple copies in the same run?) and the fact that it's not working corroborates that something fishy is going on with the memory allocation.

这篇关于gd-png无法分配图像数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆