中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

php生成圖片縮略圖類

2018-07-20    來源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用
 
<?php
/**
 * @abstract 生成圖片的縮略圖,可以指定任意尺寸,生成的圖片為png格式
 * @example
 * $file = 'test.png';
 * $th =new Thumbnail();
 * $th->GenerateThumbnail($file, 400, 500);
 *
 */
class Thumbnail{
     /**
     * @var string $from 源圖片
     */
     private $from;
     /**
     * @var string $name 縮略圖的文件名
     */
     private $name = '';
     /**
     * @var 原圖寬
     */
     private $rWidth;
     /**
     * @var 原圖高
     */
     private $rHeight;
     /**
     * @var 縮略圖寬
     */
     private $tWidth;
     /**
     * @var 縮略圖高
     */
     private $tHeight;
     /**
     * @var 實(shí)際縮放到的寬度
     */
     private $width;
     /**
     * @var 實(shí)際縮放到的高度
     */
     private $height;

     public function __construct(){
         try{
             if(!function_exists('gd_info')){
                 throw new Exception('Must GD extension is enabled');
             }
         }
         catch(Exception $e){
             $msg = 'class ' . __CLASS__ . ' Error:' . $e->getMessage();
             echo $msg;
             exit;
         }
     }
     /**
     * @var $from 原圖像
     * @var $width 生成的縮略圖的寬
     * @var $height 生成縮略圖的高
     * @var $name 生成的縮略圖的文件名,不帶后綴
     * @return string 生成的縮略圖
     */
     public function GenerateThumbnail($from, $width, $height, $name=''){
         try{
             if(!file_exists($from)){
                 throw new Exception('File does not exist');
             }
             if($width <= 0){
                 throw new Exception('The width is invalid');
             }
             if($height <= 0){
                 throw new Exception('The height is invalid');
             }
             $this->from = $from;
             $this->tWidth = $width;
             $this->tHeight = $height;
             if(!empty($name)){
                 $this->name = $name;
             }
             else{
                 $this->name = date('Ymd') . mt_rand(0, 9999);
             }
             $this->createThumbnail();
         }
         catch(Exception $e){
             $msg = 'class ' . __CLASS__ . ' Error:' . $e->getMessage();
             echo $msg;
             exit;
         }
     }

     public function getThumbnail(){
         return $this->name;
     }

     /**
     * 生成縮略圖文件
     */
     private function createThumbnail(){
         try{
             //讀取原始圖像信息
             $sourceInfo = getimagesize($this->from);
             $this->rWidth = $sourceInfo[0];
             $this->rHeight = $sourceInfo[1];
             //創(chuàng)建縮略圖圖像資源句柄
             $new_pic = imagecreatetruecolor($this->tWidth, $this->tHeight);
             //原圖繪制到縮略圖的x、y坐標(biāo)
             $x = 0;
             $y = 0;
             //創(chuàng)建原始圖像資源句柄
             $source_pic = '';
             switch ($sourceInfo[2]){
                 case 1: $source_pic = imagecreatefromgif($this->from); //gif
                         break;
                 case 2: $source_pic = imagecreatefromjpeg($this->from); //jpg
                         break;
                 case 3: $source_pic = imagecreatefrompng($this->from); //png
                         break;
                 default: throw new Exception('Does not support this type of image');
             }
             //計(jì)算縮放后圖像實(shí)際大小
             //原圖寬高均比縮略圖大
             if($this->rWidth > $this->tWidth && $this->rHeight > $this->tHeight){
                 $midw = ($this->rWidth - $this->tWidth) / $this->rWidth; //寬縮小的比例
                 $midh = ($this->rHeight - $this->tHeight) / $this->rHeight; //高縮小的比例
                 //那個(gè)縮小的比例大以那個(gè)為準(zhǔn)
                 if($midw > $midh){
                     $this->width = $this->tWidth;
                     $this->height = $this->rHeight - floor($this->rHeight * $midw);
                     $y = ($this->tHeight - $this->height) / 2;
                 }
                 else{
                     $this->width = $this->rWidth - floor($this->rWidth * $midh);
                     $this->height = $this->tHeight;
                     $x = ($this->tWidth - $this->width) / 2;
                 }
             }
             //原圖寬高均比縮略圖小
             elseif($this->rWidth < $this->tWidth && $this->rHeight < $this->tHeight){
                 $midw = ($this->tWidth - $this->rWidth) / $this->rWidth; //寬放大的比例
                 $midh = ($this->tHeight - $this->rHeight) / $this->rHeight; //高放大的比例
                 //那個(gè)放大的比例小以那個(gè)為準(zhǔn)
                 if($midw < $midh){
                     $this->width = $this->tWidth;
                     $this->height = $this->rHeight + floor($this->rHeight * $midw);
                     $y = ($this->tHeight - $this->height) / 2;
                 }
                 else{
                     $this->width = $this->rWidth + floor($this->rWidth * $midh);
                     $this->height = $this->tHeight;
                     $x = ($this->tWidth - $this->width) / 2;
                 }
             }
             //原圖寬小于縮略圖寬,原圖高大于縮略圖高
             elseif($this->rWidth < $this->tWidth && $this->rHeight > $this->tHeight){
                 $mid = ($this->rHeight - $this->tHeight) / $this->rHeight; //高縮小的比例
                 $this->width = $this->rWidth - floor($this->rWidth * $mid);
                 $this->height = $this->rHeight - floor($this->rHeight * $mid);
                 $x = ($this->tWidth - $this->width) / 2;
                 $y = ($this->tHeight - $this->height) / 2;
             }
             //原圖寬大于縮略圖寬,原圖高小于縮略圖高
             elseif($this->rWidth > $this->tWidth && $this->rHeight < $this->tHeight){
                 $mid = ($this->rWidth - $this->tWidth) / $this->rWidth; //寬縮小的比例
                 $this->width = $this->rWidth - floor($this->rWidth * $mid);
                 $this->height = $this->rHeight - floor($this->rHeight * $mid);
                 $x = ($this->tWidth - $this->width) / 2;
                 $y = ($this->tHeight - $this->height) / 2;
             }
             else{
                 throw new Exception('Resize error');
             }

             //給縮略圖添加白色背景
             $bg = imagecolorallocate($new_pic, 255, 255, 255);
             imagefill($new_pic, 0, 0, $bg);
             //縮小原始圖片到新建圖片
             imagecopyresampled($new_pic, $source_pic, $x, $y, 0, 0, $this->width, $this->height, $this->rWidth, $this->rHeight);
             //輸出縮略圖到文件
             imagepng($new_pic, $this->name.'.png');
             imagedestroy($new_pic);
             imagedestroy($source_pic);
         }
         catch(Exception $e){
             $msg = 'class ' . __CLASS__ . ' Error:' . $e->getMessage();
             echo $msg;
             exit;
         }
     }
}
  

標(biāo)簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:php中循環(huán)實(shí)現(xiàn)(字符串,對(duì)象,或者數(shù)組)編碼相互轉(zhuǎn)換

下一篇:PHP計(jì)算日期差異