SoFunction
Updated on 2025-03-02

Complete example of the image (thumbnail) processing class encapsulated by php

This article describes the image (thumbnail) processing class encapsulated by PHP. Share it for your reference, as follows:

<?php
//Picture processing tool classclass Image{
    //property    private $thumb_width; //The width of the thumbnail    private $thumb_height;
    //Error attribute    public $thumb_error;
    //Construction method    public function __construct($width = 0,$height = 0){
      $this->thumb_width = ($width == 0) ? $GLOBALS['config']['admin_goods_thumb']['width'] : $width;
      $this->thumb_height = ($height == 0) ? $GLOBALS['config']['admin_goods_thumb']['height'] : $height;
    }
    /*
      * Make thumbnails
      * @param1 string $src, original image path, /uploads/
      * @param2 string $path, thumbnail save path/uploads/thumb_20150122101010abcdef.gif
      * @return The name of the thumbnail
     */
    public function makeThumb($src,$path){
      //Judge whether the original image exists      if(!file_exists($src)){
        $this->thumb_error = 'The original picture does not exist!  ';
        return false;
      }
      //Open the original image resource      //Get the suffix that can be used      $ext = $this->getFunctionName($src); //gif
      //Patch together the function name      $open = 'imagecreatefrom' . $ext;    //imagecreatefromgif
      $save = 'image' . $ext;          //imagegif
      //If it is not clear; echo $open,$save; exit;      //The variable function opens the original image resource      $src_img = $open($src); //Use variable functions to open image resources      //imagecreatefromgif($src)
      //Thumbnail resources      $dst_img = imagecreatetruecolor($this->thumb_width,$this->thumb_height);
      //Background color fills white      $dst_bg_color = imagecolorallocate($dst_img,255,255,255);
      imagefill($dst_img,0,0,$dst_bg_color);
      //Aspect and height ratio determine width and height      $dst_size = $this->thumb_width / $this->thumb_height;
      //Get original image data      $file_info = getimagesize($src);
      $src_size = $file_info[0]/$file_info[1];
      //Finish the width and height of the thumbnail      if($src_size > $dst_size){
        //The aspect ratio of the original image is greater than that of the thumbnail        $width = $this->thumb_width;
        $height = round($width / $src_size);
      }else{
        $height = $this->thumb_height;
        $width = round($height * $src_size);
      }
      //Finish the starting position of the thumbnail image      $dst_x = round($this->thumb_width - $width)/2;
      $dst_y = round($this->thumb_height - $height)/2;
      //Make thumbnails      if(imagecopyresampled($dst_img,$src_img,$dst_x,$dst_y,0,0,$width,$height,$file_info[0],$file_info[1])){
        //Sampling successfully: save, save the file to the corresponding path        $thumb_name = 'thumb_' . basename($src);
        $save($dst_img,$path . '/' . $thumb_name);
        //Save successfully        return $thumb_name;
      }else{
        //Sampling failed        $this->thumb_error = 'Thumbnail sampling failed!  ';
        return false;
      }
    }
    /*
      * Get the function name to call the file
      * @param1 string $file, file name
      * @return Function string obtained by file suffix name
     */
    private function getFunctionName($file){
      //Get the file suffix      $file_info = pathinfo($file);
      $ext = $file_info['extension']; //Suffix: gif, png, jpg, jpeg, pjpeg      //imagecreatefromgif,imagecreatefromjpeg,imagecreatefrompng
      //Define an array to save function name      $func = array(
        'gif' => 'gif',
        'png' => 'png',
        'jpg' => 'jpeg',
        'jpeg' => 'jpeg',
        'pjpeg' => 'jpeg'
      );
      //Return value      return $func[$ext];
    }
}

For more information about PHP related content, please check out the topic of this site:PHP file operation summary》、《Summary of PHP graphics and picture operation skills》、《Complete collection of PHP array (Array) operation techniques》、《Introduction to PHP basic syntax》、《Summary of PHP operations and operator usage》、《PHP object-oriented programming tutorial》、《Summary of PHP network programming skills》、《Summary of usage of php strings》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php

I hope this article will be helpful to everyone's PHP programming.