imagecopyresized()返回黑色缩略图

时间:2013-10-14 02:10:32

标签: php html file-upload upload thumbnails

我之前发布过关于另一个不能正常工作的调整大小脚本,我对这个脚本的处理方式稍微有些不同。

我走得更远,但现在又出现了一个新问题。代码的前三行成功地在目标目录中将三个相同的文件与文件放在一起,并且相应地命名了两个缩略图文件。然后,我想要加载仍然是全尺寸的缩略图,并调整它们的大小,但脚本停在imagecreatefromjpeg(),我似乎无法找出原因,因为$src有一个值。

我认为我可能会移除该行并将$source替换为$src函数中的imagecopyresized(),这让我更加接近。但它会返回目标尺寸的缩略图,但缩略图为黑色

move_uploaded_file($tmpFilePath, $newFilePath);
copy($newFilePath, $thumb500);
copy($newFilePath, $thumb200);


function thumbImage($src, $dest, $newheight) {
  list($width, $height) = getimagesize($src);

  $newwidth = $width * ($newheight / $height);

  // Load
  $thumb = imagecreatetruecolor($newwidth, $newheight);
  header('Content-type: image/jpeg');
  $source = imagecreatefromjpeg($src);

  // Resize
  imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

  // Output
  imagejpeg($thumb, $dest);
}

thumbImage($thumb500, $thumb500, 500);
thumbImage($thumb200, $thumb200, 200);

我觉得这一定是个常见问题。有人建议吗?

1 个答案:

答案 0 :(得分:0)

对我来说,如果我使用JPEG图像作为源,则提供的代码块可以工作。

问题可能是您使用的是使用透明度的PNG图像。由于JPEG无法处理透明胶片,因此将填充透明背景颜色。也许这就是问题所在。如果没有,请提供具有问题行为的示例图像。