Home  >  Article  >  Backend Development  >  php 如何提高上传图片的画质,没有锯齿?

php 如何提高上传图片的画质,没有锯齿?

WBOY
WBOYOriginal
2016-06-23 14:01:211381browse

如题,面对挑剔的设计师,说上传图片有锯齿(PS :我不太看得出来!= =)

请问高手们,在上传图片的时候如何提高画质呢,没有锯齿呢?


回复讨论(解决方案)

上传只是做拷贝,不会影响图片画质
当然,你有用 GD 操作了,就另当别论

上传只是做拷贝,不会影响图片画质
当然,你有用 GD 操作了,就另当别论

斑竹英明,就是用了GD函数了,做了等比缩放这些操作,您能帮忙指点指点吗?

代码如下

function resizeImg($from,$w=100,$h=100,$newfile){		  $info = getimagesize($from);		  switch ($info[2]){		  case 1:		  $im = imagecreatefromgif($from);			 break;			 case 2:		  $im = imagecreatefromjpeg($from);		  break;			 case 3:		  $im = imagecreatefrompng($from);		  break;			 default:		  	 //exit('不支持的图像格式');			 break;		 }		 $temp = pathinfo($from);		 $name = $temp["basename"];//文件名		 $dir = $temp["dirname"];//文件所在的文件夹		 $extension = $temp["extension"];//文件扩展名		 $width = $info[0];//获取图片宽度		 $height = $info[1];//获取图片高度		 $per1 = round($width/$height,2);//计算原图长宽比		 $per2 = round($w/$h,2);//计算缩略图长宽比		 //计算缩放比例		 if($per1>$per2||$per1==$per2)		 {			 //原图长宽比大于或者等于缩略图长宽比,则按照宽度优先			 $per=$w/$width;		 }		 if($per1<$per2)		 {			 //原图长宽比小于缩略图长宽比,则按照高度优先			 $per=$h/$height;		 }		 $temp_w = intval($width*$per);//计算原图缩放后的宽度		 $temp_h = intval($height*$per);//计算原图缩放后的高度		 $dst_im = imagecreatetruecolor($temp_w, $temp_h);		 //调整大小		 imagecopyresized($dst_im, $im, 0, 0, 0, 0, $temp_w, $temp_h, $width, $height);		 //输出缩小后的图像		 //exit($newfile);		 imagejpeg($dst_im,$newfile,100);		 imagedestroy($dst_im);		 imagedestroy($im);	}


还有一段代码如下

$file = $_FILES["photo"];		$filename=$file["tmp_name"]; 	$size=$file['size']; 	$type=$file['type'];    $max_size=5*1024*1024;	//echo $size;		if($type!="image/jpg"&&$type!="image/jpeg"&&$type!="image/png"&&$type!="image/gif")	{	  	echo"<script>alert('不支持的图像格式')</script>";		exit;	}	 	if($size>$max_size)	{	  	echo"<script>alert('图片尺寸超出上传限制')</script>";		exit;	}	 	$udid=guid();		$target_file="../photos/".$udid.".jpg";	copy($filename,$target_file);		/*	$info=getimagesize($target_file);    print_r($info);	*/	resizeImg($target_file,$nw,$nh,$target_file);		$ifn = $target_file;   //-----------原图像----	$ofn = $target_file;  //---剪切后保存的图像名----	$ext = strtoupper(end(explode('.',$ifn))); 	if(is_file($ifn) && ($ext == "JPG" || $ext == "JPEG")){ 		$source = imagecreatefromjpeg($ifn); 	}elseif(is_file($ifn) && $ext == "PNG"){ 		$source = imagecreatefromPNG($ifn); 	}elseif(is_file($ifn) && $ext == "GIF"){ 		$source = imagecreatefromGIF($ifn); 	} 	$sourceWidth  = imagesx($source); 	$sourceHeight = imagesy($source); 	$thumbWidth = $_POST['w']; 	$thumbHeight = $_POST['h']; 	$thumb = imagecreatetruecolor($thumbWidth, $thumbHeight); 	$x1 = $_POST['x1']; 	$y1 = $_POST['y1']; 	$x2 = $_POST['x2']; 	$y2 = $_POST['y2']; 	imagecopyresampled($thumb, $source,0,0,$x1,$y1,$thumbWidth,$thumbHeight,$thumbWidth,$thumbHeight); 	imagejpeg($thumb, $ofn);		resizeImg($target_file,100,100,$target_file);



您能帮忙看看问题在哪吗?因为对GD函数不熟悉,希望您能指导指导,谢谢

尝试启用抗锯齿功能(imageantialias)

不过你对位图做了缩放,丢失精度是必然的

尝试启用抗锯齿功能(imageantialias)

不过你对位图做了缩放,丢失精度是必然的

试了,好像没有效果呢!!!

所以说是“尝试”

好吧,继续尝试!

找到问题了,不过还是谢谢斑竹的指导



原来是这两个函数有关系,呵呵!以后得注意了

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn