PHP to add text to the image Watermark method Summary _php Tips

Source: Internet
Author: User
Tags image identifier imagecopy imagejpeg mixed strcmp unsupported

1: Process-oriented writing method

Specifies the picture path
$src = ' 001.png ';
Get picture information
$info = getimagesize ($SRC);
Gets the picture extension
$type = image_type_to_extension ($info [2],false);
Dynamic image import into memory
$fun = "imagecreatefrom{$type}";
$image = $fun (' 001.png ');
Specifies the font color
$col = Imagecolorallocatealpha ($image, 255,255,255,50);
Specifies the font content
$content = ' HelloWorld ';
Add text to the picture
imagestring ($image, 5,20,30, $content, $col);
Specifies the input type
header (' Content-type: '. $info [' MIME ']);
Dynamic output picture to the browser
$func = "image{$type}";
$func ($image);
Destroy picture
Imagedestroy ($image);

2: Object-oriented approach to implementation

Class Image_class {private $image;

  Private $info;
    /** * @param $src: Picture path * load picture into memory/function __construct ($src) {$info = getimagesize ($SRC);
    $type = Image_type_to_extension ($info [2],false);
    $this-> info = $info;
    $this->info[' type '] = $type;
    $fun = "Imagecreatefrom". $type;
  $this-> image = $fun ($SRC); /** * @param $fontsize: Font size * @param $x: The x position of the font in the picture * @param $y: The y position of the font in the picture * @param $color: The color of the font is a containing r
    GBA array * @param $text: What you want to add * manipulate the picture in memory, add a text watermark to the picture/Public function Fontmark ($fontsize, $x, $y, $color, $text) {
    $col = Imagecolorallocatealpha ($this->image, $color [0], $color [1], $color [2], $color [3]);
  Imagestring ($this->image, $fontsize, $x, $y, $text, $col);
    * * * Output picture to Browser/public function show () {header (' Content-type: ', $this-> info[' mime ')); $fun = ' image '.
    $this->info[' type '];
  $fun ($this->image); /** * Destroy Picture * * Function __destruct () {imagEdestroy ($this->image);
The call to class $obj = new Image_class (' 001.png ');
$obj->fontmark (20,20,30,array (255,255,255,60), ' hello ');

 $obj->show ();

3. Support to add a watermark to a picture and text in two ways . The picture supports gif,png,jpg three formats, and the watermark image supports PNG and GIF

function Setwater ($IMGSRC, $MARKIMG, $markText, $TextColor, $markPos, $fontType, $markType) {$srcInfo = @getimagesize ($
  IMGSRC);
  $SRCIMG _w = $srcInfo [0];
    
  $SRCIMG _h = $srcInfo [1]; 
      Switch ($srcInfo [2]) {Case 1: $srcim =imagecreatefromgif ($IMGSRC); 
    Break 
      Case 2: $srcim =imagecreatefromjpeg ($IMGSRC); 
    Break 
      Case 3: $srcim =imagecreatefrompng ($IMGSRC); 
    Break 
      Default:die ("Unsupported picture file type"); 
  Exit
    } if (!strcmp ($markType, "img")) {if (!file_exists ($markImg) | | | empty ($MARKIMG)) {return;
    } $markImgInfo = @getimagesize ($MARKIMG);
    $MARKIMG _w = $markImgInfo [0];
      
    $MARKIMG _h = $markImgInfo [1];
    if ($srcImg _w < $markImg _w | | | $srcImg _h < $MARKIMG _h) {return; 
        Switch ($markImgInfo [2]) {Case 1: $markim =imagecreatefromgif ($MARKIMG); 
      Break Case 2: $markim =imagecreatefromjpeg ($marKIMG); 
      Break 
        Case 3: $markim =imagecreatefrompng ($MARKIMG); 
      Break 
        Default:die ("Unsupported watermark picture file type"); 
    Exit
    } $logow = $markImg _w;
  $logoh = $MARKIMG _h;
    } if (!strcmp ($markType, "text")) {$fontSize = 16;
      if (!empty ($markText)) {if (!file_exists ($fontType)) {return;
    } else {return;
    $box = @imagettfbbox ($fontSize, 0, $fontType, $markText);
    $logow = Max ($box [2], $box [4])-min ($box [0], $box [6]);
  $logoh = Max ($box [1], $box [3])-min ($box [5], $box [7]);
  } if ($markPos = = 0) {$markPos = rand (1, 9);
      Switch ($markPos) {Case 1: $x = +5;
      $y = +5;
    Break
      Case 2: $x = ($srcImg _w-$logow)/2;
      $y = +5;
    Break
      Case 3: $x = $srcImg _w-$logow-5;
      $y = +15;
    Break
      Case 4: $x = +5;
      $y = ($srcImg _h-$logoh)/2;
    Break Case 5: $x = ($srcImg _w-$logow)/2;
      $y = ($srcImg _h-$logoh)/2;
    Break
      Case 6: $x = $srcImg _w-$logow-5;
      $y = ($srcImg _h-$logoh)/2;
    Break
      Case 7: $x = +5;
      $y = $srcImg _h-$logoh-5;
    Break
      Case 8: $x = ($srcImg _w-$logow)/2;
      $y = $srcImg _h-$logoh-5;
    Break
      Case 9: $x = $srcImg _w-$logow-5;
      $y = $srcImg _h-$logoh-5;
    Break 
      Default:die ("This position does not support");
  Exit
    
  $DST _img = @imagecreatetruecolor ($srcImg _w, $srcImg _h);
    
  Imagecopy ($dst _img, $SRCIM, 0, 0, 0, 0, $srcImg _w, $srcImg _h);
    if (!strcmp ($markType, "img")) {imagecopy ($dst _img, $markim, $x, $y, 0, 0, $logow, $logoh);
  Imagedestroy ($markim);
      
    } if (!strcmp ($markType, "text")) {$rgb = explode (', ', $TextColor);
    $color = Imagecolorallocate ($dst _img, $rgb [0], $rgb [1], $rgb [2]); Imagettftext ($dst _img, $fontSize, 0, $x, $y, $color, $fontType, $mArktext); 
      Switch ($srcInfo [2]) {case 1:imagegif ($dst _img, $IMGSRC); 
    Break 
      Case 2:imagejpeg ($dst _img, $IMGSRC); 
    Break 
      Case 3:imagepng ($dst _img, $IMGSRC);
    Break 
      Default:die ("Unsupported watermark picture file type"); 
  Exit
  } Imagedestroy ($dst _img);
Imagedestroy ($SRCIM);

 }

Parameter description:

$IMGSRC: Target picture, with relative directory address,
$MARKIMG: Watermark image, with relative directory address, PNG and GIF support two formats, such as watermark image in the execution file mark directory, can be written as: Mark/mark.gif
$markText: Watermark text added to a picture
$TextColor: Font color for watermark text
$markPos: Image Watermark added location, value range: 0~9
0: Random position, randomly select a position between the 1~8
1: Top left 2: Top Center 3: Top right 4: center left
5: Picture Center 6: Right Center 7: Bottom left 8: Bottom Center 9: Bottom Right
$fontType: A specific font library with a relative directory address
$markType: The way to add a watermark to the image, IMG represents the image, the text represents the text to add a watermark

4. The method of adding text watermark to Picture

<?php/* To the picture Plus text watermark method * * * $dst _path = ' http://f4.topitme.com/4/15/11/1166351597fe111154l.jpg ';
$DST = imagecreatefromstring (file_get_contents ($DST _path)); /*imagecreatefromstring ()--Creates a new image from the image stream in the string, returns an image identifier that expresses an image Image format from a given string that will automatically be monitored as long as PHP supports jpeg,png,gif,wbmp,gd2.*/$
Font = './t1.ttf ';
$black = imagecolorallocate ($dst, 0, 0, 0);
Imagefttext ($DST, 0, $black, $font, ' Hello world! '); /*imagefttext ($img, $size, $angle, $x, $y, $color, $fontfile, $text) $img the image resource size returned by the image creation function The font size of the watermark to be used angle (angle) text tilt angle , if it is 0 degrees to represent the text from left to right, if it is 90 degrees from the top down x,y watermark text from the beginning of the first text color is watermark text colors Fontfile, you want to use TrueType font path/list ($dst _w, $dst _h, $DST
_type) = getimagesize ($dst _path); /*list (mixed $varname [, mixed $ ...])
--Assign the values in the array to some variables like array (), which is not a real function, but a language structure, and the List () is assigned a set of variables in one operation.//*getimagesize () What information can be obtained?
    The GetImageSize function returns all information about the image, including size, type, etc. */switch ($DST _type) {Case 1://gif header ("Content-type:image/gif");
    Imagegif ($DST);
  Break
    Case 2://jpg Header ("Content-type:image/jpeg"); Imagejpeg ($DST);
  Break
    Case 3://png Header ("Content-type:image/png");
    Imagepng ($DST);
  Break
  Default:break;
/*imagepng--the image to the browser or file Imagepng () in PNG format to output the GD image stream (image) in PNG format to the callout output (usually the browser), or if the filename is given a file name, it is exported to the document/}
 Imagedestroy ($DST);?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.