php生成圖片縮略圖類

n6bg 9年前發布 | 1K 次閱讀 PHP

 
<?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 實際縮放到的寬度 */ private $width; /**
    • @var 實際縮放到的高度 */ 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];
        //創建縮略圖圖像資源句柄
        $new_pic = imagecreatetruecolor($this->tWidth, $this->tHeight);
        //原圖繪制到縮略圖的x、y坐標
        $x = 0;
        $y = 0;
        //創建原始圖像資源句柄
        $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');
        }
        //計算縮放后圖像實際大小
        //原圖寬高均比縮略圖大
        if($this->rWidth > $this->tWidth && $this->rHeight > $this->tHeight){
            $midw = ($this->rWidth - $this->tWidth) / $this->rWidth; //寬縮小的比例
            $midh = ($this->rHeight - $this->tHeight) / $this->rHeight; //高縮小的比例
            //那個縮小的比例大以那個為準
            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; //高放大的比例
            //那個放大的比例小以那個為準
            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;
      

      } } }

</pre>

 本文由用戶 n6bg 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!