php - 为什么这个基本的 imagejpeg() 缩放器返回黑色图像?

标签 php image image-processing upload gd

编辑

感谢您的所有回答,特别是 @Mailerdaimon,他注意到我没有在 imagecopyresampled 函数中使用计算值。

我不再得到黑色图像,但我仍然得到一些黑色部分,所以我认为我的比率公式应该更新:如果我上传横向图像,新图像的高度小于 170 像素,然后有一些黑色的表现。

我怎样才能确保图像的高度一直保持不变?


下面是一个允许用户上传图片的简单脚本。上传完成后,图片将显示为 170 像素(高)x 150 像素(宽)的缩略图。

调整大小部分确实有效,因为输出图像是 170x150px 但我仍然会得到一些黑色区域,如果

if ($_SERVER["REQUEST_METHOD"] == "POST") 
{

$maxWidth  = 150;
$maxHeight = 170;

$name = $_FILES ['image'] ['name'];
$type = $_FILES ["image"] ["type"];
$size = $_FILES ["image"] ["size"];
$tmp_name = $_FILES ['image'] ['tmp_name'];
list($originalWidth, $originalHeight) = getimagesize($tmp_name);


 if ($originalWidth > $originalHeight) 
 {
   $thumbnail_height = floor(($originalHeight/$originalWidth)*$maxWidth);
   $thumbnail_width  = $maxWidth;
 } else {
   $thumbnail_width  = floor(($originalWidth/$originalHeight)*$maxHeight);
   $thumbnail_height = $maxHeight;
 }

 // Resample  
  $image_p = imagecreatetruecolor($maxWidth, $maxHeight);
  imagecreatefrompng($tmp_name);
  imagecopyresampled($image_p, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, 
  $originalWidth, $originalHeight);

//start upload process
$RandomNumber = uniqid();
$location = "uploads/$RandomNumber";
imagejpeg($image_p,  $location, 100);      

$sql=query("UPDATE users SET image = '".$location."' WHERE id = '$id'"); 

        } 
  }

知道我做错了什么吗?

最佳答案

我从未使用过 php,所以这可能不正确,但我看不到您在哪里使用计算出的宽度和高度值!

if ($originalWidth > $maxWidth || $originalHeight > $maxHeight)
 {
      if ($originalWidth / $maxWidth > $originalHeight / $maxHeight) 
     {
       // width is the limiting factor
       $width = $maxWidth;
       $height = floor($width * $originalHeight / $originalWidth);

     } else { 
       // height is the limiting factor
       $height = $maxHeight;
       $width = floor($height * $originalWidth / $originalHeight);
     }

在这里你计算高度和宽度,然后它们就不会出现在你的代码中的任何地方......

编辑: 您使用:

$image_p = imagecreatetruecolor($maxWidth, $maxHeight);
imagecreatefrompng($tmp_name);
imagecopyresampled($image_p, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height,$originalWidth, $originalHeight);

但是Documentation imagecopyresmapled 声明该函数遵循以下内容

imagecopyresampled ($dst_image ,$src_image ,$dst_x ,$dst_y ,$src_x ,$src_y ,$dst_w , $dst_h ,$src_w ,$src_h )

虽然我认为你的代码应该是

imagecopyresampled($image_p,$tmp_name, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height,$originalWidth, $originalHeight);

正如@Manolo Salsas(和其他几个人)已经提到的

编辑 2: 另一个问题在于如何计算高度和宽度值。 如果 $height 像这样计算

$height = floor($width * $originalHeight / $originalWidth);

它可以小于 $maxHeight,但会在结果图像中创建一个黑色(未填充)区域,因为您使用最大值创建了此图像

$image_p = imagecreatetruecolor($maxWidth, $maxHeight);

要解决此问题,请使用您计算的 $thumbnail_height 和 $thumbnail_width 值来创建图像

 $image_p = imagecreatetruecolor($thumbnail_width, $thumbnail_height);

或者始终将图像调整为最大值

imagecopyresampled($image_p,$tmp_name, 0, 0, 0, 0, $maxWidth, $maxHeight,$originalWidth, $originalHeight);

这可能会扭曲图像。如果您不想扭曲图像,请考虑先调整大小,使尺寸适合缩略图,然后裁剪较长的一侧。

关于php - 为什么这个基本的 imagejpeg() 缩放器返回黑色图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19551747/

相关文章:

php - 我需要学习专业的PHP网站开发

javascript - 如何显示图像的非常具体的部分

swift - UITableView 单元格背景

python - 如何实时使用 matplotlib?

c++ - 当画面中出现蓝色时,图像处理失败。可能是什么原因?

image - 清理二值图像的最佳方法

php - 如何在没有控制台访问权限的情况下处理主机上的 yii2 迁移?

php - 我们如何将 PHP 变量注入(inject) Javascript

带有 CSS 的矩形上的 JavaFX 图像

PHP:数组搜索组