Implementation code of php high definition lossless picture compression function

  • 2021-11-13 01:02:49
  • OfStack

It is often used to compress uploaded large pictures, especially the volume. In APP applications such as WeChat, they are compressed by default. So, how to compress pictures greatly but still maintain high clarity?

Compression usually has scaling and specified width compression, and the effect is very good. The 4M picture taken by a digital camera keeps high definition and original width and height value after compression, only 700K.

Here's the code (there are two files, the imgcompress. class. php class, and compress. php)

compress.php


 <?php
 require_once 'imgcompress.class.php';
 $source = 'test.png';// Original file name 
 $dst_img = 'test_.png';// Save the file name of the picture 
 $percent = ; # The original image is compressed without scaling, but the volume is greatly reduced 
 $image = (new imgcompress($source,$percent))->compressImg($dst_img);

imgcompress.class.php


 <?php
  /**
  *  Picture compression class: Compress by scaling. 
  *  If you want to keep the scale of the source image, change the parameter $percent Just keep it as. 
  *  Even if the original ratio is compressed, it can be greatly reduced. Digital camera M Pictures. It can also be reduced to KB Left and right. If the scale is reduced, the volume will be smaller. 
  *
  *  Results: It can be saved and displayed directly. 
  */
 class imgcompress{
   private $src;
   private $image;
   private $imageinfo;
   private $percent = .;
   /**
    *  Picture compression 
    * @param $src  Source map 
    * @param float $percent  Compression ratio 
    */
   public function __construct($src, $percent=)
   {
     $this->src = $src;
     $this->percent = $percent;
   }
   /**  HD compressed picture 
    * @param string $saveName  Provide the image name (without extension, use the source image extension) for saving. Or display it directly without providing a file name 
    */
   public function compressImg($saveName='')
   {
     $this->_openImage();
     if(!empty($saveName)) $this->_saveImage($saveName); // Save 
     else $this->_showImage();
   }
   /**
    *  Inside: Open the picture 
    */
   private function _openImage()
   {
     list($width, $height, $type, $attr) = getimagesize($this->src);
     $this->imageinfo = array(
       'width'=>$width,
       'height'=>$height,
       'type'=>image_type_to_extension($type,false),
       'attr'=>$attr
     );
     $fun = "imagecreatefrom".$this->imageinfo['type'];
     $this->image = $fun($this->src);
     $this->_thumpImage();
   }
   /**
    *  Internal: Operation picture 
    */
   private function _thumpImage()
   {
     $new_width = $this->imageinfo['width'] * $this->percent;
     $new_height = $this->imageinfo['height'] * $this->percent;
     $image_thump = imagecreatetruecolor($new_width,$new_height);
     // Copy the original image onto the picture carrier and follow 1 Proportional compression , The clarity is greatly maintained 
     imagecopyresampled($image_thump,$this->image,,,,,$new_width,$new_height,$this->imageinfo['width'],$this->imageinfo['height']);
     imagedestroy($this->image);
     $this->image = $image_thump;
   }
   /**
    *  Output picture : To save pictures, use the saveImage()
    */
   private function _showImage()
   {
     header('Content-Type: image/'.$this->imageinfo['type']);
     $funcs = "image".$this->imageinfo['type'];
     $funcs($this->image);
   }
   /**
    *  Save pictures to hard disk: 
    * @param string $dstImgName  You can specify the name of the string without suffix, using the source image extension   . Directly specify the target image name with an extension. 
    */
   private function _saveImage($dstImgName)
   {
     if(empty($dstImgName)) return false;
     $allowImgs = ['.jpg', '.jpeg', '.png', '.bmp', '.wbmp','.gif'];  // Use the target image extension if the target image name has a suffix   Suffix, if not, the extension of the source image 
     $dstExt = strrchr($dstImgName ,".");
     $sourseExt = strrchr($this->src ,".");
     if(!empty($dstExt)) $dstExt =strtolower($dstExt);
     if(!empty($sourseExt)) $sourseExt =strtolower($sourseExt);
     // Has the specified target name extension 
     if(!empty($dstExt) && in_array($dstExt,$allowImgs)){
       $dstName = $dstImgName;
     }elseif(!empty($sourseExt) && in_array($sourseExt,$allowImgs)){
       $dstName = $dstImgName.$sourseExt;
     }else{
       $dstName = $dstImgName.$this->imageinfo['type'];
     }
     $funcs = "image".$this->imageinfo['type'];
     $funcs($this->image,$dstName);
   }
   /**
   *  Destroy pictures 
   */
   public function __destruct(){
     imagedestroy($this->image);
   }
 }

After use, I feel that it is good to set $percent to about 0.5, and the quality of compressed pictures and original pictures is basically 1.

Summarize


Related articles: