为什么我在PHP中调整大小的图像会失真?
我正在通过PHP调整图像的大小,但是它们被扭曲了。下面是我正在使用的代码 我编辑了80分的质量分数,但这并没有以任何方式改变质量 也许有更好的方法,任何建议都会有帮助为什么我在PHP中调整大小的图像会失真?,php,image,resize,photos,Php,Image,Resize,Photos,我正在通过PHP调整图像的大小,但是它们被扭曲了。下面是我正在使用的代码 我编辑了80分的质量分数,但这并没有以任何方式改变质量 也许有更好的方法,任何建议都会有帮助 // Image resize function with php + gd2 lib function imageresize($source, $destination, $width = 0, $height = 0, $crop = false, $quality = 80) { $quality = $quali
// Image resize function with php + gd2 lib
function imageresize($source, $destination, $width = 0, $height = 0, $crop = false, $quality = 80) {
$quality = $quality ? $quality : 80;
$image = imagecreatefromstring($source);
if ($image) {
// Get dimensions
$w = imagesx($image);
$h = imagesy($image);
//die(json_encode(array('width' => $w, 'height' => $h)));
if (($width && $w > $width) || ($height && $h > $height)) {
$ratio = $w / $h;
if (($ratio >= 1 || $height == 0) && $width && !$crop) {
$new_height = $width / $ratio;
$new_width = $width;
} elseif ($crop && $ratio <= ($width / $height)) {
$new_height = $width / $ratio;
$new_width = $width;
} else {
$new_width = $height * $ratio;
$new_height = $height;
}
} else {
$new_width = $w;
$new_height = $h;
}
$x_mid = $new_width * .5; //horizontal middle
$y_mid = $new_height * .5; //vertical middle
// Resample
error_log('height: ' . $new_height . ' - width: ' . $new_width);
$new = imagecreatetruecolor(floor($new_width), floor($new_height));
$x = 0;
if ($new_width > $new_height) {
//$new_height = $new_height *8;
} else {
//$x = -$new_width * 7;
//$new_width = $new_width *8;
}
imagecopyresampled($new, $image, 0, 0, $x, 0, $new_width, $new_height, $w, $h);
// Crop
if ($crop) {
$crop = imagecreatetruecolor($width ? $width : $new_width, $height ? $height : $new_height);
imagecopyresampled($crop, $new, 0, 0, ($x_mid - ($width * .5)), 0, $width, $height, $width, $height);
//($y_mid - ($height * .5))
}
// Output
// Enable interlancing [for progressive JPEG]
imageinterlace($crop ? $crop : $new, true);
$dext = strtolower(pathinfo($destination, PATHINFO_EXTENSION));
if ($dext == '') {
$dext = $ext;
$destination .= '.' . $ext;
}
switch ($dext) {
case 'jpeg':
case 'jpg':
imagejpeg($crop ? $crop : $new, $destination, $quality);
break;
case 'png':
$pngQuality = ($quality - 100) / 11.111111;
$pngQuality = round(abs($pngQuality));
imagepng($crop ? $crop : $new, $destination, $pngQuality);
break;
case 'gif':
imagegif($crop ? $crop : $new, $destination);
break;
}
@imagedestroy($image);
@imagedestroy($new);
@imagedestroy($crop);
}
//使用php+gd2库调整图像大小的函数
函数imageresize($source、$destination、$width=0、$height=0、$crop=false、$quality=80){
$quality=$quality?$quality:80;
$image=imagecreatefromstring($source);
如果($image){
//获取维度
$w=imagesx($image);
$h=imagesy($image);
//模具(json_编码(数组('width'=>w,'height'=>h));
如果($width&&$w>$width)| |($height&&$h>$height)){
$ratio=$w/$h;
如果(($ratio>=1 | |$height==0)&&&$width&&!$crop){
$new_高度=$width/$ratio;
$new_width=$width;
}elseif($作物和$比率$新高度){
//$new_height=$new_height*8;
}否则{
//$x=-$new_宽度*7;
//$new_width=$new_width*8;
}
imagecopyresampled($new,$image,0,0,$x,0,$new\u width,$new\u height,$w,$h);
//收成
如果(作物){
$crop=IMAGECreateTureColor($width?$width:$new\u width,$height?$height:$new\u height);
imagecopyresampled($crop,$new,0,0,($x_mid-($width*.5)),0,$width,$height,$width,$height);
//($y_mid-($height*.5))
}
//输出
//启用隔行扫描[用于渐进式JPEG]
imageinterlace($crop?$crop:$new,true);
$dext=strtolower(路径信息($destination,路径信息扩展));
如果($dext=''){
$dext=$ext;
$destination.='..$ext;
}
交换机($dext){
案例“jpeg”:
案件‘jpg’:
图像JPEG($crop?$crop:$new,$destination,$quality);
打破
案例“png”:
$pngQuality=($quality-100)/11.111111;
$pngQuality=圆形(abs($pngQuality));
imagepng($crop?$crop:$new,$destination,$pngQuality);
打破
案例“gif”:
imagegif($crop?$crop:$new,$destination);
打破
}
@图像销毁($图像);
@(新的);
@图像处理(作物);
}
是否有前后质量的示例?如果原始源文件的质量和/或分辨率较低,则最终会生成低质量的图像。请使用更高分辨率的源文件。代码看起来正确,请在输出图像之前检查质量变量,可能您传递的质量非常低。请尝试删除调用imageinterlace()并对其进行测试,可能此调用会降低图像质量。然后,代码中出现了一个小问题,您在混合变量。开头的$crop变量是bool(函数参数),然后您将其覆盖为图像,最后您将使用imagedestroy对其进行销毁。因此,如果有人向您传递false,您将使用false调用imagedestroy()。您的起始图像是什么格式的?Gif?Jpg?Png?我最成功地将大型Jpg文件作为原始文件。您如何查看图像?在html页面中还是在磁盘上?