這篇文章主要介紹“基于GD2圖形庫的PHP生成圖片縮略圖類實(shí)例代碼”,在日常操作中,相信很多人在基于GD2圖形庫的PHP生成圖片縮略圖類實(shí)例代碼問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”基于GD2圖形庫的PHP生成圖片縮略圖類實(shí)例代碼”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
為且末等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及且末網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都做網(wǎng)站、網(wǎng)站建設(shè)、且末網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!1.使用方法
$resizeimage = new resizeimage("圖片源文件地址", "200", "100", "0","縮略圖地址"); //就只用上面的一句話,就能生成縮略圖,其中,源文件和縮略圖地址可以相同,200,100分別代表寬和高
2. 縮略圖類代碼
//使用如下類就可以生成圖片縮略圖, srcimg = $img; $this->resize_width = $wid; $this->resize_height = $hei; $this->cut = $c; //圖片的類型 $this->type = strtolower(substr(strrchr($this->srcimg,"."),1)); //初始化圖象 $this->initi_img(); //目標(biāo)圖象地址 $this -> dst_img($dstpath); //-- $this->width = imagesx($this->im); $this->height = imagesy($this->im); //生成圖象 $this->newimg(); ImageDestroy ($this->im); } function newimg() { //改變后的圖象的比例 $resize_ratio = ($this->resize_width)/($this->resize_height); //實(shí)際圖象的比例 $ratio = ($this->width)/($this->height); if(($this->cut)=="1") //裁圖 { if($ratio>=$resize_ratio) //高度優(yōu)先 { $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); ImageJpeg ($newimg,$this->dstimg); } if($ratio<$resize_ratio) //寬度優(yōu)先 { $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height); imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width)/$resize_ratio)); ImageJpeg ($newimg,$this->dstimg); } } 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); ImageJpeg ($newimg,$this->dstimg); } if($ratio<$resize_ratio) { $newimg = imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height); imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio, $this->resize_height, $this->width, $this->height); ImageJpeg ($newimg,$this->dstimg); } } } //初始化圖象 function initi_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); } } //圖象目標(biāo)地址 function dst_img($dstpath) { $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 = $dstpath; //echo $this->dstimg; } } ?>
到此,關(guān)于“基于GD2圖形庫的PHP生成圖片縮略圖類實(shí)例代碼”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!