一直在做有关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. POJ2195 最小费用流

    题目:http://poj.org/problem?id=2195 处理出每个人到每个门的曼哈顿距离,分别建立容量为1费用为曼哈顿距离的边,在源点和每个人人之间建立容量为1费用为0的边,在门和汇点之间 ...

  2. command shell 的知识整理

    cmd 也是shell  windowns的外壳一种. 查看文件和文件夹 dir mkdir *** 创建文件夹 rd *** 删除文件夹(应该有参数的,递归之类的)CTR+C 终止命令 cd> ...

  3. uoj228 基础数据结构练习题

    趁别人题解没有放出来赶快写一篇 整数序列,操作 区间加 区间变成sqrt(下取整) 区间和 考虑一下对于每个区间里所有sqrt不同的段操作,那么可以在O(段数logn)一次的时间内完成sqrt操作.考 ...

  4. AdminLTE 2 开源模版

    AdminLTE  2 开源模版: 1. 文档  https://almsaeedstudio.com/themes/AdminLTE/documentation/index.html 2. 代码   ...

  5. Ubuntu 14.4 配置

    1.安装 Orcal Java 使用下面的命令安装,只需一些时间,它就会下载许多的文件,所及你要确保你的网络环境良好: sudo add-apt-repository ppa:webupd8team/ ...

  6. PHP实现四种基本排序算法

    前提:分别用冒泡排序法,快速排序法,选择排序法,插入排序法将下面数组中的值按照从小到大的顺序进行排序. $arr(1,43,54,62,21,66,32,78,36,76,39); 1. 冒泡排序 思 ...

  7. php curl 例子

    get方式: set_time_limit(0);        $url ='http://xxxxxxxx?pwd=wJ2312&s=1&e=2&d=1&t=asc ...

  8. intellij idea 插件 ideaVim 用法

    intellij idea 插件 ideaVim - Genji_ - 博客园http://www.cnblogs.com/nova-/p/3535636.html IdeaVim插件使用技巧 - - ...

  9. 带callback的future实现

    jdk暂时不支持,所以只有自己实现带callback的future. 完成后callback的 final TaskPromise promise = new DefaultTaskPromise() ...

  10. PHP "万能"输出随机字符串

    <?php function getRandomCode ($length = 32, $mode = 0){ switch ($mode) { case "1": $str ...