2019-03-26 16:41:07 2467瀏覽
有喜歡PHP開發(fā)技術(shù)的小伙伴通過扣丁學(xué)堂官網(wǎng)咨詢扣丁學(xué)堂的PHP培訓(xùn)老師關(guān)于PHP如何進(jìn)行圖像壓縮的問題,現(xiàn)在小編整理出來分享給大家,對PHP如何進(jìn)行圖像壓縮有疑問的小伙伴來了解一下吧,希望對小伙伴有所幫助。
圖像壓縮:
對圖像進(jìn)行壓
縮處理非常簡單,因?yàn)榫鸵粋€函數(shù)
參數(shù)1:目標(biāo)圖像資源(畫布)
參數(shù)2:等待壓縮圖像資源
參數(shù)3:目標(biāo)點(diǎn)的x坐標(biāo)
參數(shù)4:目標(biāo)點(diǎn)的y坐標(biāo)
參數(shù)5:原圖的x坐標(biāo)
參數(shù)6:原圖的y坐標(biāo)
參數(shù)7:目的地寬度(畫布寬)
參數(shù)8:目的地高度(畫布高)
參數(shù)9:原圖寬度
參數(shù)10:原圖高度
imagecopyresampled($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)
封裝的圖像壓縮類
<?php /* * 圖像壓縮處理類 */ class Thumb { private $_filename; //等待壓縮的圖像 private $_thumb_path = 'thumb/'; //壓縮圖像的保存目錄 public function __set($p,$v) { if(property_exists($this,$p)){ $this -> $p = $v; } } //構(gòu)造方法初始化需要壓縮的圖像 public function __construct($file) { if(!file_exists($file)){ echo '文件有誤,不能壓縮'; return; } $this -> _filename = $file; } //圖像壓縮處理 function makeThumb($area_w,$area_h) { $src_image = imagecreatefrompng($this->_filename); $res = getimagesize($this->_filename); echo '<pre>'; var_dump($res); die; $dst_x = 0; $dst_y = 0; $src_x = 0; $src_y = 0; //原圖的寬度、高度 $src_w = imagesx($src_image); //獲得圖像資源的寬度 $src_h = imagesy($src_image); //獲得圖像資源的高度 if($src_w / $area_w < $src_h/$area_h){ $scale = $src_h/$area_h; } if($src_w / $area_w >= $src_h/$area_h){ $scale = $src_w / $area_w; } $dst_w = (int)($src_w / $scale); $dst_h = (int)($src_h / $scale); $dst_image = imagecreatetruecolor($dst_w,$dst_h); $color = imagecolorallocate($dst_image,255,255,255); //將白色設(shè)置為透明色 imagecolortransparent($dst_image,$color); imagefill($dst_image,0,0,$color); imagecopyresampled($dst_image,$src_image,$dst_x,$dst_y,$src_x,$src_y,$dst_w,$dst_h,$src_w,$src_h); //可以在瀏覽器直接顯示 //header("Content-Type:image/png"); //imagepng($dst_image); //分目錄保存壓縮的圖像 $sub_path = date('Ymd').'/'; //規(guī)范:上傳的圖像保存到upload目錄,壓縮的圖像保存到thumb目錄 if(!is_dir($this -> _thumb_path . $sub_path)){ mkdir($this -> _thumb_path . $sub_path,0777,true); } $filename = $this -> _thumb_path . $sub_path.'thumb_'.$this->_filename; //也可以另存為一個新的圖像 imagepng($dst_image,$filename); return $filename; } } $thumb = new Thumb('upload.jpg'); $thumb -> _thumb_path = 'static/thumb/'; $file = $thumb -> makeThumb(100,50); // var_dump($file);
最后想要了解更多關(guān)于PHP開發(fā)方面內(nèi)容的小伙伴,請關(guān)注扣丁學(xué)堂PHP培訓(xùn)官網(wǎng)、微信等平臺,扣丁學(xué)堂IT職業(yè)在線學(xué)習(xí)教育平臺為您提供權(quán)威的PHP開發(fā)視頻,PHP培訓(xùn)后的前景無限,行業(yè)薪資和未來的發(fā)展會越來越好的,扣丁學(xué)堂老師精心推出的PHP視頻教程定能讓你快速掌握PHP從入門到精通開發(fā)實(shí)戰(zhàn)技能。扣丁學(xué)堂PHP技術(shù)交流群:374332265。
【關(guān)注微信公眾號獲取更多學(xué)習(xí)資料】 【掃碼進(jìn)入前端H5架構(gòu)師進(jìn)階VIP體驗(yàn)課】
查看更多關(guān)于“php培訓(xùn)資訊”的相關(guān)文章>>