pdo文字水印类,验证码类,缩略图类,logo类

文字水印类

image.class.php

  1. <?php
  2. /**
  3. * webrx.cn qq:7031633
  4. * @author webrx
  5. * @copyright copyright (c) 2003-2014 webrx Inc. (http://www.webrx.cn)
  6. * @version 2014-9-7 [email protected]
  7. */
  8. class Image {
  9. private $f;
  10. private $i;
  11. private $path;
  12. public function __construct($i = ‘‘) {
  13. $this->f = dirname ( __FILE__ ) . ‘/f.ttf‘;
  14. if (empty ( $i )) {
  15. } else {
  16. $this->i = imagecreatefromjpeg ( $i );
  17. $this->path = $i;
  18. }
  19. }
  20. public function check($len = 4) {
  21. session_start ();
  22. header ( ‘content-type:image/png‘ );
  23. $fs = [
  24. ‘/a.ttf‘,
  25. ‘/b.ttf‘,
  26. ‘/f.ttf‘
  27. ];
  28. $font = dirname ( __FILE__ ) . $fs [mt_rand ( 0, 1 )];
  29. $w = 35 * $len;
  30. $h = 50;
  31. $this->i = imagecreatetruecolor ( $w, $h );
  32. $c = imagecolorallocatealpha ( $this->i, 0, 0, 0, 127 );
  33. // imagecolortransparent($i,$c);
  34. // imagefill($i,0,0,$c);
  35. imagefilledrectangle ( $this->i, 0, 0, $w, $h, $this->gc ( $this->i, ‘ffffff‘, mt_rand ( 0, 2 ) ) );
  36. $sss = ‘‘;
  37. for($j = 0; $j < $len; $j ++) {
  38. $st = $this->gs ( 1 );
  39. $sss .= $st;
  40. imagettftext ( $this->i, mt_rand ( 15, 25 ), mt_rand ( - 30, 30 ), $j * 35 + 10, mt_rand ( 28, 38 ), $this->gc ( $this->i ), $font, $st );
  41. }
  42. $_SESSION [‘code‘] = $sss;
  43. imagesetthickness ( $this->i, mt_rand ( 2, 8 ) );
  44. for($j = 0; $j < mt_rand ( 5, 10 ); $j ++) {
  45. imagefilledarc ( $this->i, mt_rand ( 0, $w ), mt_rand ( 0, $h ), mt_rand ( 0, $w ), mt_rand ( 0, $h ), mt_rand ( 0, 360 ), mt_rand ( 0, 360 ), $this->gc ( $this->i, ‘rand‘, mt_rand ( 100, 120 ) ), IMG_ARC_NOFILL );
  46. }
  47. for($j = 0; $j < 10; $j ++) {
  48. imagettftext ( $this->i, mt_rand ( 10, 15 ), mt_rand ( - 5, 5 ), mt_rand ( 0, $w ), mt_rand ( 0, $h ), $this->gc ( $this->i, ‘rand‘, mt_rand ( 100, 120 ) ), $font, $this->gs ( 1 ) );
  49. }
  50. imagepng ( $this->i );
  51. }
  52. public function gs($n = 4) {
  53. $s = ‘abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789‘;
  54. $t = ‘‘;
  55. for($i = 0; $i < $n; $i ++) {
  56. $t .= substr ( $s, mt_rand ( 0, strlen ( $s ) - 1 ), 1 );
  57. }
  58. return $t;
  59. }
  60. public function setImg($i) {
  61. $this->i = imagecreatefromjpeg ( $i );
  62. $this->path = $i;
  63. }
  64. public function text($s = 30, $t = ‘版权所有‘, $c = ‘rand‘, $a = 0, $p = 5, $f = true, $fn = ‘t_‘) {
  65. $ii = getimagesize ( $this->path );
  66. if ($ii [2] == 2) {
  67. if ($ii [0] > 300) {
  68. $pos = imagettfbbox ( $s, 0, $this->f, $t );
  69. $pad = 30;
  70. switch ($p) {
  71. case 1 : // 左上角
  72. $x = 0 - $pos [0] + $pad;
  73. $y = 0 - $pos [7] + $pad;
  74. break;
  75. case 2 : // 上边 水平中央
  76. $x = ($ii [0] - $pos [2]) / 2;
  77. $y = 0 - $pos [7] + $pad;
  78. break;
  79. case 3 :
  80. $x = $ii [0] - $pos [2] - $pad;
  81. $y = 0 - $pos [7] + $pad;
  82. break;
  83. case 4 :
  84. $x = 0 - $pos [0] + $pad;
  85. $y = ($ii [1] - $pos [6]) / 2;
  86. break;
  87. case 5 :
  88. $x = ($ii [0] - $pos [2]) / 2;
  89. $y = ($ii [1] - $pos [6]) / 2;
  90. break;
  91. case 6 :
  92. $x = $ii [0] - $pos [2] - $pad;
  93. $y = ($ii [1] - $pos [6]) / 2;
  94. break;
  95. case 7 :
  96. $x = 0 - $pos [0] + $pad;
  97. $y = $ii [1] - $pos [6] - $pad;
  98. break;
  99. case 8 :
  100. $x = ($ii [0] - $pos [2]) / 2;
  101. $y = $ii [1] - $pos [6] - $pad;
  102. break;
  103. case 9 :
  104. $x = $ii [0] - $pos [2] - $pad;
  105. $y = $ii [1] - $pos [6] - $pad;
  106. break;
  107. }
  108. imagettftext ( $this->i, $s, 0, $x, $y, $this->gc ( $this->i, $c, $a ), $this->f, $t );
  109. if ($f) {
  110. imagejpeg ( $this->i, $this->path );
  111. } else {
  112. $path = dirname ( $this->path ) . ‘/‘;
  113. $name = $fn . substr ( $this->path, strrpos ( $this->path, ‘/‘ ) + 1 );
  114. imagejpeg ( $this->i, $path . $name );
  115. }
  116. }
  117. }
  118. }
  119. public function logo($p = 5, $f = true, $fn = ‘logo_‘) {
  120. $ii = getimagesize ( $this->path );
  121. if ($ii [2] == 2) {
  122. if ($ii [0] > 300) {
  123. $w = $ii [0];
  124. $h = $ii [1];
  125. // 水银图标 logo.png 格式
  126. $logo = dirname ( __FILE__ ) . ‘/logo.png‘;
  127. $li = imagecreatefrompng ( $logo );
  128. $liw = imagesx ( $li );
  129. $lih = imagesy ( $li );
  130. $x = ($w - $liw) / 2;
  131. $y = ($h - $lih) / 2;
  132. $pad = 35;
  133. switch ($p) {
  134. case 1 :
  135. $x = 0 + $pad;
  136. $y = 0 + $pad;
  137. break;
  138. case 2 :
  139. $y = 0 + $pad;
  140. break;
  141. case 3 :
  142. $x = $w - $liw - $pad;
  143. $y = 0 + $pad;
  144. break;
  145. case 4 :
  146. $x = 0 + $pad;
  147. break;
  148. case 6 :
  149. $x = $w - $liw - $pad;
  150. break;
  151. case 7 :
  152. $x = 0 + $pad;
  153. $y = $h - $lih - $pad;
  154. break;
  155. case 8 :
  156. $y = $h - $lih - $pad;
  157. break;
  158. case 9 :
  159. $x = $w - $liw - $pad;
  160. $y = $h - $lih - $pad;
  161. break;
  162. }
  163. imagecopy ( $this->i, $li, $x, $y, 0, 0, $liw, $lih );
  164. if ($f) {
  165. imagejpeg ( $this->i, $this->path );
  166. } else {
  167. $path = dirname ( $this->path ) . ‘/‘;
  168. $name = $fn . substr ( $this->path, strrpos ( $this->path, ‘/‘ ) + 1 );
  169. imagejpeg ( $this->i, $path . $name );
  170. }
  171. imagedestroy ( $li );
  172. }
  173. }
  174. }
  175. public function thumb($f = false, $w = 220, $h = 0, $fn = ‘s_‘) {
  176. $ii = getimagesize ( $this->path );
  177. if ($ii [2] == 2) {
  178. if ($ii [0] > $w) {
  179. $sw = $ii [0];
  180. $sh = $ii [1];
  181. $h = $h == 0 ? $w / $sw * $sh : $h;
  182. $dst = imagecreatetruecolor ( $w, $h );
  183. imagecopyresampled ( $dst, $this->i, 0, 0, 0, 0, $w, $h, $sw, $sh );
  184. if ($f) {
  185. imagejpeg ( $dst, $this->path );
  186. } else {
  187. $path = dirname ( $this->path ) . ‘/‘;
  188. $name = $fn . substr ( $this->path, strrpos ( $this->path, ‘/‘ ) + 1 );
  189. imagejpeg ( $dst, $path . $name );
  190. }
  191. imagedestroy ( $dst );
  192. }
  193. }
  194. }
  195. public function gc($i, $c = ‘rand‘, $a = 0) {
  196. $color = ‘‘;
  197. switch ($c) {
  198. case ‘white‘ :
  199. $color = imagecolorallocatealpha ( $i, 255, 255, 255, $a );
  200. break;
  201. case ‘black‘ :
  202. $color = imagecolorallocatealpha ( $i, 0, 0, 0, $a );
  203. break;
  204. case ‘red‘ :
  205. $color = imagecolorallocatealpha ( $i, 255, 0, 0, $a );
  206. break;
  207. case ‘green‘ :
  208. $color = imagecolorallocatealpha ( $i, 0, 255, 0, $a );
  209. break;
  210. case ‘rand‘ :
  211. $color = imagecolorallocatealpha ( $i, mt_rand ( 0, 255 ), mt_rand ( 0, 255 ), mt_rand ( 0, 255 ), $a );
  212. break;
  213. default :
  214. $cc = str_split ( $c, 2 );
  215. $color = imagecolorallocatealpha ( $i, hexdec ( $cc [0] ), hexdec ( $cc [1] ), hexdec ( $cc [2] ), $a );
  216. break;
  217. }
  218. return $color;
  219. }
  220. public function __destruct() {
  221. if (isset ( $this->i )) {
  222. imagedestroy ( $this->i );
  223. }
  224. }
  225. }

类的调用及使用

  1. <?php
  2. include ‘inc/Image.class.php‘;
  3. $i=new Image(‘c:/a.jpg‘);
  4. $i->text();

给多张图片添加水印文字

  1. <?php
  2. include ‘inc/Image.class.php‘;
  3. $i=new Image(‘c:/a.jpg‘);
  4. $i->text();
  5. $i->setImg(‘c:/b.jpg‘);
  6. $i->text(50);
  7. $i->setImg(‘c:/c.jpg‘);
  8. $i->text(100);

验证码的调用

  1. <?php
  2. include ‘inc/Image.class.php‘;
  3. $i=new Image();
  4. $i->check(4);

缩略图类的调用,如果写200,100表示固定宽高,只写200,高度自适应,

  1. <?php
  2. include ‘inc/Image.class.php‘;
  3. $i=new Image();
  4. $i->setImg(‘c:/d.jpg‘);
  5. $i->thumb(false,200);

logo 图片放在图上的调用

  1. <?php
  2. include ‘inc/Image.class.php‘;
  3. $i=new Image();
  4. $i->setImg(‘c:/d.jpg‘);
  5. $i->logo();
  1. <?php
  2. include ‘inc/Image.class.php‘;
  3. $i=new Image();
  4. $i->setImg(‘c:/d.jpg‘);
  5. $i->logo(5,false); //5 表示位置,false表示新建一张图片

来自为知笔记(Wiz)

时间: 2024-10-19 03:32:29

pdo文字水印类,验证码类,缩略图类,logo类的相关文章

php生成验证码,缩略图及水印图的类分享

封装了一个类,可生成验证码,缩略图,及水印图,分享给大家 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

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

学完了如何使用GD库来实现对图片的各种处理,那么我们可以发现,不管哪种方法,都有相似之处,如果我们把这些相似的地方和不相似的地方都封装成类,这样就可以提升代码的速度,而且节省了很多时间,废话不多说,来人,上代码! 首先,先创建一个PHP文件:class.php(自定义) 我们知道,在 在原始图片中添加文字水印:http://www.cnblogs.com/finalanddistance/p/7243346.html 在原始图片中添加图片水印:http://www.cnblogs.com/fin

图片工具类, 图片水印,文字水印,缩放,补白等

import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.Image; import java.awt.geom.AffineTransform; import java.awt.image.AffineTransformOp; import java.awt.image.BufferedImage; import

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

import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Toolkit; import java.awt.color.ColorSpace; import java.awt.geom.AffineTransform;

.NET图片操作类,包含图片格式转换、图片缩放、 文字水印、图片水印、路径转换

using System;using System.Collections.Generic;using System.Text;using System.IO;using System.Drawing.Imaging;using System.Drawing;using System.Web;namespace ZC.Utils{  public  static class ImageHelper  { #region 图片格式转换      /// <summary>      /// 图片

php 图片操作类,支持生成缩略图,添加水印,上传缩略图

<?php class Image {     //类开始     public $originimage = ""; //源图片文件地址     public $imageext = ""; //源图片格式     public $thumbimage = ""; //缩略图文件存放地址     public $thumb_maxwidth = 1440; //缩略图最大宽度     public $thumb_maxheight = 9

【python】-- 类的多继承、经典类、新式类

继承知识点补充 在python还支持多继承,但是一般我们很少用,有些语言干脆就不支持多继承,有多继承,就会带来两个概念,经典类和新式类. 一.多继承 之前我们都是讲的单继承,那么什么是多继承呢?说白了,就是:子类可以继承多个父类,就叫多继承. class SchoolMember(object): #SchoolMember类 '''学校成员基类''' def tell(self): print("the schoolmeber is tell...") class School(ob

【C++缺省函数】 空类默认产生的6个类成员函数

1.缺省构造函数. 2.缺省拷贝构造函数. 3. 缺省析构函数. 4.缺省赋值运算符. 5.缺省取址运算符. 6. 缺省取址运算符 const. <span style="font-size:18px;"> class A { public: A(){}//缺省构造函数 A(const A&){}//拷贝构造函数 ~A(){}//析构函数 A&operator=(const A&){}//赋值运算符 A*operator&(){}//取址运算

Python3基础 类的组合 把类的实例化放到一个新类里面

镇场诗: 诚听如来语,顿舍世间名与利.愿做地藏徒,广演是经阎浮提. 愿尽吾所学,成就一良心博客.愿诸后来人,重现智慧清净体.------------------------------------------ code: class Girls: def __init__(self,num): self.num=num class OurClass: #组合,其他类的实例化,在这里类的实例化中进行 def __init__(self,boyNum,girlNum): self.boys=Boys