ホームページ  >  記事  >  バックエンド開発  >  PHP は比例サムネイル クラスとカスタム関数を生成します Sharing_PHP チュートリアル

PHP は比例サムネイル クラスとカスタム関数を生成します Sharing_PHP チュートリアル

WBOY
WBOYオリジナル
2016-07-13 10:26:06916ブラウズ

学習できる均等な比率のサムネイル方法が 2 つあります
1. インスタンス化して使用できるクラスファイルです
2. カスタマイズされたメソッドなので、比較的軽量です

クラスファイル

コードをコピー コードは次のとおりです:
$resizeimage = new assignimage("./shawn.jpg", "200", "100", "0","../pic/shawnsun.jpg ");
//サムネイルを生成するために次のクラスをインスタンス化します
//そのうち、ソースファイルとサムネイルのアドレスは同じにすることができます、200と100はそれぞれ幅と高さを表します、4番目のパラメータはオプションです、0はスクリーンショットを取得しません、1はスクリーンショットを意味します

コードをコピーします コードは次のとおりです:
クラスサイズ変更イメージ{

//画像タイプ
パブリック $type;
//実際の幅
パブリック $width;
//実際の身長
公開 $height;
//変更された幅
パブリック $resize_width;
//変更後の高さ
パブリック $resize_height;
//画像をトリミングするかどうか
公開 $cut;
//ソース画像
公開 $srcimg;
//対象の画像アドレス
公開 $dstimg;
//一時的に作成した画像
公開 $im;

関数 Resizeimage($img, $wid, $hei,$c,$dstpath){

$this--->srcimg = $img;
$this->resize_width = $wid;
$this->resize_height = $hei;
$this->cut = $c;

//画像の種類
$this->type = strto lower(substr(strrchr($this->srcimg,"."),1));
//画像を初期化します
$this->initi_img();
//対象の画像アドレス
$this->dst_img($dstpath);
//W & H
$this->width = imagex($this->im);
$this->height = imagey($this->im);
//画像を生成
$this->newimg();
ImageDestroy ($this->im);
}

関数 newimg(){

//変更された画像の割合
$resize_ratio = ($this->resize_width)/($this->resize_height);
//実際の画像の比率
$ratio = ($this->幅)/($this->高さ);
                                       if(($this->cut)=="1")
// 画像をトリミングします
{
if($ratio>=$resize_ratio)
// 高優先度
{
$newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,
$this->resize_height, (($this->height)*$resize_ratio),
                                                        );
ImageJpeg ($newimg,$this->dstimg);
}
if($ratio //幅優先
            {
                $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,
                                   $this->resize_height、$this->width、
                                   (($this->width)/$resize_ratio)
                );
                ImageJpeg ($newimg,$this->dstimg);
            }
              }
        それ以外
        //不裁图
        {
            if($ratio>=$resize_ratio)
            {
                $newimg = imagecreatetruecolor($this->resize_width,
                                               ($this->resize_width)/$ratio
                );
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,
                                   ($this->resize_width)/$ratio、$this->width、
                                   $this->身長
                );
                ImageJpeg ($newimg,$this->dstimg);
            }
            if($ratio             {
                $newimg = imagecreatetruecolor(($this->resize_height)*$ratio,
                                                $this->resize_height
                );
                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0,
                                   ($this->resize_height)*$ratio、
                                   $this->resize_height、$this->width、
                                   $this->身長
                );
                ImageJpeg ($newimg,$this->dstimg);
            }
        }
   }
    
    //初期化图象
    関数 initi_img(){
 
        if($this->type=="jpg")
        {
            $this->im = imagecreatefromjpeg($this->srcimg);
        }
        if($this->type=="gif")
        {
            $this->im = imagecreatefromgif($this->srcimg);
        }
        if($this->type=="png")
        {
            $this->im = imagecreatefrompng($this->srcimg);
        }
    }
    //图象目标地址
    関数 dst_img($dstpath){
    
        $full_length = strlen($this->srcimg);
        $type_length = strlen($this->type);
        $name_length = $full_length-$type_length;
 
        $name = substr($this->srcimg,0,$name_length-1);
        $this->dstimg = $dstpath;
 
        //エコー $this->dstimg;
    }
}
 
?>

独自の方法

复制代码代码如下:

サムズ('ショーン.jpg','ショーンサン.jpg',100,100);
//パラメータプロパティはメソッド 1 に似ています

复制代価代価如下:

 
関数つまみ($FileName,$SaveTo,$SetW,$SetH){
    $IMGInfo= getimagesize($FileName);
    if(!$IMGInfo) は false を返します;
        
    if($IMGInfo['mime']== "画像/pjpeg" || $IMGInfo['mime']=="画像/jpeg"){
        $ThisPhoto= imagecreatefromjpeg($FileName);
    }elseif($IMGInfo['mime']== "画像/x-png" || $IMGInfo['mime']== "画像/png"){
        $ThisPhoto= imagecreatefrompng($FileName);  
    }elseif($IMGInfo['mime']== "画像/gif"){
        $ThisPhoto=imagecreatefromgif($FileName);
    }
    
    $width=$IMGInfo[0];
    $height=$IMGInfo[1];  
    $scalc = max($width/$SetW,$height/$SetH);
    $nw = intval($width/$scalc);
    $nh = intval($height/$scalc);
    echo "缩略大小:w$nw,h$nh
";
    
    if($SetW-$nw == 1){$nw = $SetW;}
    if($SetH-$nh == 1){$nh = $SetH;}
    echo "+修正1像素: w$nw,h$nh
";
    
    //补宽
    if($SetW-$nw > 0){
        $nh = $nh +(($nh/$nw) * ($SetW-$nw));
        echo "*必要补宽".($SetW-$nw).",陪补高".(($nh/$nw) * ($SetW-$nw))."
"; 
        $nw = $SetW;
    }
    //补高い
    if($SetH-$nh > 0){
        $nw = $nw + (($nw/$nh) * ($SetH-$nh));
        echo "*必要补高".($SetH-$nh).",陪补宽"。 (($nw/$nh) * ($SetH-$nh)) ."
";
        $nh = $SetH;
    }
    $nw = intval($nw);
    $nh = intval($nh);
    echo "+修正大小:w$nw,h$nh
";
    
    $px = ($SetW - $nw)/2;
    $py = ($SetH - $nh)/2;
    echo "窗口大小:w$SetW,h$SetH
";
    echo "+偏移修正:x$px,y$py
";
    
    $NewPhoto=imagecreatetruecolor($SetW,$SetH);
    imagecopyresize($NewPhoto,$ThisPhoto,$px,$py,0,0,$nw,$nh,$width,$height);
    ImageJpeg ($NewPhoto,$SaveTo);
    true を返します;
}
    
?>

www.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/824811.html技術記事共有二种等比缩略图方法可用借用一、类文件,实例化後即可二,自定方法,比较轻巧类文件复制代码如下: $resi...
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。