Home  >  Article  >  Backend Development  >  PHP image file operation encapsulation class

PHP image file operation encapsulation class

墨辰丷
墨辰丷Original
2018-06-07 13:49:291496browse

This article mainly introduces the PHP image file operation encapsulation class. Interested friends can refer to it. I hope it will be helpful to everyone.

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 operate pictures:
1. Open the picture2. Manipulate the picture
3. Output the picture
4. Destroy the picture## Steps #1, 3, and 4 need to be written every time, and they are almost the same every time. The only thing that really needs to be changed is the step of manipulating images. Manipulating pictures is often done through one or more main GD functions.

<?php 

class Image
{  
  private $info;

  private $image;
  public $type;
  public function __construct($src)
  {

    $this->info=getimagesize($src);
    $this->type=image_type_to_extension($this->info[&#39;2&#39;],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[&#39;0&#39;], $col[&#39;1&#39;], $col[&#39;2&#39;],$col[&#39;3&#39;]);

    imagettftext($this->image, $size, $angle, $location[&#39;0&#39;], $location[&#39;1&#39;], $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[&#39;2&#39;],false);
    $func2="imagecreatefrom".$type;
    $water=$func2($imageMark);

    imagecopymerge($this->image, $water, $dst[0], $dst[1], 0, 0, $info2[&#39;0&#39;], $info2[&#39;1&#39;], $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[&#39;0&#39;], $this->info[&#39;1&#39;]);
    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[&#39;mime&#39;]);

    $funn="image".$this->type;

    $funn($this->image);
  }
  /**
   * 保存图片
 * @param [type] $newname 新图片名
 * @return [type]     [description]
 */
   public function save($newname){
     header("content-type:".$this->info[&#39;mime&#39;]);

     $funn="image".$this->type;

     $funn($this->image,$newname.&#39;.&#39;.$this->type);
   }
   public function __destruct(){
     imagedestroy($this->image);
   }

 }

 ?>

Summary

: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

php supports Chinese file download

Detailed explanation of Mongodb connection to remote database under PHP And cases

The principle of PHP text to image function

The above is the detailed content of PHP image file operation encapsulation class. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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