PHP Picture Add Watermark Function Sample Summary _php tips

Source: Internet
Author: User

The example of this article summarizes the PHP image add watermark function. Share to everyone for your reference, specific as follows:

Example 1, very simple to add a watermark function under the diagram

<?php
/**
 * Append Watermark
 * *
 @param string $source _img original picture url
 * @param string $water _map watermark Picture URL
 * *
function Addwatermap ($source _img, $water _map)
{
  $img = imagecreatefromjpeg ($source _img);//Read the original
  $img _x = Imagesx ($img);//original width
  $img _y = Imagesy ($img); /Original High
  imagealphablending ($img, true);//set to mixed fill mode
  //$img _water_map = Imagecreatefromjpeg ($water _map); Read watermark picture
  $img _water_map = imagecreatefrompng ($water _map);
  $water _x = imagesx ($img _water_map); Watermark width
  $water _y = Imagesy ($img _water_map);//Watermark high
  $wimg _x = $img _x-$water _x-10;//watermark x coordinates
  $wimg _y = $img _y -$water _y-10;
  Watermark Picture Horizontal start point, watermark Picture longitudinal axis start point, watermark horizontal end, watermark longitudinal axis end
  imagejpeg ($img, $source _img, 95);//output to target file
  Imagedestroy ($img); Destroy memory data stream
  Imagedestroy ($img _water_map);//Destroy Memory stream return
  true;
>

Example 2, a more complete picture to increase the watermark class

<?php/** * Image watermark (for png/jpg/gif format) * * @author FLYNETCN * * * @param $srcImg Original picture * @param $waterImg watermark Picture * @par AM $savepath Save path * @param $savename save name * @param $positon Watermark Location * 1: Top left, 2: Top right, 3: center, 4: Bottom left, 5: bottom Right * @p       Aram $alpha Transparency-0: completely transparent, 100: completely opaque * * @return Success-Add watermark to the new picture address * failed---1: The original file does not exist,-2: Watermark picture does not exist,-3: Original file image Object build failed * -4: Watermark file Image Object Establishment failure-5: New image Save after Watermark failed/function Img_water_mark ($SRCIMG, $WATERIMG, $savepath =null, $savename =null, $pos
 Iton=5, $alpha =30) {$temp = PathInfo ($SRCIMG);
 $name = $temp [' basename '];
 $path = $temp [' dirname '];
 $exte = $temp [' extension ']; $savename = $savename?
 $savename: $name; $savepath = $savepath?
 $savepath: $path; $savefile = $savepath. ' /'.
 $savename;
 $srcinfo = @getimagesize ($SRCIMG);
 if (! $srcinfo) {return-1;//The original file does not exist} $waterinfo = @getimagesize ($WATERIMG);
 if (! $waterinfo) {return-2;//watermark picture does not exist} $SRCIMGOBJ = Image_create_from_ext ($SRCIMG); if (! $srcImgObj) {return-3;//original file image object builtFailure} $WATERIMGOBJ = Image_create_from_ext ($WATERIMG);
 if (! $waterImgObj) {return-4;//Watermark file Image object build failed} switch ($positon) {//1 top left Case 1: $x = $y =0; 2 top Right Case 2: $x = $srcinfo [0]-$waterinfo [0]; $y = 0;
 Break 3 Center Case 3: $x = ($srcinfo [0]-$waterinfo [0])/2; $y = ($srcinfo [1]-$waterinfo [1])/2;
 Break 4 bottom left case 4: $x = 0; $y = $srcinfo [1]-$waterinfo [1];
 Break 5 bottom Right Case 5: $x = $srcinfo [0]-$waterinfo [0]; $y = $srcinfo [1]-$waterinfo [1];
 Break
 Default: $x = $y = 0;
 Imagecopymerge ($SRCIMGOBJ, $WATERIMGOBJ, $x, $y, 0, 0, $waterinfo [0], $waterinfo [1], $alpha);
 Switch ($srcinfo [2]) {case 1:imagegif ($SRCIMGOBJ, $savefile); Case 2:imagejpeg ($SRCIMGOBJ, $savefile);
 Break Case 3:imagepng ($SRCIMGOBJ, $savefile);
 Break default:return-5;
 Save failed} Imagedestroy ($SRCIMGOBJ);
 Imagedestroy ($WATERIMGOBJ);
return $savefile;
 The function Image_create_from_ext ($imgfile) {$info = getimagesize ($imgfile);
 $im = null; Switch ($info [2]) {Case 1: $im =imAgecreatefromgif ($imgfile);
 Break Case 2: $im =imagecreatefromjpeg ($imgfile);
 Break Case 3: $im =imagecreatefrompng ($imgfile);
 Break
return $im;

 }

For more information on PHP related content readers can view the site topics: "PHP graphics and pictures Operating skills summary", "Basic PHP Grammar Introductory Course", "PHP Operations and Operator Usage Summary", "PHP object-oriented Program Design Introductory Course", "PHP Network Programming Skills Summary", "PHP array (Array) Operations tips Encyclopedia, "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP Common database Operation tips Summary"

I hope this article will help you with the PHP program design.

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.