gd - php图片旋转问题
程序员文章站
2022-05-15 12:08:14
...
$fullFileName = $uploadImgDir."/".$filename;
$thumbFileName = $filename.".thumb";
if ($type = pathinfo($fullFileName, PATHINFO_EXTENSION)) {
switch ($type) {
case 'jpg';
$isJpeg = true;
break;
case 'gif';
$isGif = true;
break;
case 'png';
$isPng = true;
break;
default:
return false;
}
// Load
if(isset($isGif) && $isGif){
$source = imagecreatefromgif($fullFileName);
$rotate = imagerotate($source, $degrees, 0);
imagegif($rotate,$fullFileName,100);
return true;
}elseif(isset($isJpeg) && $isJpeg){
$source = imagecreatefromjpeg($fullFileName);
$rotate = imagerotate($source, $degrees, 0);
imagejpeg($rotate,$fullFileName,100);
return true;
}elseif(isset($isPng) && $isPng){
//这个地方报错了
**$source = imagecreatefrompng($fullFileName);
$rotate = imagerotate($source, $degrees, 0);
imagepng($rotate,$fullFileName,100);**
return true;
}else{
return false;
}
jgp的旋转没问题,png 报错了
imagecreatefrompng(): gd-png: fatal libpng error: Read Error: truncated data
求解决
或者求png图片旋转的好方法
回复内容:
$fullFileName = $uploadImgDir."/".$filename;
$thumbFileName = $filename.".thumb";
if ($type = pathinfo($fullFileName, PATHINFO_EXTENSION)) {
switch ($type) {
case 'jpg';
$isJpeg = true;
break;
case 'gif';
$isGif = true;
break;
case 'png';
$isPng = true;
break;
default:
return false;
}
// Load
if(isset($isGif) && $isGif){
$source = imagecreatefromgif($fullFileName);
$rotate = imagerotate($source, $degrees, 0);
imagegif($rotate,$fullFileName,100);
return true;
}elseif(isset($isJpeg) && $isJpeg){
$source = imagecreatefromjpeg($fullFileName);
$rotate = imagerotate($source, $degrees, 0);
imagejpeg($rotate,$fullFileName,100);
return true;
}elseif(isset($isPng) && $isPng){
//这个地方报错了
**$source = imagecreatefrompng($fullFileName);
$rotate = imagerotate($source, $degrees, 0);
imagepng($rotate,$fullFileName,100);**
return true;
}else{
return false;
}
jgp的旋转没问题,png 报错了
imagecreatefrompng(): gd-png: fatal libpng error: Read Error: truncated data
求解决
或者求png图片旋转的好方法
imagepng($rotate,$fullFileName,100);的问题
把第三个参数去掉就行了。不知道具体原因。
第三个参数是保真度,png为什么不能100%保真。
可以参考 OSC链接
路由规则可以根据自己需求重写~~~~~~~~~~