php缩小png质量变差_PHP高质量不失真等比例图片缩放类

本文介绍了如何使用PHP进行等比例不失真图片缩放,特别是针对PNG图片,通过调整imagepng()函数的参数来控制质量。文中提供了一个PHP类,用于创建高质量的缩略图,并详细解释了相关PHP图像处理函数的用法,如imagepng()、imagejpeg()和imagegif()。此外,还分享了如何解决图片缩放后质量降低和模糊的问题。
摘要由CSDN通过智能技术生成

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 国际」创作共享协议,可自由转载、引用,但需署名作者且注明文章出处

相关文章

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值