php给图片加文字水印与图片水印代码
这款程序给图片加文字水印时是调用 了C:\WINDOWS\Fonts\\SIMHEI.TTF字体,给图片加水印时就可以自定图片,实例代码如下:
<?php $image->wprint_img(); //执行图片水印 $image->wprint_string(); //执行文字水印 class editimage { private $imagefile; //图片文件 private $smallimg; //水印图片 private $string; //水印文字 private $position; //存放位置 private $dst_x = 600; //原始图片打水印x坐标 private $dst_y = 0; //原始图片打水印y坐标 private $str_x = 450; private $str_y = 200; private $font = "c:windows ontssimhei.ttf"; //原始图片打水印字体路径 private $imgej; // imagecolorallocate后的变量 function __get($value) { return $this->$value; } function __set($property, $value) { $this->$property = $value; } /** * 构造函数初始化 * * @param string $imagefile 被上水印的文件 * @param string $smallimg 水印文件 * @param string $string 水印文字 * @param string $position 存放位置 * @param int $dst_x 被上水印的图片x * @param int $dst_y 被上水印的图片y */ function __construct($imagefile, $smallimg = '', $string = '') { //,$position='',$dst_x=0,$dst_y=0 $this->imagefile = $imagefile; $this->smallimg = $smallimg; $this->string = $string; $this->imgej = $this->imagecreatef($this->imagefile); } function get_extname($file) { //获取文件的后缀名 if (file_exists($this->imagefile)) { $img = getimagesize($file); switch ($img[2]) { case "1": return "gif"; case "2": return "jpg"; case "3": return "png"; } } else { return false; } } function getsize($file, $wh) { //获取图大小. $wh:w获得宽,h获得高 $image = getimagesize($file); if ($wh) { switch ($wh) { case "w": return $image[0]; case "h": return $image[1]; } } else { return false; } } function imagecreatef($file) { //创建类型 if ($this->get_extname($file)) { switch ($this->get_extname($file)) { case "gif": return imagecreatefromgif($file); case "jpg": return imagecreatefromjpeg($file); case "png": return imagecreatefrompng($file); } } else { echo "文件不存在"; } } //水印图片处理 function wprint_img() { if ($this->smallimg) { imagecopy($this->imgej, $this->imagecreatef($this->smallimg) , $this->dst_x, $this->dst_y, 0, 0, $this->getsize($this->smallimg, "w") , $this->getsize($this->smallimg, "h")); } else { return "水印图片不存在!"; } } //水印文字处理 function wprint_string() { return imagettftext($this->imgej, 20, 0, $this->str_x, $this->str_y, imagecolorallocate($this->imgej, 200, 200, 200) , $this->font, iconv("gb2312", "utf-8", $this->string)); } function choose_imgouttype() { //输出 if ($this->position) { $this->get_extname($this->imagefile); switch ($this->get_extname($this->imagefile)) { case "gif": return imagegif($this->imgej, $position); case "jpg": return imagejpeg($this->imgej, $this->position); case "jpeg": return imagejpeg($this->imgej, $this->position); case "png": return imagepng($this->imgej, $position); } } else { switch ($this->get_extname($this->imagefile)) { case "gif": return imagegif($this->imgej); case "jpg": return imagejpeg($this->imgej); case "jpeg": return imagejpeg($this->imgej); case "png": return imagepng($this->imgej); } } } } //使用方法如下: $image = new editimage("d90.gif", "hknmtt.png", "我的d90"); $image->wprint_img(); //执行图片水印 $image->wprint_string(); //执行文字水印,开源代码phprm.com $image->choose_imgouttype();
本文地址:http://www.phprm.com/tuxiang/fs4486.html
转载随意,但请附上文章地址:-)