一直在做有关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. tyvj1097 mm不哭

    背景 Bless all rp++.. 描述 在一个数轴上,有n个MM(绝非恐龙!)在哭泣(5555~一直哭). tcboy也在这个数轴上,并恰好看到了这一幕,由于每个MM哭都会让tcboy损失一定的 ...

  2. UVALive 3644 X-Plosives

    X-Plosives Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu [Submit]   ...

  3. 禁用PHP函数,可以对php.ini进行配置

    php.ini 里有个 disable_functions 开关选项,此选项可关闭一些危险的函数,比如system,exec 等.比如: disable_functions = phpinfo , 如 ...

  4. php-css外边距

    css 基本语法  selector{declaration1;declaration2;....delecrationN;} (选择器和一条或多条声明)  选择器为需要改变样式的html元素,每条声 ...

  5. jquery版相片墙(鼠标控制图片聚合和散开)

    照片墙,简单点说就是鼠标点击小图片时,聚合变成一张大图片:点击大图片时,散开变成小图片.这个是我一年前无意间看到的动画效果(现在已经忘记是哪位大神制作的了,引用了他的图片),刚看到这个很炫的动画超级激 ...

  6. IOS单元测试,OCMock

    1.环境搭建 使用官方推荐,手动搭建.. 1.下载所需文件 http://ocmock.org/download/ 根据自己的需要下载不同的版本. 打开iOS library文件夹,里面就有需要用到的 ...

  7. debug [LTS]

    0613 A. 复制代码的时候忘了后续的对称的修改. 统计答案时出现了一些不可理喻的低级失误. B. 在0-indexed的程序中访问第一个元素使用了Arr[1]. Matrix-tree为mat[d ...

  8. PHP写的异步高并发服务器,基于libevent

    PHP写的异步高并发服务器,基于libevent 博客分类: PHP PHPFPSocketLinuxQQ  本文章于2013年11月修改. swoole已使用C重写作为PHP扩展来运行.项目地址:h ...

  9. ecshop二次开发 给商品添加自定义字段【包含我自己进一步的开发实例详解】

    本文包含商品自定义添加教程及进一步的开发实例: 教程: 说起自定义字段,我想很多的朋友像我一样会想起一些开源的CMS(比如Dedecms.Phpcms.帝国)等,他们是可以在后台直接添加自定义字段的. ...

  10. upload&&download

    package am.demo;  import java.io.File;  import java.io.IOException;  import java.util.Iterator;  imp ...