一直在做有关php文字图片上传方面的工作,所以把此功能函数整理了一次,现在分享给大家.

<?php
class image {
var $g_img;
var $g_w;
var $g_h;
var $img;
var $save_name;
var $g_type;
var $pos;
var $w_img;
var $transition;
var $jpeg_quality;
var $water_margin;
var $text;
var $text_size;
var $text_font;
var $text_color;
var $text_angle = 0;
var $t_x = 0;
var $t_y = 0;
var $make = true;
var $water_min_wh=0;
function image($g_img,$pos=0,$w_img=0,$transition=0,$jpeg_quality=0,$text=0,$text_size=0,$text_font=0,$text_color=0,$water_margin=0){
$this->g_img = $g_img;
$info = getimagesize($this->g_img);
if(!$info) {
$this->make = false;
return false;
}
$this->g_type = $info[2];
if($this->g_type == 1 && (!function_exists('imagegif') || !function_exists('imagecreatefromgif'))) {
$this->make = false;
return false;
}
$this->img = $this->createimage($this->g_type, $this->g_img);
if(!$this->img) {
$this->make = false;
return false;
}
$this->g_w = $info[0];
$this->g_h = $info[1];
if($this->g_type == 1 || $this->g_type == 3) {
$tmp_im = imagecreatetruecolor($this->g_w, $this->g_h);
imagefill($tmp_im, 0, 0, imagecolorallocate($tmp_im, 255, 255, 255));
imagecopyresampled($tmp_im, $this->img, 0, 0, 0, 0, $this->g_w, $this->g_h, $this->g_w, $this->g_h);
$this->img = $tmp_im;
unset($tmp_im);
}
$this->pos=$pos?$pos:0;
$this->w_img =$w_img?$w_img:'watermark.png';
$this->transition=$transition?$transition:85;
$this->jpeg_quality=$jpeg_quality?$jpeg_quality:80;
$this->text=$text?$text:'http://anmo.dianqu.net';
$this->text_size=$text_size?$text_size:16;
$this->text_font= $text_font?$text_font:'angltrr.ttf';
$this->text_color=$text_color?$text_color:'#000000';
$this->water_margin=$water_margin?$water_margin:10;
} function waterimage($save_name = '',$water_min_wh) {
is_file($this->w_img) or $this->make = false;
if($water_min_wh) {
$min_wh = $water_min_wh;
if($this->g_w < $min_wh || $this->g_h < $min_wh) {
$this->make = false;
}
}
if(!$this->make) return false;
$this->save_name = $save_name ? $save_name : $this->g_img;
$info = getimagesize($this->w_img);
$w_img = $this->createimage($info[2], $this->w_img);
$w_w = $info[0];
$w_h = $info[1];
$temp_w_im = $this->get_pos('image', $w_w, $w_h);
$w_im_x = $temp_w_im["dest_x"];
$w_im_y = $temp_w_im["dest_y"];
if($info[2] == 1) {
imagecopymerge($this->img, $w_img, $w_im_x, $w_im_y, 0, 0, $w_w, $w_h, $this->transition);
} else {
$b_img = imagecreatetruecolor($this->g_w, $this->g_h);
imagecopy($b_img, $this->img, 0, 0, 0, 0, $this->g_w, $this->g_h);
imagecopy($b_img, $w_img, $w_im_x, $w_im_y, 0, 0, $w_w, $w_h);
$this->img = $b_img;
}
imagedestroy($w_img);
$this->save();
return true;
} function watertext($save_name = '',$water_min_wh) {
is_file($this->text_font) or $this->make = false;
if($water_min_wh) {
$min_wh = $water_min_wh;
if($this->g_w < $min_wh || $this->g_h < $min_wh) $this->make = false;
}
if(!$this->make) return false;
//$this->text = convert($this->text, DT_CHARSET, 'UTF-8');中文乱码处理
$this->save_name = $save_name ? $save_name : $this->g_img;
$temp_text = $this->get_pos('text');
$text_x = $temp_text['dest_x'];
$text_y = $temp_text['dest_y'];
if(preg_match("/([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])/i", $this->text_color, $color)) {
$red = hexdec($color[1]);
$green = hexdec($color[2]);
$blue = hexdec($color[3]);
$text_color = imagecolorallocate($this->img, $red, $green, $blue);
} else {
$text_color = imagecolorallocate($this->img, 255, 255, 255);
}
imagettftext($this->img, $this->text_size, $this->text_angle, $text_x, $text_y, $text_color, $this->text_font, $this->text);
$this->save();
return true;
} function thumb($w = 0, $h = 0, $t = 0, $save_name = '') {
if(!$this->make) return false;
if($w == $this->g_w && $h == $this->g_h) {
if($save_name && $this->g_img != $save_name) file_copy($this->g_img, $save_name);
return true;
}
$this->save_name = $save_name ? $save_name : $this->g_img;
if($t ? $this->resize($w, $h) : $this->cut($w, $h)) $this->save();
return true;
} function save(){
switch($this->g_type) {
case '1': imagegif($this->img, $this->save_name); break;
case '3': imagepng($this->img, $this->save_name); break;
default : imagejpeg($this->img, $this->save_name, $this->jpeg_quality); break;
}
} function cut($w = 0, $h = 0) {
if(!$w || !$h) return false;
$_w = intval($h*$this->g_w/$this->g_h);
$_h = intval($w*$this->g_h/$this->g_w);
$im = imagecreatetruecolor($w, $h);
if($w >= $this->g_w || $h >= $this->g_h) {
imagefill($im, 0, 0, imagecolorallocate($im, 255, 255, 255));
$x = $w >= $this->g_w ? -intval(($this->g_w - $w)/2) : 0;
$y = $h >= $this->g_h ? -intval(($this->g_h - $h)/2) : 0;
imagecopy($im, $this->img, $x, $y, 0, 0, $this->g_w, $this->g_h);
} else if($_w >= $w) {
$tb = imagecreatetruecolor($_w, $h);
$x = intval(($_w - $w)/2);
imagecopyresampled($tb, $this->img, 0, 0, 0, 0, $_w, $h, $this->g_w, $this->g_h);
imagecopy($im, $tb, 0, 0, $x, 0, $w, $h);
} else if($_w < $w) {
$tb = imagecreatetruecolor($w, $_h);
$y = intval(($_h - $h)/2);
imagecopyresampled($tb, $this->img, 0, 0, 0, 0, $w, $_h, $this->g_w, $this->g_h);
imagecopy($im, $tb, 0, 0, 0, $y, $w, $h);
} else {
return false;
}
$this->img = $im;
unset($im);
if(isset($tb)) unset($tb);
return true;
} function resize($w = 0, $h = 0) {
if(!$w || !$h) return false;
$_w = intval($h*$this->g_w/$this->g_h);
$_h = intval($w*$this->g_h/$this->g_w);
$im = imagecreatetruecolor($w, $h);
imagefill($im, 0, 0, imagecolorallocate($im, 255, 255, 255));
if($this->g_w < $w) {
if($this->g_h <= $h) {
$x = intval(($w - $this->g_w)/2);
$y = intval(($h - $this->g_h)/2);
imagecopy($im, $this->img, $x, $y, 0, 0, $this->g_w, $this->g_h);
} else if($this->g_h > $h) {
$tb = imagecreatetruecolor($_w, $h);
imagecopyresampled($tb, $this->img, 0, 0, 0, 0, $_w, $h, $this->g_w, $this->g_h);
$x = intval(($w - $_w)/2);
imagecopy($im, $tb, $x, 0, 0, 0, $_w, $h);
}
} else if($this->g_w > $w) {
if($this->g_h <= $h) {
$tb = imagecreatetruecolor($w, $_h);
imagecopyresampled($tb, $this->img, 0, 0, 0, 0, $w, $_h, $this->g_w, $this->g_h);
$y = intval(($h - $_h)/2);
imagecopy($im, $tb, 0, $y, 0, 0, $w, $_h);
} else if($this->g_h > $h) {
if($_w >= $w) {
$tb = imagecreatetruecolor($w, $_h);
imagecopyresampled($tb, $this->img, 0, 0, 0, 0, $w, $_h, $this->g_w, $this->g_h);
$y = intval(($h - $_h)/2);
imagecopy($im, $tb, 0, $y, 0, 0, $w, $_h);
} else {
$tb = imagecreatetruecolor($_w, $h);
imagecopyresampled($tb, $this->img, 0, 0, 0, 0, $_w, $h, $this->g_w, $this->g_h);
$x = intval(($w - $_w)/2);
imagecopy($im, $tb, $x, 0, 0, 0, $_w, $h);
}
}
} else if($this->g_w = $w) {
if($this->g_h <= $h) {
$y = intval(($h - $this->g_h)/2);
imagecopy($im, $this->img, 0, $y, 0, 0, $this->g_w, $this->g_h);
} else if($this->g_h > $h) {
$tb = imagecreatetruecolor($_w, $h);
imagecopyresampled($tb, $this->img, 0, 0, 0, 0, $_w, $h, $this->g_w, $this->g_h);
$x = intval(($w - $_w)/2);
imagecopy($im, $tb, $x, 0, 0, 0, $_w, $h);
}
} else {
return false;
}
$this->img = $im;
unset($im);
if(isset($tb)) unset($tb);
return true;
} function createimage($type, $img_name) {
if($type == 1) {
return imagecreatefromgif($img_name);
} else if($type == 2) {
return imagecreatefromjpeg($img_name);
} else if($type == 3) {
return imagecreatefrompng($img_name);
}
return false;
} function get_pos($type, $p_w = 0, $p_h = 0) {
if($type == 'text') {
$line = count(explode("\n",$this->text));
$temp = imagettfbbox($this->text_size, $this->text_angle, $this->text_font, $this->text);
$p_w = $temp[2] - $temp[6];
$p_h = $line*($temp[3] - $temp[7]);
unset($temp);
}
if(($this->g_w < $p_w) || ($this->g_h < $p_h)) return false;
switch($this->pos) {
case 1:
$p_x = $this->water_margin;
$p_y = ($type == 'image' ? $this->water_margin : $p_h) + $this->water_margin;
break;
case 2:
$p_x = ($this->g_w - $p_w) / 2;
$p_y = ($type == 'image' ? 0 : $p_h) + $this->water_margin;
break;
case 3:
$p_x = $this->g_w - $p_w - $this->water_margin;
$p_y = ($type == 'image' ? 0 : $p_h) + $this->water_margin;
break;
case 4:
$p_x = $this->water_margin;
$p_y = ($this->g_h - $p_h) / 2;
break;
case 5:
$p_x = ($this->g_w - $p_w) / 2;
$p_y = ($this->g_h - $p_h) / 2;
break;
case 6:
$p_x = $this->g_w - $p_w - $this->water_margin;
$p_y = ($this->g_h - $p_h) / 2;
break;
case 7:
$p_x = $this->water_margin;
$p_y = $this->g_h - $p_h - $this->water_margin;
break;
case 8:
$p_x = ($this->g_w - $p_w) / 2;
$p_y = $this->g_h - $p_h - $this->water_margin;
break;
case 9:
$p_x = $this->g_w - $p_w - $this->water_margin;
$p_y = $this->g_h - $p_h - $this->water_margin;
break;
default:
$p_x = rand($this->water_margin, ($this->g_w - $p_w - $this->water_margin));
$p_y = rand($this->water_margin, ($this->g_h - $p_h - $this->water_margin));
break;
}
return array('dest_x'=>$p_x, 'dest_y'=>$p_y);
}
}
?>

php文字、图片水印功能函数封装的更多相关文章

  1. php 使用GD库压缩图片,添加文字图片水印

    先上一个工具类,提供了压缩,添加文字.图片水印等方法: image.class.php <?php class Image { private $info; private $image; pu ...

  2. php使用GD库实现图片水印和缩略图——封装成类

    学完了如何使用GD库来实现对图片的各种处理,那么我们可以发现,不管哪种方法,都有相似之处,如果我们把这些相似的地方和不相似的地方都封装成类,这样就可以提升代码的速度,而且节省了很多时间,废话不多说,来 ...

  3. asp.net生成缩略图、文字图片水印

    /// <summary> /// 会产生graphics异常的PixelFormat /// </summary> private static PixelFormat[] ...

  4. ASP.NET(C#)图片加文字、图片水印,神啊,看看吧

    ASP.NET(C#)图片加文字.图片水印 一.图片上加文字: //using System.Drawing; //using System.IO; //using System.Drawing.Im ...

  5. PHP加水印代码 支持文字和图片水印

    PHP加图片水印.文字水印类代码,PHP加水印类,支持文字图片水印的透明度设置.水印图片背景透明.自己写的一个类,因为自己开发的一套CMS中要用到,网上的总感觉用着不顺手,希望大家也喜欢这个类,后附有 ...

  6. 使用GD库做图片水印

    png图片作为水印加到其他类型图片后,背景变黑色 原因: imagecopy函数拷贝时可以保留png图像的原透明信息,而imagecopymerge却不支持图片的本身的透明拷贝. 然后直接上代码: / ...

  7. Swift - 给图片添加图片水印(图片上绘制另一张图,并可设透明度)

    我前面写了篇文章讲解如何给图片添加文字水印,而如果想要添加图片类型的水印也很简单,只要把原来代码里添加文字的部分改成图片即可. 1,效果图如下: (在图片左上角添加了一个半透明的logo图片) 2,为 ...

  8. PHP 图片水印类

    <?php /** * 加水印类,支持文字图片水印的透明度设置.水印图片背景透明. * $obj = new WaterMask($imgFileName); //实例化对象 * $obj-&g ...

  9. 本图片处理类功能非常之强大可以实现几乎所有WEB开发中对图像的处理功能都集成了,包括有缩放图像、切割图像、图像类型转换、彩色转黑白、文字水印、图片水印等功能

    import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.awt.Graphic ...

随机推荐

  1. 设置arc 的默认编辑器

    arc set-config editor "vim" 转自:http://udn.yyuap.com/thread-39791-1-1.html Pharicator是FB的代码 ...

  2. 基于iSCSI的SQL Server 2012群集测试(一)--SQL群集安装

    一.测试需求介绍与准备 公司计划服务器迁移过程计划同时上线SQL Server2012,引入SQL Server2012群集提高高可用性,需要对SQL Server2012群集技术进行研究.测试,确保 ...

  3. 代码质量管理工具 sonar 配置

    代码检查工具有很多findBugs等等 sonar配置: 1.下载sonar 5.5, 解压,运行 sonarqube-5.5\bin\windows-x86-64\StartSonar.bat , ...

  4. 《征服 C 指针》摘录4:函数 与 指针

    一.指向函数的指针 函数名可以在表达式中被解读成“指向函数的指针”,因此,正如代码清单 2-2 的实验那样,写成 func 就可以取得指向函数的指针. “指向函数的指针”本质上也是指针(地址),所以可 ...

  5. TypeError: test() got multiple values for keyword argument 'key'

    原因是: 1.函数调用的最终形式只会调用两个函数.一个list参数和一个dict参数,格式为call(func, list, dict); 2.如果传入参数中有key参数,那么首先key参数(包括扩展 ...

  6. java notepad++

  7. java16

    1:List的子类(掌握) (1)List的子类特点 ArrayList: 底层数据结构是数组,查询快,增删慢 线程不安全,效率高 Vector: 底层数据结构是数组,查询快,增删慢 线程安全,效率低 ...

  8. eclipse配置jdk的src.zip源代码步骤

    MyEclipse配置JDK的源代码的src.zip包很简单.只需要简单的几个步骤. 1.点 “window”-> “Preferences” -> “Java” -> “Insta ...

  9. AngularJS的JSONP服务

    有些页面的response中,包含了 Access-Control-Allow-Origin 这个header,说明可以进行跨域请求,如果没有包含这个header的页面可以利用JSONP进行跨域 sc ...

  10. python笔记1:python函数的使用

    一.函数:len() 1:作用:返回字符串.列表.字典.元组等长度 2:语法:len(str) 3:参数: str:要计算的字符串.列表.字典.元组等 4:返回值:字符串.列表.字典.元组等元素的长度 ...