How to add watermark to pictures in PHP, _PHP tutorial

WBOY
Release: 2016-07-12 08:53:39
Original
1061 people have browsed it

How to add watermark to PHP images,

The example in this article describes how to add watermark to PHP images. Share it with everyone for your reference, the details are as follows:

<&#63;php
echo img_water_mark("2008112023204423477802.gif", "copyImg.png", $savepath=null, $savename="123.gif", $positon=2, $alpha=60);
/**
 * 图片加水印(适用于png/jpg/gif格式)
 * 
 * @author flynetcn
 *
 * @param $srcImg 原图片
 * @param $waterImg 水印图片
 * @param $savepath 保存路径
 * @param $savename 保存名字
 * @param $positon 水印位置 
 * 1:顶部居左, 2:顶部居右, 3:居中, 4:底部局左, 5:底部居右 
 * @param $alpha 透明度 -- 0:完全透明, 100:完全不透明
 * 
 * @return 成功 -- 加水印后的新图片地址
 *     失败 -- -1:原文件不存在, -2:水印图片不存在, -3:原文件图像对象建立失败-4:水印文件图像对象建立失败, -5:加水印后的新图片保存失败
 */
function img_water_mark($srcImg, $waterImg, $savepath=null, $savename=null, $positon=5, $alpha=30)
{
  $temp = pathinfo($srcImg);
  $name = $temp['basename'];
  $path = $temp['dirname'];
  $exte = $temp['extension'];
  $savename = $savename &#63; $savename : $name;
  $savepath = $savepath &#63; $savepath : $path;
  $savefile = $savepath .'/'. $savename;
  $srcinfo = @getimagesize($srcImg);
  if (!$srcinfo) {
    return -1; //原文件不存在
  }
  $waterinfo = @getimagesize($waterImg);
  if (!$waterinfo) {
    return -2; //水印图片不存在
  }
  $srcImgObj = image_create_from_ext($srcImg);
  if (!$srcImgObj) {
    return -3; //原文件图像对象建立失败
  }
  $waterImgObj = image_create_from_ext($waterImg);
  if (!$waterImgObj) {
    return -4; //水印文件图像对象建立失败
  }
  switch ($positon) {
  //1顶部居左
  case 1: $x=$y=0; break;
  //2顶部居右
  case 2: $x = $srcinfo[0]-$waterinfo[0]; $y = 0; break;
  //3居中
  case 3: $x = ($srcinfo[0]-$waterinfo[0])/2; $y = ($srcinfo[1]-$waterinfo[1])/2; break;
  //4底部居左
  case 4: $x = 0; $y = $srcinfo[1]-$waterinfo[1]; break;
  //5底部居右
  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); break;
  case 2: imagejpeg($srcImgObj, $savefile); break;
  case 3: imagepng($srcImgObj, $savefile); break;
  default: return -5; //保存失败
  }
  imagedestroy($srcImgObj);
  imagedestroy($waterImgObj);
  return $savefile;
}
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;
}
&#63;>

Copy after login

Readers who are interested in more PHP-related content can check out the special topics of this site: "Summary of PHP graphics and image operation skills", "Complete of PHP array (Array) operation skills", "Summary of PHP mathematical operation skills", "php Summary of Date and Time Usage", "Introduction Tutorial on PHP Object-Oriented Programming", "Summary of PHP String Usage", "Introduction Tutorial on PHP MySQL Database Operation" and "Summary of Common PHP Database Operation Skills"

I hope this article will be helpful to everyone in PHP programming.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1123796.htmlTechArticleHow to implement watermarking on PHP images. This article describes the implementation method of adding watermarks to PHP images. Share it with everyone for your reference, the details are as follows: phpecho img_water_mark("2008112023204423477...
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