GDC


/**
 * GDC 
 * 我重新改寫的圖片應用函式
 * 有縮圖 有合併 有上字 基本的應用
 * 把一些常用的功能自動化 
 * 歡迎取用。
 * 
 * The MIT License (MIT)
 *
 * Copyright (c) 2016-7-6 henry
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * 
 */
class GDC 
{
    public $font = 'Font/PingFang.ttc';//基本字型

    function __construct()
    {

    }
    /**
     * [setfont 重新設定字型] 
     * @param  string $font [字型檔的位置]
     * @return [type]       
     */
    public function setfont($font)
    {
        $this->font = $font; //改變字型
    }

    /**
     * [GDinit 將圖片轉為變數]  
     * @param [string] $filepath [圖檔的路徑]
     * @return [type]  1. array: (變數 , 圖片類型) 2. 0: 為沒有檔案 3. -1: 格式不符
     */
    public function GDinit($filepath)
    {

        // $imageTypeArray = array (0=>'UNKNOWN', 1=>'GIF', 2=>'JPEG', 3=>'PNG', 4=>'SWF', 5=>'PSD', 6=>'BMP', 7=>'TIFF_II', 8=>'TIFF_MM', 9=>'JPC', 10=>'JP2', 11=>'JPX', 12=>'JB2', 13=>'SWC', 14=>'IFF', 15=>'WBMP', 16=>'XBM', 17=>'ICO', 18=>'COUNT');
        $type=exif_imagetype($filepath); //取得圖檔類型 參照上方

        if (!file_exists($filepath)) {
            // throw new InvalidArgumentException('File "'.$filepath.'" not found.');
            return 0 ;
            break;
        }

        switch ($type) {  
            case '3':  //PNG
                return array(imagecreatefrompng($filepath),'png');
                break;  
            case '2':  //JPEG
                return array(imagecreatefromjpeg($filepath),'jpg');
                break;  
            case '1':  //GIF
                return array(imagecreatefromgif($filepath),'gif');
                break;  
            default:    //false or 其他
                return -1 ;
            break;
        }

    }

    /**
     * [GDresize 等比例縮小]
     * @param [type] $source   已經被轉換的檔
     * @param [type] $width    原寬度
     * @param [type] $height   原高度
     * @param [type] $max      限制最寬跟最高
     * @return $thumb 
     * PS:
     * list($w, $h) = getimagesize(檔案路徑);
     * 或
     * imagesx(圖檔變數) = 寬 & imagesy(圖檔變數) = 高
     */
    public function GDresize($source, $width, $height, $max){

        // Get new sizes
        // 最高最寬不超過 $max 依等比縮小
        if($width>$max){
            // resize
            $newwidth = $max;
            $percent = $max / $width;
            $newheight = $height * $percent;
        }else if($height>$max){
            // resize
            $newheight = $max;
            $percent = $max / $height;
            $newwidth = $width * $percent;
        }else if($height>=$width){
            $newheight = $max;
            $percent = $max / $height;
            $newwidth = $width * $percent;
        }else if($height<$width){
            $newwidth = $max;
            $percent = $max / $width;
            $newheight = $height * $percent;            
        }

        // $newwidth = $width * $percent;
        // $newheight = $height * $percent;
        // echo $newwidth, $newheight;
        // 創造一個真色彩圖檔 黑色底
        $thumb = imagecreatetruecolor($newwidth, $newheight);

        // Resize 更改原始圖檔大小 ()
        /**
         * @param  [type] $thumb    [description]
         * @param  [type] $source   [description]
         * @param  [type] $newwidth [description]
         * @param  [type] $newheight[description]
         * @param  [type] $width    [description]
         * @param  [type] $height   [description]
         */
        imagecopyResampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
        // imagecopyResampled 
        // imagecopyresized // 其他函式方法

        return $thumb;
    }



    /**
     * 計算文字寬度,自動換行 (文字高度的有待研究)
     * @param  [type] $fontsize [description]
     * @param  [type] $angle    [description]
     * @param  [type] $fontface [description]
     * @param  [type] $string   [description]
     * @param  [type] $width    [description]
     * @return [type]           [description]
     */
    public function autowrap($fontsize, $angle, $string, $width ,$i=2) {
    // 字體大小 角度 字串 寬度或高度 (1=高度 2=寬度)
     $content = "";

     // 將字符傳分拆成一個個chars,再放入letter中
     for ($i=0;$i<mb_strlen($string);$i++) {
      $letter[] = mb_substr($string, $i, 1);
     }

     foreach ($letter as $l) {
      $teststr = $content." ".$l;
      $testbox = imagettfbbox($fontsize, $angle, $this->font, $teststr);
      // 判斷串接串後的字串是否超過預設的寬度(or高度)
      if (($testbox[$i] > $width) && ($content !== "")) {
       $content .= "\n";
      }
      $content .= $l;
     }
     return $content;
    }

    /**
     * [GDftext description]
     * @param [type]  $Image    [圖檔變數]
     * @param string  $text     [添加文字]
     * @param integer $fontsize [文字大小]
     * @param integer $x        [文字 X座標]
     * @param integer $y        [文字 Y座標]
     * @param integer $red      [文字顏色(紅) 0-255]
     * @param integer $green    [文字顏色(綠) 0-255]
     * @param integer $blue     [文字顏色(藍) 0-255]
     * @param integer $angle    [文字角度]
     * @return Image [圖檔變數]
     */
    public function GDftext($Image,$text,$fontsize=20,$x=0,$y=0,$red=110,$green=110,$blue=110,$angle=0)
    {
        // 給文字的字體顏色 通常是黑色 
        // imagecolorexactalpha ( resource $image , int $red , int $green , int $blue , int $alpha )
        $newImageSmallTextColor = imagecolorallocate($Image,$red,$green,$blue);

        // fontsize 為字體大小
        // angle 為字型的角度,順時鐘計算,0 度為水平,也就是三點鐘的方向 (由左到右),90 度為上下顛倒
        // x,y 為文字的坐標值 (原點為左上角);
        imagettftext($Image, $fontsize, $angle, $x, $y, $newImageSmallTextColor, $this->font , $text);  //塞文字
        // return $Image; //不用回傳
    }

    /**
     * [GDrotate description] 網路上神人寫的 把那些會莫名其妙顛倒的圖片轉正 (應該是從exif資料去判斷的)
     * @param [type] $filename 圖檔路徑
     * @param [type] $thumb    圖檔變數 (使用GDinit)
     */
    public function GDrotate($filepath,$thumb)
    {
        $exif = exif_read_data($filepath);

        if (!empty($exif['Orientation'])) {
            switch ($exif['Orientation']) {
            case 3:
            $thumb = imagerotate($thumb, 180, 0);
            break;
            case 6:
            $thumb = imagerotate($thumb, -90, 0);
            break;
            case 8:
            $thumb = imagerotate($thumb, 90, 0);
            break;
            }
        }

        return $thumb;
    }
//==================================================================
//  合併圖檔
//==================================================================

    // imagecopy的 - 拷貝圖像的一部分
    // imagecopymerge - 拷貝並合併圖像的一部分
    // imagecopymergegray - 用灰度拷貝並合併圖像的一部分
    // imagecopyresampled - 重採樣拷貝部分圖像並調整大小
    // imagecopyresized - 拷貝部分圖像並調整大小

    /**
     * [合併圖不斷增高]
     * @param  [type] $array [圖檔變數]
     * @param  [type] $width [統一的固定寬 自行取最大值 可使用GDmax]
     * @return [type]        [description]
     */
    public function GDmarge_height($array,$width)
    {
        $gap=10; // gap
        $cont_height=0+$gap; 
        $height=0+$gap;
        foreach ($array as $h) {
            $cont_height+=imagesy($h);
            $cont_height+=$gap;
        }
        $width+=$gap*2;
        $imageResource = imagecreatetruecolor($width, $cont_height); 

        foreach ($array as $k=> $image) {
            imagecopy($imageResource, $image, $gap, $height, 0, 0, $width, $cont_height); 
            $height=$height+imagesy($image)+$gap; //計算在照片上方有多高了 要移動到這高度才輸出照片 一開始只有間隔
        }
        return $imageResource;
    }

    /**
     * 合併圖不斷加寬
     * @param  [type] $array  [圖檔變數]
     * @param  [type] $height [統一的固定高 自行取最大值 可使用GDmax]
     * @return [type]         [description]
     */
    public function GDmarge_width($array,$height)
    {         
        $gap=10; // gap
        $cont_width=0+$gap; // 最左邊的空格
        $width=0+$gap;
        foreach ($array as $w) {
            $cont_width+=imagesx($w);
            $cont_width+=$gap;
        }
        $height+=$gap*2;
        $imageResource = imagecreatetruecolor($cont_width,$height); 

        foreach ($array as $k=> $image) {
            imagecopy($imageResource, $image,  $width ,$gap, 0, 0, $cont_width,$height);
            $width=$width+imagesx($image)+$gap;
        }
        return $imageResource;
    }
    /**
     * [GDmax description]
     * @param [type]  $array [圖檔變數]
     * @param integer $type  [1=取最高 其他=取最寬]
     */
    public function GDmax($array,$type=1)
    {   
        $max = 0;
        foreach ($array as $i) {
            if($type===1){$l=imagesx($i);}else{$l=imagesy($i);}
            $max=($l>=$max)?$l:$max;
        }
        return $max;
    }

//==================================================================

    /**
     * Output and free memory 存檔
     * @param  [type] $thumb    圖檔變數
     * @param  [type] $savepath 儲存位址 (不存也可以)
     * @param  [type] $quality  質量(0-100)
     * @return [type] true or false
     * 加上檔頭就會
     * header('Content-Type: image/jpeg');
     */
    public function GDoutput($thumb,$savepath=NULL,$quality=70)
    {
        return imagejpeg($thumb,$savepath,$quality); 

        imagedestroy($thumb);
    }

}

results matching ""

    No results matching ""