使用 PHP 从 PNG 转换时,JPEG 图像会变成全黑

发布于 2024-09-29 15:22:40 字数 1850 浏览 2 评论 0原文

问题:将任何 PNG 图像转换为 JPEG 时,图像会变成全黑

首先,我搜索了互联网和 stackoverflow 以了解如何执行此操作。我尝试了 PHP 手册和 Stack Overflow 上能找到的所有方法。问题仍然存在。我正在使用 GD(没有安装 ImageMagick)。

我的代码如下。这是对该函数的调用:

$tempImage = $dirPath.$filename.$tempMini.".jpg";           
createTempImage($sourcefile, $tempImage, $tempMini_width, $tempMini_height, 100);

我已经注释掉了我尝试过的不同方法。

function createTempImage($sourcefile, $setNewName, $maxwidth, $maxheight, $quality){

$fileInfoArray = getimagesize($sourcefile);
$imagetype = $fileInfoArray['mime'];

if($imagetype == 'image/jpeg'){
    $img = imagecreatefromjpeg($sourcefile);

}elseif($imagetype == 'image/gif'){
    $img = imagecreatefromgif($sourcefile);

}elseif(($imagetype == 'image/png')||($imagetype == 'image/x-png')){
    $img = imagecreatefrompng($sourcefile);
}

$width = imagesx( $img );
$height = imagesy( $img );

if ($width > $maxwidth || $height > $maxheight){
    $factor = min(($maxwidth/$width),($maxheight/$height));
    $newwidth = round($width*$factor);
    $newheight = round($height*$factor);
} else {
    $newwidth = $width;
    $newheight = $height;
}   


$tmpimg = imagecreatetruecolor( $newwidth, $newheight );
imagecopyresampled($tmpimg, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height );
imagejpeg($tmpimg, $setNewName, 100);

imagedestroy($tmpimg);
imagedestroy($img);

更新

还尝试了以下操作:

$white = imagecolorallocate($tmpimg, 255, 255, 255);
ImageFill($tmpimg, 0, 0, $white);
ImageSaveAlpha($tmpimg, false);
ImageAlphaBlending($tmpimg, false);
$white = imagecolorallocate($tmpimg,  255, 255, 255);
imagefilledrectangle($tmpimg, 0, 0, $newwidth, $newheight, $white);

:顶部黑框是图像结果: http://twitpic.com/30ywf5< /a>

The problem: When converting any PNG image into a JPEG, the image turns all black.

To start off, I've searched the internet and stackoverflow to find out how to do this. I've tried every method I could find in the PHP Manual and on Stack Overflow. The problem still exists. I'm using GD (don't have ImageMagick installed).

My code is below. This is the call to the function:

$tempImage = $dirPath.$filename.$tempMini.".jpg";           
createTempImage($sourcefile, $tempImage, $tempMini_width, $tempMini_height, 100);

I've commented out the different methods that I've tried.

function createTempImage($sourcefile, $setNewName, $maxwidth, $maxheight, $quality){

$fileInfoArray = getimagesize($sourcefile);
$imagetype = $fileInfoArray['mime'];

if($imagetype == 'image/jpeg'){
    $img = imagecreatefromjpeg($sourcefile);

}elseif($imagetype == 'image/gif'){
    $img = imagecreatefromgif($sourcefile);

}elseif(($imagetype == 'image/png')||($imagetype == 'image/x-png')){
    $img = imagecreatefrompng($sourcefile);
}

$width = imagesx( $img );
$height = imagesy( $img );

if ($width > $maxwidth || $height > $maxheight){
    $factor = min(($maxwidth/$width),($maxheight/$height));
    $newwidth = round($width*$factor);
    $newheight = round($height*$factor);
} else {
    $newwidth = $width;
    $newheight = $height;
}   


$tmpimg = imagecreatetruecolor( $newwidth, $newheight );
imagecopyresampled($tmpimg, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height );
imagejpeg($tmpimg, $setNewName, 100);

imagedestroy($tmpimg);
imagedestroy($img);

}

The following have also been attempted:

$white = imagecolorallocate($tmpimg, 255, 255, 255);
ImageFill($tmpimg, 0, 0, $white);
ImageSaveAlpha($tmpimg, false);
ImageAlphaBlending($tmpimg, false);
$white = imagecolorallocate($tmpimg,  255, 255, 255);
imagefilledrectangle($tmpimg, 0, 0, $newwidth, $newheight, $white);

Update: The top black box is the image result: http://twitpic.com/30ywf5

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

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

发布评论

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

评论(2

眼趣 2024-10-06 15:22:40

只是几个想法:

  • $newHeight = $maxheight; 似乎是一个拼写错误,整个代码中“newheight”的拼写没有大写“H”。

  • 可以大大缩短确定新尺寸的代码:

if ($width > $maxwidth || $height > $maxheight ){
$factor = min(($maxwidth/$width),($maxheight/$height));
$newwidth = round($width*$factor);
$newheight = round($height*$factor);
}

  • 您使用 imagecopyresampled 创建新图像 - 这只适用于特定的 GD 版本(“版本 2”),请尝试使用 imagecopyresized否则。

Just a couple of ideas:

  • $newHeight = $maxheight; seems to be a typo, "newheight" is spelled without the capital "H" throughout the code.

  • The code to determine the new size can be shortened sigificantly:

if ($width > $maxwidth || $height > $maxheight){
$factor = min(($maxwidth/$width),($maxheight/$height));
$newwidth = round($width*$factor);
$newheight = round($height*$factor);
}

  • You use imagecopyresampled to create the new image - this only works in specific GD-versions ("version 2"), try to use imagecopyresized otherwise.
纵性 2024-10-06 15:22:40

我似乎通过从头开始重新创建整个函数来解决这个问题。谢谢你们的意见。

问题是 PNG 没有被上传。当使用已上传的 URL 执行脚本时,它工作得很好。

再次感谢。

I seemed to have fixed the problem by recreating the whole function from scratch. Thank you guys for your input.

The problem was that PNG's weren't being uploaded. When executing the script with already uploaded URLs, it worked fine.

Thanks again.

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