PHP developed text watermarking thumbnail picture watermarking implementation class and usage examples

  • 2021-12-05 05:51:24
  • OfStack

In this paper, examples of PHP development of text watermarking, thumbnail, picture watermarking implementation class and usage. Share it for your reference, as follows:

1. Reference code for implementing class ImageToTest. class. php


class ImageToTest {
  /**
   *  Basic information of pictures 
   */
  private $info;
  private $image;
  public function __construct($src){
    $info = getimagesize($src);
    $this->info = array(
      'width'=> $info[0],
      'height'=> $info[1],
      'type'=> image_type_to_extension($info[2],false),
      'mime'=>$info['mime']
    );
    $fun = "imagecreatefrom{$this->info['type']}";
    $this->image = $fun($src);
  }
  /**
   *  Operation picture   (Compressed) 
   */
  public function thumb($width,$height){
    $image_thumb = imagecreatetruecolor($width,$height);
    imagecopyresampled($image_thumb,$this->image,0,0,0,0,$width,$height,
      $this->info['width'],$this->info['height']);
    imagedestroy($this->image);
    $this->image = $image_thumb;
  }
  /**
   *  Manipulate pictures (add text watermark) 
   */
  public function fontMark($content,$font_url,$size,$color,$local,$angle){
    $col = imagecolorallocatealpha($this->image,$color[0],$color[1],$color[2],$color[3]);
    imagettftext($this->image,$size,$angle,$local['x'],$local['y'],$col,$font_url,$content);
  }
  /**
   *  Manipulate pictures (add watermarked pictures) 
   */
  public function imageMark($source,$local,$alpha){
    //1. Get the basic information of watermark image 
    $info2 = getimagesize($source);
    //2. The image type of the watermark is obtained by the image number of the watermark 
    $type2 = image_type_to_extension($info2[2],false);
    //3. Create in memory 1 And our watermark image 1 Image type of 
    $func2 = "imagecreatefrom{$type2}";
    //4. Copy the watermark image into memory 
    $water = $func2($source);
    //5. Merge pictures 
    imagecopymerge($this->image,$water,$local['x'],$local['y'],0,0,$info2[0],$info2[1],$alpha);
    //6. Destroy watermark picture 
    imagedestroy($water);
  }
  /**
   *  Output pictures in the browser 
   */
  public function show(){
    header("Content-type:".$this->info['mime']);
    $funs = "image{$this->info['type']}";
    $funs($this->image);
  }
  /**
   *  Save the picture to the hard disk 
   */
  public function save($newName){
    $funs = "image{$this->info['type']}";
    $funs($this->image,'./outPut/'.$newName.'.'.$this->info['type']);
  }
  /**
   *  Destroy pictures   Use a destructor 
   */
  public function __destruct()
  {
    imagedestroy($this->image);
  }
}

2. Test the reference code


require_once('ImageToTest.class.php');
/*$src = './image/wbg.jpg';
$image = new ImageToTest($src);
$image->thumb(700,550);
$image->show();*/
/*$src2 = './image/wbg.jpg';
$content = 'SGC';
$font_url = './image/YGYcuhei.ttf';
$size = 33;
$color = array(
  0=>2,
  1=>222,
  2=>222,
  3=>60
);
$local = array(
  'x'=>20,
  'y'=>100
);
$angle = 10;
$image2 = new ImageToTest($src2);
$image2->fontMark($content,$font_url,$size,$color,$local,$angle);
$image2->show();
$image2->save('hahahah');*/
$src3 = './image/wbg.jpg';
$source = './image/water.jpg';
$local = array(
  'x'=>20,
  'y'=>100
);
$font_url = './image/YGYcuhei.ttf';
$size = 38;
$color = array(
  0=>2,
  1=>222,
  2=>222,
  3=>60
);
$alpha = 60;
$angle = 50;
$image3 = new ImageToTest($src3);
$image3->imageMark($source,$local,$alpha);
$image3->thumb(700,550);
$image3->fontMark('Hello',$font_url,$size,$color,$local,$angle);
$image3->show();
$image3->save('WAWAWAWAWA');

For more readers interested in PHP related contents, please check the special topics of this site: "Summary of PHP Graphics and Pictures Operation Skills", "PHP Array (Array) Operation Skills", "PHP Data Structure and Algorithm Tutorial", "php Programming Algorithm Summary", "PHP Mathematical Operation Skills Summary", "php String (string) Usage Summary" and "php Common Database Operation Skills Summary"

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


Related articles: