Home >Backend Development >PHP Tutorial >PHP generates proportional thumbnail class and custom function sharing_PHP tutorial

PHP generates proportional thumbnail class and custom function sharing_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:26:06956browse

There are two equal-proportion thumbnail methods that you can learn from
1. It is a class file, which can be used after instantiation
2. It is a custom method, which is relatively lightweight

Class file

Copy code The code is as follows:
$resizeimage = new resizeimage("./shawn.jpg", "200", "100", "0 ","../pic/shawnsun.jpg");
//Instantiate the following class to generate thumbnails
//Among them, the source file and thumbnail addresses can be the same, 200 and 100 represent width and height respectively, the fourth parameter is optional, 0 does not take a screenshot, 1 means a screenshot

Copy code The code is as follows:
class resizeimage{

//Image type
Public $type;
//Actual width
Public $width;
//Actual height
Public $height;
//Changed width
Public $resize_width;
//Height after change
Public $resize_height;
//Whether to crop the image
Public $cut;
//Source image
Public $srcimg;
//Target image address
Public $dstimg;
//Temporarily created image
Public $im;
​  
Function resizeimage($img, $wid, $hei,$c,$dstpath){
​  
               $this--->srcimg = $img;
              $this->resize_width = $wid;
              $this->resize_height = $hei;
               $this->cut = $c;
​  
//Type of image
               $this->type = strtolower(substr(strrchr($this->srcimg,"."),1));
​​​​​ //Initialize image
             $this->initi_img();
//Target image address
             $this->dst_img($dstpath);
                 //W & H
$this->width = imagesx($this->im);
                $this->height = imagesy($this->im);
​​​​​ //Generate image
              $this->newimg();
ImageDestroy ($this->im);
}
​  
Function newimg(){
​  
​​​​ //The proportion of the changed image
          $resize_ratio = ($this->resize_width)/($this->resize_height);
//Proportion of actual image
          $ratio = ($this->width)/($this->height);
                                   
           if(($this->cut)=="1")
// Creed            {
If($ratio>=$resize_ratio)
​​​​​​ //High priority
                {
$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),
$this->height
);
ImageJpeg ($newimg,$this->dstimg);
            }
If($ratio<$resize_ratio)
​​​​​​ //Width priority
            {
                $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);
            }
              }
        else
        //不裁图
        {
            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->height
                );
                ImageJpeg ($newimg,$this->dstimg);
            }
            if($ratio<$resize_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->height
                );
                ImageJpeg ($newimg,$this->dstimg);
            }
        }
   }
    
    //初始化图象
    function 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);
        }
    }
    //图象目标地址
    function 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;
 
        //echo $this->dstimg;
    }
}
 
?>

自定义方法

复制代码 代码如下:

thumbs('shawn.jpg','shawnsun.jpg',100,100);
//参数属性类似于方法一

复制代码 代码如下:

 
function thumbs($FileName,$SaveTo,$SetW,$SetH){
    $IMGInfo= getimagesize($FileName);
    if(!$IMGInfo) return false;
        
    if($IMGInfo['mime']== "image/pjpeg" || $IMGInfo['mime']=="image/jpeg"){
        $ThisPhoto= imagecreatefromjpeg($FileName);
    }elseif($IMGInfo['mime']== "image/x-png" || $IMGInfo['mime']== "image/png"){
        $ThisPhoto= imagecreatefrompng($FileName);  
    }elseif($IMGInfo['mime']== "image/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);
    imagecopyresized($NewPhoto,$ThisPhoto,$px,$py,0,0,$nw,$nh,$width,$height);
    ImageJpeg ($NewPhoto,$SaveTo);
    return true;
}
    
?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/824811.htmlTechArticle共有两种等比例缩略图方法可以借鉴 一、为类文件,实例化之后即可使用 二、为自定义方法,比较轻巧 类文件 复制代码 代码如下: $resi...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn