php png 旋转后黑,使用PHP从PNG转换为JPEG图像时,图像全部变黑

.

我的代码在下面。这是对函数的调用:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值