Home > Backend Development > PHP Tutorial > Summary of examples of adding watermarks to images in PHP

Summary of examples of adding watermarks to images in PHP

高洛峰
Release: 2023-03-03 18:08:01
Original
1449 people have browsed it

The example in this article summarizes the function of adding watermark to PHP images. Share it with everyone for your reference, the details are as follows:

Example 1. Add a watermark function under a very simple picture

<?php
/**
 * 追加水印
 *
 * @param string $source_img 原始图片url
 * @param string $water_map 水印图片url
 */
function addWaterMap($source_img, $water_map)
{
  $img = imagecreatefromjpeg($source_img); //读取原图
  $img_x = imagesx($img); //原图宽
  $img_y = imagesy($img); //原图高
  imagealphablending($img, true);//设置为混合填色模式
  //$img_water_map = imagecreatefromjpeg($water_map); //读取水印图片
  $img_water_map = imagecreatefrompng($water_map);
  $water_x = imagesx($img_water_map); //水印宽
  $water_y = imagesy($img_water_map); //水印高
  $wimg_x = $img_x - $water_x - 10; //水印x坐标
  $wimg_y = $img_y - $water_y - 10; //水印y坐标
  imagecopy($img, $img_water_map, $wimg_x, $wimg_y, 0, 0, $water_x, $water_y); //分别为原图,水印,水印x坐标,水印y坐标,水印图片横轴开始点,水印图片纵轴开始点,水印横轴结束,水印纵轴结束
  imagejpeg($img, $source_img, 95); //输出到目标文件
  imagedestroy($img); //销毁内存数据流
  imagedestroy($img_water_map); //销毁内存数据流
  return true;
}
?>
Copy after login

Example 2. Add a watermark class to a relatively complete picture

<?php
/**
 * 图片加水印(适用于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[&#39;basename&#39;];
 $path = $temp[&#39;dirname&#39;];
 $exte = $temp[&#39;extension&#39;];
 $savename = $savename ? $savename : $name;
 $savepath = $savepath ? $savepath : $path;
 $savefile = $savepath .&#39;/&#39;. $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;
}
Copy after login

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

For more examples and summary of PHP image adding watermark function, please pay attention to PHP Chinese website for related articles!

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