php 縮圖類(附調用樣本)

來源:互聯網
上載者:User
本文介紹下,php實現的一個縮圖類,支援載入圖片檔案與載入圖片字串,按比例展開等,代碼後面有調用樣本供參考。

分享個php縮圖類,可以實現如下的功能:

1,支援載入圖片檔案和載入圖片字串2,可以將縮圖輸出到瀏覽器和保持縮圖檔案3,支援gif,png,jpeg類型的縮減4,可以設定是否按比例來展開

代碼如下:

maxWidth = $maxWidth;                $this->maxHeight = $maxHeight;                $this->scale = $scale;                $this->inflate = $inflate;                $this->types = array(                    'image/jpeg',                    'image/png',                    'image/gif'                );                //載入MIME類型映像的函數名稱                $this->imgLoaders = array(                    'image/jpeg'        =>      'imagecreatefromjpeg',                    'image/png'         =>      'imagecreatefrompng',                    'image/gif'         =>      'imagecreatefromgif'                );                //儲存建立MIME類型圖片的函數名稱                $this->imgCreators = array(                    'image/jpeg'        =>      'imagejpeg',                    'image/png'         =>      'imagepng',                    'image/gif'         =>      'imagegif'                );                   }        /**         * 檔案方式載入圖片         * @param       string  $image 源圖片         * @return      bool             */        public function loadFile($image){                if(!$dims = @getimagesize($image)){                        trigger_error("源圖片不存在");                }                if(in_array($dims['mime'], $this->types)){                        $loader = $this->imgLoaders[$dims['mime']];                        $this->source = $loader($image);                        $this->sourceWidth = $dims[0];                        $this->sourceHeight = $dims[1];                        $this->sourceMime = $dims['mime'];                        $this->initThumb();                        return TRUE;                }else{                        trigger_error('不支援'.$dims['mime']."圖片類型");                }        }        /**         * 字串方式載入圖片         * @param       string $image  字串         * @param       string $mime    圖片類型         * @return type          */        public function loadData($image,$mime){                if(in_array($mime, $this->types)){                        if($this->source = @imagecreatefromstring($image)){                                $this->sourceWidth = imagesx($this->source);                                $this->sourceHeight = imagesy($this->source);                                $this->sourceMime = $mime;                                $this->initThumb();                                return TRUE;                        }else{                                trigger_error("不能從字串載入圖片");                        }                }else{                        trigger_error("不支援".$mime."圖片格式");                }        }        /**         * 產生縮圖         * @param       string  $file   檔案名稱。如果不為空白則儲存為檔案,否則直接輸出到瀏覽器         */        public function buildThumb($file = null){                $creator = $this->imgCreators[$this->sourceMime];                if(isset($file)){                        return $creator($this->thumb,$file);                }else{                        return $creator($this->thumb);                }        }        /**         * 處理縮放         */        public function initThumb(){                if($this->scale){                        if($this->sourceWidth > $this->sourceHeight){                                $this->thumbWidth = $this->maxWidth;                                $this->thumbHeight = floor($this->sourceHeight*($this->maxWidth/$this->sourceWidth));                        }elseif($this->sourceWidth < $this->sourceHeight){                                $this->thumbHeight = $this->maxHeight;                                $this->thumbWidth = floor($this->sourceWidth*($this->maxHeight/$this->sourceHeight));                        }else{                                $this->thumbWidth = $this->maxWidth;                                $this->thumbHeight = $this->maxHeight;                        }                }                $this->thumb = imagecreatetruecolor($this->thumbWidth, $this->thumbHeight);                                if($this->sourceWidth <= $this->maxWidth && $this->sourceHeight <= $this->maxHeight && $this->inflate == FALSE){                        $this->thumb = $this->source;                }else{                        imagecopyresampled($this->thumb, $this->source, 0, 0, 0, 0, $this->thumbWidth, $this->thumbHeight, $this->sourceWidth, $this->sourceHeight);                }        }                public function getMine(){                return $this->sourceMime;        }                public function getThumbWidth(){                return $this->thumbWidth;        }                public function getThumbHeight(){                return $this->thumbHeight;        }}/** * 縮圖類調用樣本(檔案) */$thumb = new Thumbnail(200, 200);$thumb->loadFile('wap.gif');header('Content-Type:'.$thumb->getMine());$thumb->buildThumb();/** * 縮圖類調用樣本(字串) */$thumb = new Thumbnail(200, 200);$image = file_get_contents('wap.gif');$thumb->loadData($image, 'image/jpeg');$thumb->buildThumb('wap_thumb.gif');?>
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.