<?php
class 圖形 {
    // 顯示圖形
    public static function &顯示圖形(&$檔名, $寬度, $高度) {
        $參數 = getimagesize($檔名);
        if (false == $參數) {
            return '';
        }
        $參數 = self::比例參數($參數[0], $參數[1], $寬度, $高度);
        return sprintf('<img src=%s width=%s height=%s>',
            $檔名, $參數['寬度'], $參數['高度']
        );
    }

    // 傳回比例調整參數
    public static function &比例參數($來源寬度, $來源高度, $目的寬度, $目的高度) {
        $輸出 = array();
        $寬度比例 = $目的寬度 / $來源寬度;
        $高度比例 = $目的高度 / $來源高度;
        
        if (1 <= $寬度比例 && 1 <= $高度比例) {
            $輸出['寬度'] = $來源寬度;
            $輸出['高度'] = $來源高度;
        } else if ($寬度比例 > $高度比例) {
            $輸出['寬度'] = ceil($高度比例 * $來源寬度);
            $輸出['高度'] = $目的高度;
        } else {
            $輸出['寬度'] = $目的寬度;
            $輸出['高度'] = ceil($寬度比例 * $來源高度);
        }
        return $輸出;
    }

    // 調整圖檔型態字串
    public static function &圖檔型態(&$檔名, $型態 = null) {
        if (null === $型態) {
            $型態 = strtolower(pathinfo($檔名, PATHINFO_EXTENSION));
        } else {
            $型態 = strtolower($型態);
        }
        if ('jpg' == $型態) {
            $型態 = 'jpeg';
        }
        return $型態;
    }

    // 載入圖檔
    public static function &載入圖檔(&$檔名, $型態 = null) {
        $函數 = 'imagecreatefrom' . self::圖檔型態($檔名, $型態);
        return $函數($檔名);
    }

    // 寫入圖檔
    public static function 寫入圖檔(&$圖形, &$檔名, $型態 = null) {
        $函數 = 'image' . self::圖檔型態($檔名, $型態);
        $目錄 = dirname(realpath($檔名));
        if (false == is_dir($目錄)) {
            @mkdir($目錄, 0777, true);
        }
        return $函數($圖形, $檔名);
    }

    // 縮圖
    public static function 縮圖(&$來源檔名, &$目的檔名, $來源型態 = null, $目的型態 = null, $目的寬度 = 1, $目的高度 = 1) {
        $來源圖形 = self::載入圖檔($來源檔名, $來源型態);
        if ('' == $來源圖形) {
            return false;
        }
        $參數 = self::比例參數(imagesx($來源圖形), imagesy($來源圖形), $目的寬度, $目的高度);
        $目的圖形 = imagecreatetruecolor($參數['寬度'], $參數['高度']);
        imagecopyresampled($目的圖形, $來源圖形, 0, 0, 0, 0,
            $參數['寬度'], $參數['高度'], $來源寬度, $來源高度
        );
        $輸出 = self::寫入圖檔($目的圖形, $目的檔名, $目的型態);
        imagedestroy($目的圖形);
        imagedestroy($來源圖形);
        return $輸出;
    }
}
?>
arrow
arrow
    全站熱搜

    HACGIS 發表在 痞客邦 留言(0) 人氣()