Home > Backend Development > PHP Tutorial > PHP encapsulation class for adding image watermark, compression, and cutting

PHP encapsulation class for adding image watermark, compression, and cutting

PHPz
Release: 2018-10-11 15:53:44
Original
1260 people have browsed it

Adding watermarks to pictures is actually adding the original pictures and watermarks together. Below, I will share with you the information I have recently compiled.

PHP mainly uses GD library extension to operate image files. When we frequently use PHP to operate images, we will naturally encapsulate many functions, otherwise we will write too much repetitive code. When there are many functions related to pictures, we can consider organizing these functions, so we have the idea of ​​encapsulating them into classes.

There are four main steps to operating pictures:

Step 1: Open the picture

Step 2: Manipulate the picture

Step 3: Output the picture

Step 4: Destroy the picture

Steps 1, 3, and 4 must be written every time, and each time Pretty much the same. The only step that really needs to be changed is the image manipulation step. Manipulating pictures is often done through one or more main GD functions.

The four methods in the package class of this article are text watermark (imagettftext()), image watermark (imagecopymerge()), image compression, image cutting (imagecopyresampled()), and the rest are commonly used GD The functions will not be described in detail.

Directly enter the code:

info=getimagesize($src);
  $this->type=image_type_to_extension($this->info['2'],false);
  $fun="imagecreatefrom{$this->type}";
  $this->image=$fun($src);
 }
 /**
  * 文字水印
  * @param [type] $font  字体
  * @param [type] $content 内容
  * @param [type] $size  文字大小
  * @param [type] $col  文字颜色(四元数组)
  * @param array $location 位置 
  * @param integer $angle 倾斜角度
  * @return [type]   
  */
 public function fontMark($font,$content,$size,$col,$location,$angle=0){
  $col=imagecolorallocatealpha($this->image, $col['0'], $col['1'], $col['2'],$col['3']);
  imagettftext($this->image, $size, $angle, $location['0'], $location['1'], $col,$font,$content);
 }
 /**
  * 图片水印
  * @param [type] $imageMark 水印图片地址
  * @param [type] $dst  水印图片在原图片中的位置
  * @param [type] $pct  透明度
  * @return [type]   
  */
 public function imageMark($imageMark,$dst,$pct){
  $info2=getimagesize($imageMark);
  $type=image_type_to_extension($info2['2'],false);
  $func2="imagecreatefrom".$type;
  $water=$func2($imageMark);
  imagecopymerge($this->image, $water, $dst[0], $dst[1], 0, 0, $info2['0'], $info2['1'], $pct);
  imagedestroy($water);
 }
 /**
  * 压缩图片
  * @param [type] $thumbSize 压缩图片大小
  * @return [type]   [description]
  */
 public function thumb($thumbSize){
  $imageThumb=imagecreatetruecolor($thumbSize[0], $thumbSize[1]);
  imagecopyresampled($imageThumb, $this->image, 0, 0, 0, 0, $thumbSize[0], $thumbSize[1], $this->info['0'], $this->info['1']);
  imagedestroy($this->image);
  $this->image=$imageThumb;
 }
 /**
 * 裁剪图片
  * @param [type] $cutSize 裁剪大小
  * @param [type] $location 裁剪位置
  * @return [type]   [description]
  */
  public function cut($cutSize,$location){
   $imageCut=imagecreatetruecolor($cutSize[0],$cutSize[1]);
   imagecopyresampled($imageCut, $this->image, 0, 0, $location[0], $location[1],$cutSize[0],$cutSize[1],$cutSize[0],$cutSize[1]);
   imagedestroy($this->image);
   $this->image=$imageCut;
  }
 /**
  * 展现图片
  * @return [type] [description]
  */
 public function show(){
  header("content-type:".$this->info['mime']);
  $funn="image".$this->type;
  $funn($this->image);
 }
 /**
  * 保存图片
 * @param [type] $newname 新图片名
 * @return [type]   [description]
 */
  public function save($newname){
   header("content-type:".$this->info['mime']);
   $funn="image".$this->type;
   $funn($this->image,$newname.'.'.$this->type);
  }
  public function __destruct(){
   imagedestroy($this->image);
  }
 }
 ?>
Copy after login

Note that the main logic is to merge the two images through the imagecopymerge() method Together they create a watermark effect. Let’s take a look at the method prototype and parameters of this method:

bool imagecopymerge ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int$src_x , int $src_y , int $src_w , int $src_h , int $pct )
Copy after login

Copy the part of the src_im image with coordinates starting from src_x, src_y, width src_w, and height src_h to the dst_im image The center coordinates are dst_x and dst_y. The two images will be merged based on pct, which ranges from 0 to 100. When pct = 0, it actually does nothing. When it is 100, this function is exactly the same as imagecopy() for palette images. It implements alpha transparency for true color images.

The above content is the entire content of this article introducing the encapsulation class of adding watermark, compression and cutting to images with PHP. I hope everyone is interested in this introduction.

For more related tutorials, please visit A complete set of video tutorials on PHP programming from entry to master

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template