PHP generates equal ratio thumbnail classes and custom function sharing

  • 2021-07-06 10:17:19
  • OfStack

There are two equal-scale thumbnail methods that can be used for reference
1. Is a class file, which can be used after instantiation
2. It is a custom method, which is lighter

Class file

$resizeimage = new resizeimage("./shawn.jpg", "200", "100", "0","../pic/shawnsun.jpg");
// Instantiate the following classes , You can generate thumbnails
// Among them , Source file and thumbnail address can be the same ,200,100 Represents width and height respectively, and the first 4 Parameters are optional 0 No screenshots, 1 For screenshots

<?php
class resizeimage{
 
    // Picture type
    public $type;
    // Actual width
    public $width;
    // Actual height
    public $height;
    // Width after change
    public $resize_width;
    // Altered height
    public $resize_height;
    // Whether to trim or not
    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 picture
          $this->type = strtolower(substr(strrchr($this->srcimg,"."),1));
          // Initialization image
          $this->initi_img();
          // Target image address
          $this->dst_img($dstpath);
          //W & H
          $this->width  = imagesx($this->im);
          $this->height = imagesy($this->im);
          // Generate an image
          $this->newimg();
          ImageDestroy ($this->im);
     }
    
    function newimg(){
    
        // The scale of the changed image
        $resize_ratio = ($this->resize_width)/($this->resize_height);
        // Scale of actual image
        $ratio = ($this->width)/($this->height);
        
        if(($this->cut)=="1")
        // Drawing
        {
            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
        // Non-trimming diagram
        {
            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);
            }
        }
   }
    
    // Initialization image
    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);
        }
    }
    // Image target address
    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;
    }
}
 
?>

Custom method


thumbs('shawn.jpg','shawnsun.jpg',100,100);
// Parameter properties are similar to methods 1


<?php
 
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 " Thumbnail size: w$nw,h$nh <br /-->";
    
    if($SetW-$nw == 1){$nw = $SetW;}
    if($SetH-$nh == 1){$nh = $SetH;}
    echo "+ Amendment 1 Pixels: w$nw,h$nh<br>";
    
    // Complement width
    if($SetW-$nw > 0){
        $nh = $nh +(($nh/$nw) * ($SetW-$nw));
        echo "* Width to be patched ".($SetW-$nw).", Accompanying and supplementing high ".(($nh/$nw) * ($SetW-$nw))."  <br>"; 
        $nw = $SetW;
    }
    // Replenish heights
    if($SetH-$nh > 0){
        $nw = $nw + (($nw/$nh) * ($SetH-$nh));
        echo "* Need to be supplemented ".($SetH-$nh).", Accompanying width ". (($nw/$nh) * ($SetH-$nh)) ."<br>";
        $nh = $SetH;
    }
    $nw = intval($nw);
    $nh = intval($nh);
    echo "+ Correction size: w$nw,h$nh<br>";
    
    $px = ($SetW - $nw)/2;
    $py = ($SetH - $nh)/2;
    echo " Window size: w$SetW,h$SetH <br>";
    echo "+ Offset correction: x$px,y$py <br>";
    
    $NewPhoto=imagecreatetruecolor($SetW,$SetH);
    imagecopyresized($NewPhoto,$ThisPhoto,$px,$py,0,0,$nw,$nh,$width,$height);
    ImageJpeg ($NewPhoto,$SaveTo);
    return true;
}
    
?>


Related articles: