本篇文章主要介紹php產生縮圖的方法,感興趣的朋友參考下,希望對大家有所協助。
PHP上傳圖片、產生縮圖是比較常用到的功能,一般我們會把這些功能寫一個類,方便我們調用。今天發現了一個比較簡單些的PHP產生縮圖函數的代碼,不敢獨享,特此與大家分享。自己使用時候認真看一下,代碼中都有注釋的。
本文執行個體講述了PHP簡單產生縮圖相簿的方法。具體如下:
<?php/* * written by mot * 根目錄下自己建立image thumb目錄 * */class thumb{ private $src; private $source; private $s_width; private $s_height; private $dest; private $d_height; private $d_width; private $name; public function thumb($image_path,$rate = 0.5){ $this->src = $image_path; $this->source = imagecreatefromjpeg($image_path); $s_size = getimagesize($image_path); $this->s_height = $s_size[1]; $this->s_width = $s_size[0]; $this->d_height = 100; $this->d_width = 100; $this->dest = imagecreate($this->d_width, $this->d_height); $this->name = explode('.jpg', $image_path); $this->name = $this->name[0]; } public function make(){ imagecopyresized($this->dest, $this->source, 0, 0, 0, 0, $this->d_width, $this->d_height, $this->s_width, $this->s_height); $thumb = str_replace('image', 'thumb', $this->name.'-thumb.jpg'); imagejpeg($this->dest,$thumb,100); $img = $thumb; echo "<a href=$this->src><img src=$img></a>"; }}$hl = opendir(".\\image\\");while(false != $file = readdir($hl)){ if($file == '.' || $file == '..') continue; $path = '.\\image\\'.$file; $tmp = new thumb($path,0.3); $tmp->make();}
總結:以上就是本篇文的全部內容,希望能對大家的學習有所協助。
相關推薦:
php可逆加密的方法及原理
PHP處理會話的10個函數
PHP如何獲得公眾平台關注使用者的基本資料