为什么这个基本的imagejpeg()缩放器返回黑色图像? [英] Why does this basic imagejpeg() resizer returns a black image?

查看:132
本文介绍了为什么这个基本的imagejpeg()缩放器返回黑色图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑

感谢您的所有答案,特别是@Mailerdaimon注意到我没有使用<$中的计算值c $ c> imagecopyresampled 功能。

Thanks for all your answers, especially @Mailerdaimon who noticed that I wasn't using the computed values in the imagecopyresampled function.

我不再获得黑色图像,但我仍然会得到一些黑色部分,所以我想到了比率公式应该更新:如果我上传风景图像,新图像的高度小于170像素,然后有一些黑色显示。

I don't get black images anymore, but i still do get some black part so i figure my ratio formula should be updated : if i upload a landscape image, the height of the new image is smaller than 170px, and then there's some black showing.

如何确保图像的高度一直保持?

以下是允许用户上传图片的简单脚本。上传完成后,图片将显示为170px(h)x 150px(W)缩略图。

Below is a simple script to allow users upload pictures. Once the upload is done, the pictures are displayed as a 170px(h) x 150px(w) thumbnail.

调整大小部分可以正常工作因为输出图像是170x150px但我仍然会得到一些黑色区域,如果

The resize part does work since the output image is 170x150px BUT i still get some black area if

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'"); 

        } 
  }

知道我在做什么错了吗?

Any idea what I'm doing wrong?

推荐答案

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

I never used php, so this might not be correct, but i can´t see where you use your computed values for width and height!

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);
     }

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

here you compute the height and width and after that they dont appear anywhere in your code...

编辑:
您使用:

You use:

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

imagecopyresmapled 的文档声明该函数采用以下

But the Documentation of imagecopyresmapled states that the function takes the follow

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

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

Though i think your code should be

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

正如@Manolo Salsas(以及其他几位)已经提到的那样

as already mentioned by @Manolo Salsas (and several others)

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

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

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

it can be smaller than $maxHeight, though creating a black (unfilled) area in your Resulting image, as you have created this image using the max values

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

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

To fix this use your computed $thumbnail_height and $thumbnail_width values to create the image

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

或始终将图片大小调整为最大值

or always resize your image to the max values

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

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

which may distort the image. If you dont want to distort the image think about resizing first such that on size fits in the thumbnail and then crop the longer side.

这篇关于为什么这个基本的imagejpeg()缩放器返回黑色图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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