调整对于我的服务器上的 GD 库来说太大的图像大小

发布于 2024-08-02 00:01:02 字数 199 浏览 2 评论 0原文

我几乎总是在我的客户的网站中使用 PHP 调整大小脚本(使用 GD)。 无论我多少次告诉他们在上传之前调整数码相机中的巨大 7MP 图像的大小,他们仍然不这样做。 结果是服务器出现“内存不足”错误,并且图像不会调整大小,因为原始图像的分辨率太大。

有没有更好的方法来调整大图像的大小? 是否有提供 API 的服务,我可以通过脚本来调整这些大图像的大小?

I almost always have a PHP resize script (using GD) in websites for my clients. No matter how many times I tell them to resize their huge 7MP images from their digital camera before uploading them, they still never do it. The result is an "Out Of Memory" error from the server, and the image doesn't get resized, because the original image was too large resolution-wise.

Is there a better way to resize really large images? Is there a service that offers an API that I could tap into through my script to resize these large images?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

柒夜笙歌凉 2024-08-09 00:01:02

你可以调用 Imagemagick 来调整你的 php 页面中的图片大小,但是有一个安全问题,如果你允许系统调用

任何 API 都会使用大量内存,你也可以更改你的 php memory_limit 。

You can call Imagemagick to resize the picture from your php page, but there's a security matter then if you allow system calls

Any API will use a lot of Memory, you can change your php memory_limit aswell.

别低头,皇冠会掉 2024-08-09 00:01:02

如果您的 PHP 配置中有可用的 ImageMagick api - 请使用它! ImageMagick 不计入 PHP memory_limit 中,因此可以处理更大的文件。

如果您确实无法控制服务器,那么快速但非常糟糕的解决方法是 - 从 $_POST 获取图像文件后,首先使用以下命令检查尺寸:

<?php
$image_size = getimagesize("yourfile.jpg")
$mem_usage = $image_size[0] * $image_size[1] * 4 //4 bytes per pixel (RGBA)
if ($mem_usage > $mem_aviable) {
    die ('Please upload smaller pic. I tell you that all the time.  '.
         'Resize yout huge 7MP images from your digital camera '.
         'before uploading them.'); //Better put something more polite here.
}

If you have available ImageMagick api in your PHP configuration - use it! ImageMagick doesn't get counted in PHPs memory_limit and therefore can work with bigger files.

If you realy have no control over the server, the quick and very bad workaround is - after getting the image file from $_POST, first check the dimensions with:

<?php
$image_size = getimagesize("yourfile.jpg")
$mem_usage = $image_size[0] * $image_size[1] * 4 //4 bytes per pixel (RGBA)
if ($mem_usage > $mem_aviable) {
    die ('Please upload smaller pic. I tell you that all the time.  '.
         'Resize yout huge 7MP images from your digital camera '.
         'before uploading them.'); //Better put something more polite here.
}
冰雪梦之恋 2024-08-09 00:01:02

我被这个问题困扰了几天(几夜)。

此外,各个提供商的内存限制各不相同。
更改 PHP 中的内存限制在共享服务器上不起作用,提供商通常会限制 ram,即使 phpinfo() 说您有 128Mo(1and1 将每个进程的 RAM 限制为 60Mo)例如)。

但我终于在这里找到了一些非常有效的东西: http://www.imagemagick.org/Usage/files /#massive

它需要 imagemagick,但我发现大多数提供商在其服务器上本机提供此功能,甚至是共享服务器。

exec('env MAGICK_TMPDIR=<tmp_dir> nice -5 convert -limit memory 32 -limit map 32 -resize 800x600 huge.jpg reasonable.jpg'); 

正如所说:

  • env MAGICK_TMPDIR= 为 imagemagick 设置临时目录
    模拟 ram(某种)

  • nice -5 也是一个用于更改进程优先级的 unix 命令
    (http://en.wikipedia.org/wiki/Nice_(Unix) )

  • convert ... 是 imagemagick 命令行

真正的交易是关于 -limit内存 32-限制映射 32。 这是限制二进制文件使用的内存的方法(此处:32Mo)。 您可能需要调整这些值以匹配您的服务器的值(一般来说,当 PHP 出现致命错误时,它会告诉您最大分配内存。我建议您将此值除以 2 或 4,以方便使用)。

我还需要在 PHP 中添加一些其他行以避免一些附带问题:

ignore_user_abort(true); // ignore user abort : let the script finish resizing even if user aborts
set_time_limit(0); // ignore server timeout
putenv('MAGICK_THREAD_LIMIT=1'); // limit the number of thread for the binary. Very important in my case 

希望所有这些都会有所帮助...


要知道 convert 在您的服务器上是否可用,您可以尝试这个(在 PHP 中) :

$out = array();
exec('which convert 2>&1', $out);
print_r($out); 

这将为您提供二进制文件的路径(如果存在)。

I got stuck for days (and nights) with this problem.

Moreover, providers memory limit differs from one to another.
And change the memory_limitin PHP does not work on shared servers, providers usually limit the ram even if phpinfo() says that you got 128Mo (1and1 limits RAM to 60Mo per process for example).

But I finally found something quite efficient here : http://www.imagemagick.org/Usage/files/#massive

It needs imagemagick, but I discovered that most of the providers natively provides this on their servers, even shared ones.

exec('env MAGICK_TMPDIR=<tmp_dir> nice -5 convert -limit memory 32 -limit map 32 -resize 800x600 huge.jpg reasonable.jpg'); 

As it's said :

  • env MAGICK_TMPDIR=<tmp_dir> set up a temp directory for imagemagick
    to simulate ram (kind of)

  • nice -5 is also a unix command to change the priority of a process
    (http://en.wikipedia.org/wiki/Nice_(Unix))

  • convert ... is the imagemagick command line

The real deal is about -limit memory 32 and -limit map 32. This is the way you limit the memory used by the binary (here : 32Mo). You will probably need to fit the values to match the values of your server (Generally PHP tells you the maximum allocated memory when it gives you the Fatal Error. I suggest you to divide this value by 2 or 4 to be confortable).

I also needed to put some other lines in my PHP to avoid some collateral issues :

ignore_user_abort(true); // ignore user abort : let the script finish resizing even if user aborts
set_time_limit(0); // ignore server timeout
putenv('MAGICK_THREAD_LIMIT=1'); // limit the number of thread for the binary. Very important in my case 

Hope all that will help ...


To know if convert is available on your server, you can try this (in PHP) :

$out = array();
exec('which convert 2>&1', $out);
print_r($out); 

That will gives you the path of the binary, if exists.

陈年往事 2024-08-09 00:01:02

您可以使用外部程序,例如使用 convert

You could shell out to an external programme, for example using convert.

留蓝 2024-08-09 00:01:02

上传它们然后排队调整大小作业怎么样,然后命令行 php 脚本可以拾取并处理(更快,并且可以有单独的内存限制和执行时间)

how about uploading them then queuing the resize job, a command line php script can then pick up and process (faster and can have a separate memory limit and execution time)

苄①跕圉湢 2024-08-09 00:01:02

只需让他们在上传之前将其调整小即可。 最大文件大小

Just make them resize it smaller before they upload it. MAX_FILE_SIZE

盗梦空间 2024-08-09 00:01:02

我花了一些时间尝试调试直接使用 GD 的 PHP 代码,但在大图像和图像子集上失败了,这些图像似乎存在一些小错误,导致 GD 窒息。

但后来我发现了 phpThumb (http://phpthumb.sourceforge.net/),我永远不会去后退。 将两个 PHP 文件添加到我的 Web 目录(phpthumb.class.phpphpthumb.functions.php)后,我可以使用以下代码生成缩略图:

include("phpthumb.class.php"); 

// Load data into phpThumb object and set parameters
$phpThumb = new phpThumb();
$phpThumb->setSourceData(file_get_contents($_GET['filename']));
$phpThumb->setParameter('w', $width);  // Set width
$phpThumb->setParameter('h', $height); // Set height
$phpThumb->setParameter('zc', 1);      // Specify zoom-crop (instead of stretch)
$phpThumb->setParameter('q', 100);     // Specify quality

// Generate & output thumbnail
$phpThumb->GenerateThumbnail();        // Generate the thumbnail
$phpThumb->OutputThumbnail();          // Output it to browser
$phpThumb->RenderToFile("$thumbname"); // Output it to file
$phpThumb->purgeTempFiles();           // Clean up

Boom ,工作起来就像一个魅力,速度相当快,并且在处理大文件时没有问题。 此外,它可以使用 GD 或 ImageMagick,具体取决于服务器上可用的内容,因此它相对便携。

I had spent some time trying to debug PHP code that used GD directly, but failed on large images and on a subset of images that seemed to have minor errors that choked GD.

But then I found phpThumb (http://phpthumb.sourceforge.net/), and I'm never going back. After adding two PHP files to my web directory (phpthumb.class.php and phpthumb.functions.php) I was able to use the following code to generate my thumbnails:

include("phpthumb.class.php"); 

// Load data into phpThumb object and set parameters
$phpThumb = new phpThumb();
$phpThumb->setSourceData(file_get_contents($_GET['filename']));
$phpThumb->setParameter('w', $width);  // Set width
$phpThumb->setParameter('h', $height); // Set height
$phpThumb->setParameter('zc', 1);      // Specify zoom-crop (instead of stretch)
$phpThumb->setParameter('q', 100);     // Specify quality

// Generate & output thumbnail
$phpThumb->GenerateThumbnail();        // Generate the thumbnail
$phpThumb->OutputThumbnail();          // Output it to browser
$phpThumb->RenderToFile("$thumbname"); // Output it to file
$phpThumb->purgeTempFiles();           // Clean up

Boom, works like a charm, is pretty fast and has no problems with large files. Also, it can use either GD or ImageMagick, depending on what's available on the server, so it's relatively portable.

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