我們經常會用到把上傳的大圖片壓縮,特別是體積,在微信等App應用上,也默認都是有壓縮的,那么,怎么樣對圖片大幅度壓縮卻仍能保持較高的清晰度呢?
壓縮通常是有按比例縮放,和指定寬度壓縮的,效果很不錯,一個數碼相機拍的4M圖片,壓縮后保持了較高的清晰度和原圖寬高值,只有700K。
下面是代碼(有兩個文件,imgcompress.class.php%20類,及compress.php)
1、創建一個imgcompress類,imgcompress.class.php
<?php
/**
*%20圖片壓縮類:通過縮放來壓縮。
*%20如果要保持源圖比例,把參數$percent保持為即可。
*%20即使原比例壓縮,也可大幅度縮小。數碼相機M圖片。也可以縮為KB左右。如果縮小比例,則體積會更小。
*
*%20結果:可保存、可直接顯示。
*/
class%20imgcompress{
private%20$src;
private%20$image;
private%20$imageinfo;
private%20$percent%20=%20.;
/**
*%20圖片壓縮
*%20@param%20$src%20源圖
*%20@param%20float%20$percent%20壓縮比例
*/
public%20function%20__construct($src,%20$percent=)
{
$this->src%20=%20$src;
$this->percent%20=%20$percent;
}
/**%20高清壓縮圖片
*%20@param%20string%20$saveName%20提供圖片名(可不帶擴展名,用源圖擴展名)用于保存。或不提供文件名直接顯示
*/
public%20function%20compressImg($saveName='')
{
$this->_openImage();
if(!empty($saveName))%20$this->_saveImage($saveName);%20//保存
else%20$this->_showImage();
}
/**
*%20內部:打開圖片
*/
private%20function%20_openImage()
{
list($width,%20$height,%20$type,%20$attr)%20=%20getimagesize($this->src);
$this->imageinfo%20=%20array(
'width'=>$width,
'height'=>$height,
'type'=>image_type_to_extension($type,false),
'attr'=>$attr
);
$fun%20=%20"imagecreatefrom".$this->imageinfo['type'];
$this->image%20=%20$fun($this->src);
$this->_thumpImage();
}
/**
*%20內部:操作圖片
*/
private%20function%20_thumpImage()
{
$new_width%20=%20$this->imageinfo['width']%20*%20$this->percent;
$new_height%20=%20$this->imageinfo['height']%20*%20$this->percent;
$image_thump%20=%20imagecreatetruecolor($new_width,$new_height);
//將原圖復制帶圖片載體上面,并且按照一定比例壓縮,極大的保持了清晰度
imagecopyresampled($image_thump,$this->image,,,,,$new_width,$new_height,$this->imageinfo['width'],$this->imageinfo['height']);
imagedestroy($this->image);
$this->image%20=%20$image_thump;
}
/**
*%20輸出圖片:保存圖片則用saveImage()
*/
private%20function%20_showImage()
{
header('Content-Type:%20image/'.$this->imageinfo['type']);
$funcs%20=%20"image".$this->imageinfo['type'];
$funcs($this->image);
}
/**
*%20保存圖片到硬盤:
*%20@param%20string%20$dstImgName%20、可指定字符串不帶后綴的名稱,使用源圖擴展名%20。、直接指定目標圖片名帶擴展名。
*/
private%20function%20_saveImage($dstImgName)
{
if(empty($dstImgName))%20return%20false;
$allowImgs%20=%20['.jpg', '.jpeg', '.png', '.bmp', '.wbmp','.gif']; //如果目標圖片名有后綴就用目標圖片擴展名 后綴,如果沒有,則用源圖的擴展名
$dstExt = strrchr($dstImgName ,".");
$sourseExt = strrchr($this->src ,".");
if(!empty($dstExt)) $dstExt =strtolower($dstExt);
if(!empty($sourseExt)) $sourseExt =strtolower($sourseExt);
//有指定目標名擴展名
if(!empty($dstExt) && in_array($dstExt,$allowImgs)){
$dstName = $dstImgName;
}elseif(!empty($sourseExt) && in_array($sourseExt,$allowImgs)){
$dstName = $dstImgName.$sourseExt;
}else{
$dstName = $dstImgName.$this->imageinfo['type'];
}
$funcs = "image".$this->imageinfo['type'];
$funcs($this->image,$dstName);
}
/**
* 銷毀圖片
*/
public function __destruct(){
imagedestroy($this->image);
}
}
2、使用,compress.php
<?php require_once 'imgcompress.class.php'; $source = 'test.png';//原圖文件名 $dst_img = 'test_.png';//保存圖片的文件名 $percent = ; #原圖壓縮,不縮放,但體積大大降低 $image = (new imgcompress($source,$percent))->compressImg($dst_img);
使用之后個人感覺 $percent 設置為0.5 左右就不錯了,壓縮后的圖片與原圖質量基本一樣。
以上就是php如何實現網站的圖片壓縮的詳細內容,希望對你有所幫助。歡迎關注我們,來獲取更多的資源。也可以點擊下方的鏈接到我們的官方網站來觀看視頻教程學習了。






