问题:将任何PNG图像转换为JPEG图像时,图像全部变为黑色。JPEG图像使用PHP转换PNG图像时转为全黑色
首先,我搜索了互联网和stackoverflow,以了解如何做到这一点。我已经尝试了所有可以在PHP手册和堆栈溢出中找到的方法。问题依然存在。我使用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
在MIME检测中添加最后的else语句: 'else die(“unknown format”);' – jmz 2010-10-26 07:55:04
你能举一个例子PNG吗?你是否尝试过来自不同来源的各种PNG? – 2010-10-26 08:20:59
每次我尝试从Google获取新的.PNG图像时。 顶部框是图像结果:http://twitpic.com/30ywf5 – stwhite 2010-10-26 08:27:15