imagecopyresampled 有时会创建空白图像

发布于 2024-11-08 00:44:38 字数 1290 浏览 0 评论 0原文

我正在使用以下方法调整图像大小:

//load file and dimensions
$obj_original_image = imagecreatefromjpeg($str_file_path);
list($int_width, $int_height, $image_type) = getimagesize($str_file_path);

//compress file
$int_thumbnail_width = 320;
$int_ratio = $int_thumbnail_width / $int_width;
$int_new_width = $int_thumbnail_width;
$int_new_height = $int_height * $int_ratio;

$obj_image = imagecreatetruecolor($int_new_width, $int_new_height);
imagecopyresampled($obj_image, $obj_original_image, 0, 0, 0, 0, $int_new_width,     $int_new_height, $int_width, $int_height);
imagejpeg($obj_image, $GLOBALS['serverpath'] . '/images/uploaded/video-thumbs/'.$arr_data['thumbnail_folder'].'/' . $arr_data['id']. '.jpg', $int_compression = 85);

这通常工作得很好。但有时,它会产生空白的白色图像。我已阅读此注释 http://php.net/manual/en/function.imagecopyresampled .php

由于调色板图像限制(255+1 种颜色)而出现问题。重新采样或过滤图像通常需要超过 255 种颜色,使用一种近似值来计算新的重新采样像素及其颜色。对于调色板图像,我们尝试分配新颜色,如果失败,我们选择最接近的(理论上)计算颜色。这并不总是最接近的视觉颜色。这可能会产生奇怪的结果,例如空白(或视觉上空白)图像。要跳过此问题,请使用真彩色图像作为目标图像,例如由 imagecreatetruecolor() 创建的图像。

不过,我已经在使用 imagecreatetruecolor 了。我总是缩放相同尺寸的图像,因此这不会是宽度/高度问题。它只是有时发生,大多数时候图像缩放工作正常。关于如何解决这个问题有什么想法吗?

I am resizing images using:

//load file and dimensions
$obj_original_image = imagecreatefromjpeg($str_file_path);
list($int_width, $int_height, $image_type) = getimagesize($str_file_path);

//compress file
$int_thumbnail_width = 320;
$int_ratio = $int_thumbnail_width / $int_width;
$int_new_width = $int_thumbnail_width;
$int_new_height = $int_height * $int_ratio;

$obj_image = imagecreatetruecolor($int_new_width, $int_new_height);
imagecopyresampled($obj_image, $obj_original_image, 0, 0, 0, 0, $int_new_width,     $int_new_height, $int_width, $int_height);
imagejpeg($obj_image, $GLOBALS['serverpath'] . '/images/uploaded/video-thumbs/'.$arr_data['thumbnail_folder'].'/' . $arr_data['id']. '.jpg', $int_compression = 85);

And this is generally working perfectly. However occasionally, it is producing a blank, white image. I have read this note at http://php.net/manual/en/function.imagecopyresampled.php:

There is a problem due to palette image limitations (255+1 colors). Resampling or filtering an image commonly needs more colors than 255, a kind of approximation is used to calculate the new resampled pixel and its color. With a palette image we try to allocate a new color, if that failed, we choose the closest (in theory) computed color. This is not always the closest visual color. That may produce a weird result, like blank (or visually blank) images. To skip this problem, please use a truecolor image as a destination image, such as one created by imagecreatetruecolor().

However, I am already using imagecreatetruecolor. I am always scaling the same sized images so it can't be a width/height issue. It is only happening sometimes, most times the image scaling is working fine. Any ideas as to how to fix this?

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

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

发布评论

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

评论(2

撩起发的微风 2024-11-15 00:44:38

当您已经使用 imagecreatefromjpeg() 打开图像时,就没有理由再使用 getimagesize()。您可以使用imagesx()imagesy()来获取宽度/高度。 getimagesize 与 GD 是分开的,并且会重新打开图像、重新解析它等等...

还要注意,GD 非常愚蠢,它会强制你确定你拥有的图像类型并调用适当的 createfrom< /代码> 功能。如果您尝试打开 .jpg 图像以外的任何图像,imagecreatefromjpeg() 将失败。

$obj_original_image = imagecreatefromjpeg($str_file_path);
if ($obj_original_image === FALSE) {
   die("Unable to load $str_file_path. Not a jpg?");
}

etc... etc...

$status = imagecopyresampled($obj_image, $obj_original_image, 0, 0, 0, 0, $int_new_width,     $int_new_height, $int_width, $int_height);

if ($status === FALSE) {
    die("imagecopyresampled failed!");
}

另外,在高度/宽度计算中添加一些调试 - 输出您生成的值。也许其中一个或多个结果为 0,因此您最终会重新采样到不存在的大小。

There's no reason to be using getimagesize() when you've already opened the image with imagecreatefromjpeg(). You can use the imagesx() and imagesy() to get the width/height. getimagesize is seperate from GD and will re-open the image, re-parse it, etc...

Also note that GD is pretty stupid and forces you to determine what image type you've got and call the appropriate createfrom function. imagecreatefromjpeg() will fail if you try to open anything OTHER than a .jpg image.

$obj_original_image = imagecreatefromjpeg($str_file_path);
if ($obj_original_image === FALSE) {
   die("Unable to load $str_file_path. Not a jpg?");
}

etc... etc...

$status = imagecopyresampled($obj_image, $obj_original_image, 0, 0, 0, 0, $int_new_width,     $int_new_height, $int_width, $int_height);

if ($status === FALSE) {
    die("imagecopyresampled failed!");
}

As well, add some debugging to your height/width calculations - output the values you're generating. Maybe one or more of them is coming out as 0, so you end up resampling to a non-existent size.

自由如风 2024-11-15 00:44:38

您提到正在处理的图像的大小始终相同,但是分辨率始终相同吗?根据我的经验,如果您正在处理特别高分辨率的图像,则某些操作可能会耗尽内存,从而产生空白的白色图像。如果是这种情况,您可以尝试通过增加 php.ini 中的 memory_limit 参数下分配给 PHP 的内存量来解决此问题。据我所知,该值适用于图像处理。

You mentioned that the size of the image being processed is always the same, but is the resolution always the same? If you have a particularly high resolution image you're working with, you may be running out of memory for certain operations which will produce a blank white image, in my experience. If this is the case, you could try to fix it by increasing the amount of memory allocated to PHP in php.ini under the memory_limit parameter. As far as I know, that value applies to image manipulations.

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