SoFunction
Updated on 2025-03-02

PHP implements watermark and thumbnail production in common image formats (object-oriented)

This article shares the php watermark and thumbnail production code for everyone, using object-oriented methods to realize the production of watermarks and thumbnails in common image formats jpg, png, and gif for your reference. The specific content is as follows

<?php
header('Content-Type:text/html;charset=utf-8');
/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
//Add a watermark to the pictureClass Water{
 //Open the watermark private $watermark_on = '1';
  
 public $water_img;
  
 //Watermark location public $pos = 1; 
  
 //Compression ratio public $pct = 80;
  
 //transparency public $quality = 80;
  
 public $text = 'Fun Net';
  
 public $size = 12;
  
 public $color = '#000000';
  
 public $font = '';
  
 //The production of thumb //The default thumbnail function is enabled private $thumb_on = 1;
 //How to generate thumbnails public $thumb_type = 1;
 // Generate the width of the thumbnail public $thumb_width;
 //The height of the generated thumbnail public $thumb_height;
 //Generate the suffix name of the thumbnail public $thumb_fix = '_dq';
 
 //Thumbnail function processing public function thumb( $img,$outfile='',$t_type='',$t_w='',$t_h='' ){
  //Verify whether the picture meets the requirements  if(!$this->check($img) || !$this->thumb_on) return FALSE;
   
  //Define the initial value of the thumbnail  $t_type = $t_type ? $t_type : $this->thumb_type;
  $t_w = $t_w ? $t_w : $this->thumb_width;
  $t_h = $t_h ? $t_h : $this->thumb_height;
   
  //Get the original image information  $img_info = getimagesize($img);
  $img_w = $img_info[0];
  $img_h = $img_info[1];
  //Get the file suffix of the image type  $img_type = image_type_to_extension($img_info[2]);
  //Get the relevant size  $thumb_size = $this->thumb_size($img_w,$img_h,$t_w,$t_h,$t_type);
  //Determine the original image type  //Use custom functions to determine the image type  $func = "imagecreatefrom".substr($img_type, 1);
  $res_img = $func($img);
   
  //Thumbnail resource Edit picture resource moon  if( $img_type == '.gif' || $img_type == '.png' ){
   $res_thumb = imagecreate($thumb_size[0], $thumb_size[1]);
   $color = imagecolorallocate($res_thumb, 255, 0, 0);
  }else{
   $res_thumb = imagecreatetruecolor($thumb_size[0], $thumb_size[1]);
  }
   
  //Make thumbnails  if(function_exists( "imagecopyresampled" ) ){
   imagecopyresampled($res_thumb, $res_img, 0, 0, 0, 0, $thumb_size[0],$thumb_size[1],$thumb_size[2],$thumb_size[3]);
  }else{
   imagecopyresized($res_thumb, $res_img, 0, 0, 0, 0, $thumb_size[0],$thumb_size[1],$thumb_size[2],$thumb_size[3]);
  }
  //Treat transparent colors  if( $img_type =='.gif' || $img_type == '.png' ){
   imagecolortransparent($res_thumb,$color);
  }
   
  //Configure output file name  $outfile = $outfile ? $outfile : $($img,0,strripos($img,'.')).$this->thumb_fix.$img_type;
   
  //Save the output of the file  $func = "image".substr($img_type, 1);
  $func($res_thumb,$outfile);
  if(isset($res_thumb)) imagedestroy ($res_thumb);
  if(isset($res_img)) imagedestroy ($res_img);
  return $outfile;
 } 
 
 public function watermark( $img,$pos='',$out_img='',$water_img='',$text='' ){
  if(!$this->check($img) || !$this->watermark_on) return false;
   
  $water_img = $water_img ? $water_img : $this->water_img;
  //The watermark is turned on  $waterimg_on = $this->check($water_img) ? 1 : 0;
  //Judge whether it is operating on the original image  $out_img = $out_img ? $out_img : $img;
  //Judge the location of the watermark  $pos = $pos ? $pos : $this->pos;
  //Watermark text  $text = $text ? $text : $this->text;
   
   
  $img_info = getimagesize($img);
  $img_w = $img_info[0];
  $img_h = $img_info[1];
  //Judge the type of watermark image   
   
  if( $waterimg_on ){
   $w_info = getimagesize($water_img);
   $w_w = $w_info[0];
   $w_h = $w_info[1];
   if ( $img_w < $w_w || $img_h < $w_h ) return false;
   switch ( $w_info[2] ){
    case 1:
     $w_img = imagecreatefromgif($water_img);
     break;
    case 2:
     $w_img = imagecreatefromjpeg($water_img);
     break;
    case 3:
     $w_img = imagecreatefrompng($water_img);
     break;
   }
  }else{
   if( empty($text) || strlen($this->color)!=7 ) return FALSE;
   $text_info = imagettfbbox($this->size, 0, $this->font, $text);
   $w_w = $text_info[2] - $text_info[6];
   $w_h = $text_info[3] - $text_info[7];
  }
   
  //Create original image resources   
  switch ( $img_info[2] ){
   case 1:
    $res_img = imagecreatefromgif($img);
    break;
   case 2:
    $res_img = imagecreatefromjpeg($img);
    break;
   case 3:
    $res_img = imagecreatefrompng($img);
    break;
  }
  //Determine the location of the watermark  switch ( $pos ){
   case 1:
    $x = $y =25;
    break;
   case 2:
    $x = ($img_w - $w_w)/2; 
    $y = 25;
    break;
   case 3:
    $x = $img_w - $w_w;
    $y = 25;
    break;
   case 4:
    $x = 25;
    $y = ($img_h - $w_h)/2;
    break;
   case 5:
    $x = ($img_w - $w_w)/2; 
    $y = ($img_h - $w_h)/2;
    break;
   case 6:
    $x = $img_w - $w_w;
    $y = ($img_h - $w_h)/2;
    break;
   case 7:
    $x = 25;
    $y = $img_h - $w_h;
    break;
   case 8:
    $x = ($img_w - $w_w)/2;
    $y = $img_h - $w_h;
    break;
   case 9:
    $x = $img_w - $w_w;
    $y = $img_h - $w_h;
    break;
   default :
    $x = mt_rand(25, $img_w - $w_w);
    $y = mt_rand(25, $img_h - $w_h);
  }
   
  //Write to image resources  if( $waterimg_on ){
   imagecopymerge($res_img, $w_img, $x, $y, 0, 0, $w_w, $w_h, $this->pct); 
 }else{
  $r = hexdec(substr($this->color, 1,2));
  $g = hexdec(substr($this->color, 3,2));
  $b = hexdec(substr($this->color, 5,2));
  $color = imagecolorallocate($res_img, $r, $g, $b);
  imagettftext($res_img, $this->size, 0, $x, $y, $color, $this->font, $text); 
 }
  
 //Generate image type switch ( $img_info[2] ){
  case 1:
   imagecreatefromgif($res_img,$out_img);
   break;
  case 2:
   //imagecreatefromjpeg($res_img,$out_img);
   imagejpeg($res_img,$out_img);
   break;
  case 3:
   imagepng($res_img,$out_img);
   break;
 }
 if(isset($res_img)) imagedestroy ($res_img);
 if(isset($w_img)) imagedestroy($w_img);
 return TRUE;
} 
 //Verify whether the image exists  private function check($img){
   $type = array('.jpg','.jpeg','.png','.gif');
   $img_type = strtolower(strrchr($img, '.'));
   return extension_loaded('gd') && file_exists($img) && in_array($img_type, $type);
  } 
   
  //Get the relevant proportion of thumbnails  //The processing type of the image is obtained  private function thumb_size( $img_w,$img_h,$t_w,$t_h,$t_type){
   //Define thumbnail size   $w = $t_w;
   $h = $t_h;
    
   //Define the original size of the picture   $cut_w = $img_w;
   $cut_h = $img_h;
    
   //When the target image is smaller than the size of the thumbnail;   if( $img_w <= $t_w && $img_h < $t_h ){
    $w = $img_w;
    $h = $img_h;
   }else{
    if( !empty($t_type) && $t_type>0 ){
     switch ( $t_type ){
      //When the width is fixed      case 1:
       $h = $t_w/$img_w*$img_h;
       break;
      //When height is fixed      case 2:
       $w = $t_h/$img_h*$img_w;
       break;
      //Fixed width, height cut      case 3:
       $cut_h = $img_w/$t_w*$t_h;
       break;
      //Fixed height, cut width      case 4:
       $cut_w = $img_h/$t_h*$t_w;
       break;
      //Equiscale scaling      default :
       if( ($img_w/$t_w) > ($img_h/$t_h) ){
        $h = $t_w/$img_w*$t_h;
       }elseif( ($img_w/$t_w) < ($img_h/$t_h) ){
        $w = $t_h/$img_h*$t_w;
       }else{
        $w = $t_w;
        $h = $t_h;
       }
     }
    }
     
     
   }
   $arr[0] = $t_w;
   $arr[1] = $t_h;
   $arr[2] = $cut_w;
   $arr[3] = $cut_h;
   return $arr;
 }
}

The above is all about this article, I hope it will be helpful for everyone to learn PHP programming.