平成30/ 2018-10-23 11:16
php/ image トリミング リサイズ Ver2
(PNG, JPEG, JPGに対応させた。Ver1はJPGのみなので。)

# 改造した
1. 高さを指定しない場合、デフォルトの高さをリサイズしないようにする。
tr100x.jpg
tr100x0.jpg
##

# Allowed Memoryエラーでた場合、以下で動くかも
ini_set("memory_limit", "200M");
##



・使い方
元の画像
<a href="/samples/20110524images/sIMG_0257.jpg">
元の画像</a>


tr100x100.jpg / 幅100px高さ100pxにトリミングしてリサイズ
<a href="/samples/20110524images/sIMG_0257_tr100x100.jpg">
tr100x100.jpg / 幅100px高さ100pxにトリミングしてリサイズ</a>


t200x300 / 幅200px高さ300pxに収まり縦横比を維持してリサイズ
<a href="/samples/20110524images/sIMG_0257_r200x300.jpg">
t200x300 / 幅200px高さ300pxに収まり縦横比を維持してリサイズ</a>


r4x3.jpg / 横縦 4:3の比率で画像中心を基準にトリミング
<a href="/samples/20110524images/sIMG_0257_t4x3.jpg">
r4x3.jpg / 横縦 4:3の比率で画像中心を基準にトリミング</a>


//-----img.php
<?php
/***************************************************************

        ※ この範囲のコメント部分は削除禁止です。

        (c)2011 篠原隆司

        http://aulta.jp/
        skype : aulta79
        facebook : https://www.facebook.com/aulta
        twitter : http://twitter.com/#!/aulta

        web系、システム系でお仕事が余っている方はご連絡頂けるとうれしいです!

***************************************************************/

$thisFile = __FILE__;

if (isset($_GET['Command'])){
        $s = $_GET['Command'];
        if (
        preg_match('/^(.+)_([rt]{1,2})([0-9]+)x([0-9]+)(\.jpeg|\.jpg)$/ui', $s, $m) or
        preg_match('/^(.+)_([rt]{1,2})([0-9]+)x([0-9]+)(\.png)$/ui', $s, $m)
        ){
                $f = dirname($thisFile) . '/' . $m[1] . $m[5];
                $o = dirname($thisFile) . '/' . $s;

                # heigh の設定ない場合は、高さがリサイズされないようにする
                if(
                empty($m[4])
                ){
                    $info = getImageSize($f);
                    if(
                    $m[3] > $info[0]
                    ){
                        $m[4] = floor($info[1] * ($m[3] / $info[0]));
                    } else{
                        $m[4] = $info[1];
                    }
                }
                ##
                if (file_exists($f)){
                        $img = new image();
                        if ($img->load($f)){
                                for($i = 0, $j = strlen($m[2]); $i <= $j; $i++){
                                        switch (substr($m[2], $i, 1)){
                                                case 'r': $img->resizeToWidthHeight($m[3], $m[4]); break;
                                                case 't': $img->triming($m[3], $m[4]); break;
                                        }
                                }
                                $img
                                ->header($m[5])
                                ->save($o)
                                ->output();
                                exit;
                        }
                }
        }
}
echo '404 error';
class image{
        private $_Image = false;
        private $_ImageType = '';
        private $_Info = null;

        public function load($filename){
                $this->_Image = false;
                $this->_Info = getImageSize($filename);
                $this->_ImageType = $this->_Info[2];
                if( $this->_ImageType == IMAGETYPE_JPEG ) {
                        $this->_Image = imageCreateFromJpeg($filename);
                } elseif( $this->_ImageType == IMAGETYPE_GIF ) {
                        $this->_Image = imageCreateFromGif($filename);
                } elseif( $this->_ImageType == IMAGETYPE_PNG ) {
                        $this->_Image = imageCreateFromPng($filename);
                } elseif( $this->_ImageType == IMAGETYPE_WBMP ) {
                        $this->_Image = imageCreateFromWbmp($filename);
                } elseif( $this->_ImageType == IMAGETYPE_JPEG2000 ) {
                } elseif( $this->_ImageType == IMAGETYPE_PSD ) {
                } elseif( $this->_ImageType == IMAGETYPE_BMP ) {
                } elseif( $this->_ImageType == IMAGETYPE_TIFF_II ) {
                } elseif( $this->_ImageType == IMAGETYPE_TIFF_MM ) {
                } elseif( $this->_ImageType == IMAGETYPE_TIFF_II ) {
                } elseif( $this->_ImageType == IMAGETYPE_TIFF_II ) {
                } elseif( $this->_ImageType == IMAGETYPE_TIFF_II ) {
                }
                return ($this->_Image !== false);
        }

        public function header($suffix = "jpg"){
                header('Content-transfer-encoding: binary');
                if(
                preg_match("/png$/ui", $suffix)
                ) {
                    header('Content-type: image/png');
                } else{
                    header('Content-type: image/jpeg');
                }
                return $this;
        }

        /*
         * 画像をファイルに保存
         */
        public function save($filename, $image_type = IMAGETYPE_JPEG, $compression = 100, $permissions = null) {
                $ret = false;
                if( $image_type == IMAGETYPE_JPEG ) {

                       $ret = imageJpeg($this->_Image, $filename, $compression);
                } elseif( $image_type == IMAGETYPE_GIF ) {
                        $ret = imageGif($this->_Image, $filename);
                } elseif( $image_type == IMAGETYPE_PNG ) {
                        $ret = imagePng($this->_Image, $filename);
                }
                if ($ret){
                        if( $permissions != null) {
                                $ret = chmod($filename, $permissions);
                        }
                }
                return $this;
        }

        public function output($image_type = IMAGETYPE_JPEG) {
                $ret = false;
                if( $image_type == IMAGETYPE_JPEG ) {
                        $ret = imageJpeg($this->_Image);
                } elseif( $image_type == IMAGETYPE_GIF ) {
                        $ret = imageGif($this->_Image);
                } elseif( $image_type == IMAGETYPE_PNG ) {
                        $ret = imagePng($this->_Image);
                }
                return $ret;
        }

        public function getWidth() {
                return imagesx($this->_Image);
        }

        public function getHeight() {
                return imagesy($this->_Image);
        }
        public function triming($widthRatio, $heightRatio){
                $width = $this->getWidth();
                $height = $this->getHeight();
                if (($width < 2) || ($height < 2)) return;
                $size = array($width, ($width / $widthRatio * $heightRatio));
                $center = array(0, ($height - $size[1]) / 2);
                if ($size[1] > $height){

                        $size = array(($height / $heightRatio * $widthRatio), $height);
                        $center = array(($width - $size[0]) / 2, 0);
                }
                $new_image = imageCreateTrueColor($size[0], $size[1]);
                imageCopyReSampled($new_image, $this->_Image, 0, 0, $center[0], $center[1], $size[0], $size[1], $size[0], $size[1]);
                $this->_Image = $new_image;
                return $this;
        }

        public function resizeToWidth($width) {
                $ratio = $width / $this->getWidth();
                $height = $this->getheight() * $ratio;
                $this->resize($width,$height);
                return $this;
        }

        public function resizeToHeight($height) {
                $ratio = $height / $this->getHeight();
                $width = $this->getWidth() * $ratio;
                $this->resize($width,$height);
                return $this;
        }

        public function resizeToWidthHeight($width, $height) {
                $wh = $this->getRatioJustSize($width, $height);
                $this->resize($wh[0], $wh[1]);
                return $this;
        }

        public function scale($scale) {
                $width = $this->getWidth() * $scale/100;
                $height = $this->getheight() * $scale/100;
                $this->resize($width,$height);
                return $this;
        }


        public function resize($width, $height) {
                $new_image = imagecreatetruecolor($width, $height);
                imagecopyresampled($new_image, $this->_Image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
                $this->_Image = $new_image;
                return $this;
        }

        public function getRatioJustSize($baseWidth = 0, $baseHeight = 0){
                $iw = $this->getWidth();
                $ih = $this->getHeight();
                if ($iw > $baseWidth){
                        $ih = $ih * ($baseWidth / $iw);
                        $iw = $baseWidth;
                }
                if ($ih > $baseHeight){
                        $iw = $iw * ($baseHeight / $ih);
                        $ih = $baseHeight;
                }
                return array($iw, $ih);
        }
}
?>



//-----htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ img.php?Command=$1&%{QUERY_STRING} [L]

//-----設置方法
htdocs/share/img.php
htdcos/share/.htaccess

でOK.

(Thanks.)
http://blog.aulta.net/2011/05/24/php-jpg%E7%94%BB%E5%83%8F%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%82%92%E3%83%88%E3%83%AA%E3%83%9F%E3%83%B3%E3%82%B0%EF%BC%8F%E3%83%AA%E3%82%B5%E3%82%A4%E3%82%BA%E3%81%99%E3%82%8B%E3%80%82/