php图片缩放类,php不失真图片缩放类,php高质量缩略图,php缩略图函数,php无损缩放,php图片缩放后模糊解决,图片缩略图质量低解决方法。在PHP网站开发过程中,如果你建立的网站涉及大量的图片处理,必然涉及到图片上传、缩放,而如何保持图片不失真,是很多初级PHP网站开发者比较头疼的一件事,今天未来往事就和大家分享一下如何进行等比例不失真图片缩放。
**首先我们来了解几个php函数:**
**imagepng():**http://cn2.php.net/manual/en/function.imagepng.php
注:函数的第三个参数的数值区间控制图片的质量,与imagejpeg()函数不同,imagepng()函数的区间值是0-9 (0表示质量最高,9表示质量最低)。PHP 5.1.2+支持。
**imagegif():**http://cn2.php.net/manual/en/function.imagegif.php
**imagejpeg():**http://cn2.php.net/manual/en/function.imagejpeg.php
**注:**函数的第三个参数控制图片的质量,区间0-100 (0表示质量最低,100表示质量最高. 默认75)
```php
srcimg = $img;
$this->resize_width = $wid;
$this->resize_height = $hei;
$this->cut = $cut;
//图片的类型
$this->type = substr(strrchr($this->srcimg,"."),1);
//初始化图象
$this->init_img();
//目标图象地址
$this -> dst_img($dst_img);
//--
$this->width = imagesx($this->im);
$this->height = imagesy($this->im);
//生成图象
$this->newimg();
ImageDestroy ($this->im);
}
//欢迎来访未来往事private function newimg()
{
//改变后的图象的比例
$resize_ratio = ($this->resize_width)/($this->resize_height);
//实际图象的比例
$ratio = ($this->width)/($this->height);
if(($this->cut)=="1")
//裁图
{
if($ratio>=$resize_ratio)
//高度优先
{
$newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height);
$this->quality_deal($newimg);
}
if($ratioresize_width,$this->resize_height);
imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width)/$resize_ratio));
$this->quality_deal($newimg);
}
}
else
//不裁图
{
if($ratio>=$resize_ratio)
{
$newimg = imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio);
imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this->width, $this->height);
$this->quality_deal($newimg);
}
if($ratioresize_height)*$ratio,$this->resize_height);
imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio, $this->resize_height, $this->width, $this->height);
$this->quality_deal($newimg);
}
}
}
//初始化图象 欢迎来访未来往事private function init_img()
{
if($this->type=="jpg")
{
$this->im = imagecreatefromjpeg($this->srcimg);
}
if($this->type=="gif")
{
$this->im = imagecreatefromgif($this->srcimg);
}
if($this->type=="png")
{
$this->im = imagecreatefrompng($this->srcimg);
}
}
//图象目标地址
private function dst_img($dst_img)
{
$full_length = strlen($this->srcimg);
$type_length = strlen($this->type);
$name_length = $full_length-$type_length;
//$name = substr($this->srcimg,0,$name_length-1);
//$this->dstimg = $name."_new.".$this->type;
$this->dstimg = $dst_img;
}
//缩略图质量处理 By:未来往事private function quality_deal($newimg){
$ext_type = strtolower($this->type);
if( $ext_type === 'jpg' || $ext_type === 'jpeg' ){
ImageJpeg ($newimg,$this->dstimg,100); //图片质量100-0
}elseif($ext_type === 'png'){
imagepng ($newimg,$this->dstimg,0); //图片质量0-9
}elseif($ext_type === 'gif'){
imagegif ($newimg,$this->dstimg);
}else{
ImageJpeg ($newimg,$this->dstimg);
}
}
}
//使用方法
$resizeimage = new resizeimage();
$resizeimage->process('./p1.jpg', '224', '126', '1', './p1_224x126.jpg');
```
最后更新于 2019-06-27 13:57:04 并被添加「php php基础 php函数 php类」标签,已有 3785 位童鞋阅读过。
本站使用「署名 4.0 国际」创作共享协议,可自由转载、引用,但需署名作者且注明文章出处
相关文章