淺談關(guān)于PHP解決圖片無損壓縮的問題
本文介紹了關(guān)于PHP解決圖片無損壓縮的問題,分享給大家,具體如下:
代碼如下:
header("Content-type: image/jpeg"); $file = "111.jpg"; $percent = 1.5; //圖片壓縮比 list($width, $height) = getimagesize($file); //獲取原圖尺寸 //縮放尺寸 $newwidth = $width * $percent; $newheight = $height * $percent; $src_im = imagecreatefromjpeg($file); $dst_im = imagecreatetruecolor($newwidth, $newheight); imagecopyresized($dst_im, $src_im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); imagejpeg($dst_im); //輸出壓縮后的圖片 imagedestroy($dst_im); imagedestroy($src_im);
我發(fā)現(xiàn)用php的imagecopyresized把大圖片縮成小圖片時(shí),圖片會(huì)變得很模糊,這時(shí)候要提升清晰度不如用 imagecopyresampled 代替 imagecopyresized也許會(huì)更好。
注:壓縮有損失是必然的,看的清楚與否實(shí)際上就是是否接受這個(gè)范圍的問題.比如你圖像上原圖有些點(diǎn)是2px,但是你壓縮5倍,那么這些點(diǎn)就會(huì)消失。
<?php /** * desription 壓縮圖片 * @param sting $imgsrc 圖片路徑 * @param string $imgdst 壓縮后保存路徑 */ function image_png_size_add($imgsrc,$imgdst){ list($width,$height,$type)=getimagesize($imgsrc); $new_width = ($width>600?600:$width)*0.9; $new_height =($height>600?600:$height)*0.9; switch($type){ case 1: $giftype=check_gifcartoon($imgsrc); if($giftype){ header('Content-Type:image/gif'); $image_wp=imagecreatetruecolor($new_width, $new_height); $image = imagecreatefromgif($imgsrc); imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); imagejpeg($image_wp, $imgdst,75); imagedestroy($image_wp); } break; case 2: header('Content-Type:image/jpeg'); $image_wp=imagecreatetruecolor($new_width, $new_height); $image = imagecreatefromjpeg($imgsrc); imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); imagejpeg($image_wp, $imgdst,75); imagedestroy($image_wp); break; case 3: header('Content-Type:image/png'); $image_wp=imagecreatetruecolor($new_width, $new_height); $image = imagecreatefrompng($imgsrc); imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); imagejpeg($image_wp, $imgdst,75); imagedestroy($image_wp); break; } } /** * desription 判斷是否gif動(dòng)畫 * @param sting $image_file圖片路徑 * @return boolean t 是 f 否 */ function check_gifcartoon($image_file){ $fp = fopen($image_file,'rb'); $image_head = fread($fp,1024); fclose($fp); return preg_match("/".chr(0x21).chr(0xff).chr(0x0b).'NETSCAPE2.0'."/",$image_head)?false:true; } ?>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- PHP圖像處理技術(shù)實(shí)例總結(jié)【繪圖、水印、驗(yàn)證碼、圖像壓縮】
- PHP圖片裁剪函數(shù)(保持圖像不變形)
- PHP圖像識(shí)別技術(shù)原理與實(shí)現(xiàn)
- PHP中繪制圖像的一些函數(shù)總結(jié)
- php圖像處理函數(shù)大全(推薦收藏)
- php使用imagick模塊實(shí)現(xiàn)圖片縮放、裁剪、壓縮示例
- 基于PHP實(shí)現(xiàn)等比壓縮圖片大小
- PHP添加圖片水印、壓縮、剪切的封裝類
- php高清晰度無損圖片壓縮功能的實(shí)現(xiàn)代碼
- PHP的圖像處理實(shí)例小結(jié)【文字水印、圖片水印、壓縮圖像等】
相關(guān)文章
php簡單的留言板與回復(fù)功能具體實(shí)現(xiàn)
留言板是在剛接觸php時(shí)用來學(xué)習(xí)的一個(gè)簡單的應(yīng)用例子了,今天我再給初學(xué)php的朋友提供一個(gè)完整的php留言板的全部制作過程,希望對(duì)你會(huì)有幫助2014-02-02Zend Framework框架的zend_cache緩存使用方法(zend框架)
這篇文章主要介紹了Zend_Cache文件緩存的基本操作,簡單的示例,,需要的朋友可以參考下2014-03-03ajax在joomla中的原生態(tài)應(yīng)用代碼
一般很少看到j(luò)oomla中使用ajax,筆者以前說到過用jquery來實(shí)現(xiàn),對(duì)于那些驗(yàn)證用戶名不能重復(fù),郵箱不能重復(fù),以及聯(lián)動(dòng)菜單,等等的應(yīng)用,使用ajax是免不了的2012-07-07thinkphp表單上傳文件并將文件路徑保存到數(shù)據(jù)庫中
這篇文章主要介紹了thinkphp表單上傳文件并將文件路徑保存到數(shù)據(jù)庫中的相關(guān)資料,需要的朋友可以參考下2016-07-07laravel 時(shí)間格式轉(zhuǎn)時(shí)間戳的例子
今天小編就為大家分享一篇laravel 時(shí)間格式轉(zhuǎn)時(shí)間戳的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10php使用Swoole實(shí)現(xiàn)毫秒級(jí)定時(shí)任務(wù)的方法
這篇文章主要介紹了php使用Swoole實(shí)現(xiàn)毫秒級(jí)定時(shí)任務(wù)的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09TP3.2.3框架使用CKeditor編輯器在頁面中上傳圖片的方法分析
這篇文章主要介紹了TP3.2.3框架使用CKeditor編輯器在頁面中上傳圖片的方法,結(jié)合實(shí)例形式分析了thinkPHP3.2.3框架使用CKeditor編輯器相關(guān)配置方法與操作注意事項(xiàng),需要的朋友可以參考下2019-12-12