功能强大的PHP图片处理类(水印、透明度、旋转)

这篇文章主要汇总介绍了php图片处理类(水印、透明度、缩放、锐化),非常的简单实用,有需要的小伙伴可以参考下。

非常强大的php图片处理类,可以自定义图片水印、透明度、图片缩放、图片锐化、图片旋转、图片翻转、图片剪切、图片反色。

* 图片处理函数功能:缩放、剪切、相框、水印、锐化、旋转、翻转、透明度、反色

* 处理并保存历史记录的思路:当有图片有改动时自动生成一张新图片,命名方式可以考虑在原图片的基础上加上步骤,例如:图片名称+__第几步

具体代码如下:

  1. <?php
  2. class picture{
  3. var $PICTURE_URL; //要处理的图片
  4. var $DEST_URL = "temp__01.jpg"; //生成目标图片位置
  5. var $PICTURE_CREATE; //要创建的图片
  6. var $TURE_COLOR; //新建一个真彩图象
  7. var $PICTURE_WIDTH; //原图片宽度
  8. var $PICTURE_HEIGHT; //原图片高度
  9. /**
  10. * 水印的类型,默认的为水印文字
  11. */
  12. var $MARK_TYPE = 1;
  13. var $WORD; //经过UTF-8后的文字
  14. var $WORD_X; //文字横坐标
  15. var $WORD_Y; //文字纵坐标
  16. var $FONT_TYPE; //字体类型
  17. var $FONT_SIZE = "12"; //字体大小
  18. var $FONT_WORD; //文字
  19. var $ANGLE = 0; //文字的角度,默认为0
  20. var $FONT_COLOR = "#000000"; //文字颜色
  21. var $FONT_PATH = "font/simkai.ttf"; //字体库,默认为宋体
  22. var $FORCE_URL; //水印图片
  23. var $FORCE_X = 0; //水印横坐标
  24. var $FORCE_Y = 0; //水印纵坐标
  25. var $FORCE_START_X = 0; //切起水印的图片横坐标
  26. var $FORCE_START_Y = 0; //切起水印的图片纵坐标
  27. var $PICTURE_TYPE; //图片类型
  28. var $PICTURE_MIME; //输出的头部
  29. /**
  30. * 缩放比例为1的话就按缩放高度和宽度缩放
  31. */
  32. var $ZOOM = 1; //缩放类型
  33. var $ZOOM_MULTIPLE; //缩放比例
  34. var $ZOOM_WIDTH; //缩放宽度
  35. var $ZOOM_HEIGHT; //缩放高度
  36. /**
  37. * 裁切,按比例和固定长度、宽度
  38. */
  39. var $CUT_TYPE = 1; //裁切类型
  40. var $CUT_X = 0; //裁切的横坐标
  41. var $CUT_Y = 0; //裁切的纵坐标
  42. var $CUT_WIDTH = 100; //裁切的宽度
  43. var $CUT_HEIGHT = 100; //裁切的高度
  44. /**
  45. * 锐化
  46. */
  47. var $SHARP = "7.0"; //锐化程度
  48. /**
  49. * 透明度处理
  50. */
  51. var $ALPHA = '100'; //透明度在0-127之间
  52. var $ALPHA_X = "90";
  53. var $ALPHA_Y = "50";
  54. /**
  55. * 任意角度旋转
  56. */
  57. var $CIRCUMROTATE = "90.0"; //注意,必须为浮点数
  58. /**
  59. * 出错信息
  60. */
  61. var $ERROR = array(
  62. 'unalviable' => '没有找到相关图片!'
  63. );
  64. /**
  65. * 构造函数:函数初始化
  66. */
  67. function __construct($PICTURE_URL){
  68. $this -> get_info($PICTURE_URL);
  69. }
  70. function get_info($PICTURE_URL){
  71. /**
  72. * 处理原图片的信息,先检测图片是否存在,不存在则给出相应的信息
  73. */
  74. @$SIZE = getimagesize($PICTURE_URL);
  75. if(!$SIZE){
  76. exit($this -> ERROR['unalviable']);
  77. }
  78. // 得到原图片的信息类型、宽度、高度
  79. $this -> PICTURE_MIME = $SIZE['mime'];
  80. $this -> PICTURE_WIDTH = $SIZE[0];
  81. $this -> PICTURE_HEIGHT = $SIZE[1];
  82. // 创建图片
  83. switch($SIZE[2]){
  84. case 1:
  85. $this -> PICTURE_CREATE = imagecreatefromgif($PICTURE_URL);
  86. $this -> PICTURE_TYPE = "imagejpeg";
  87. $this -> PICTURE_EXT = "jpg";
  88. break;
  89. case 2:
  90. $this -> PICTURE_CREATE = imagecreatefromjpeg($PICTURE_URL);
  91. $this -> PICTURE_TYPE = "imagegif";
  92. $this -> PICTURE_EXT = "gif";
  93. break;
  94. case 3:
  95. $this -> PICTURE_CREATE = imagecreatefrompng($PICTURE_URL);
  96. $this -> PICTURE_TYPE = "imagepng";
  97. $this -> PICTURE_EXT = "png";
  98. break;
  99. }
  100. /**
  101. * 文字颜色转换16进制转换成10进制
  102. */
  103. preg_match_all("/([0-f]){2,2}/i", $this -> FONT_COLOR, $MATCHES);
  104. if(count($MATCHES) == 3){
  105. $this -> RED = hexdec($MATCHES[0][0]);
  106. $this -> GREEN = hexdec($MATCHES[0][1]);
  107. $this -> BLUE = hexdec($MATCHES[0][2]);
  108. }
  109. }
  110. # end of __construct
  111. /**
  112. * 将16进制的颜色转换成10进制的(R,G,B)
  113. */
  114. function hex2dec(){
  115. preg_match_all("/([0-f]){2,2}/i", $this -> FONT_COLOR, $MATCHES);
  116. if(count($MATCHES) == 3){
  117. $this -> RED = hexdec($MATCHES[0][0]);
  118. $this -> GREEN = hexdec($MATCHES[0][1]);
  119. $this -> BLUE = hexdec($MATCHES[0][2]);
  120. }
  121. }
  122. // 缩放类型
  123. function zoom_type($ZOOM_TYPE){
  124. $this -> ZOOM = $ZOOM_TYPE;
  125. }
  126. // 对图片进行缩放,如果不指定高度和宽度就进行缩放
  127. function zoom(){
  128. // 缩放的大小
  129. if($this -> ZOOM == 0){
  130. $this -> ZOOM_WIDTH = $this -> PICTURE_WIDTH * $this -> ZOOM_MULTIPLE;
  131. $this -> ZOOM_HEIGHT = $this -> PICTURE_HEIGHT * $this -> ZOOM_MULTIPLE;
  132. }
  133. // 新建一个真彩图象
  134. $this -> TRUE_COLOR = imagecreatetruecolor($this -> ZOOM_WIDTH, $this -> ZOOM_HEIGHT);
  135. $WHITE = imagecolorallocate($this -> TRUE_COLOR, 255, 255, 255);
  136. imagefilledrectangle($this -> TRUE_COLOR, 0, 0, $this -> ZOOM_WIDTH, $this -> ZOOM_HEIGHT, $WHITE);
  137. imagecopyresized($this -> TRUE_COLOR, $this -> PICTURE_CREATE, 0, 0, 0, 0, $this -> ZOOM_WIDTH, $this -> ZOOM_HEIGHT, $this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT);
  138. }
  139. # end of zoom
  140. // 裁切图片,按坐标或自动
  141. function cut(){
  142. $this -> TRUE_COLOR = imagecreatetruecolor($this -> CUT_WIDTH, $this -> CUT_WIDTH);
  143. imagecopy($this -> TRUE_COLOR, $this -> PICTURE_CREATE, 0, 0, $this -> CUT_X, $this -> CUT_Y, $this -> CUT_WIDTH, $this -> CUT_HEIGHT);
  144. }
  145. # end of cut
  146. /**
  147. * 在图片上放文字或图片
  148. * 水印文字
  149. */
  150. function _mark_text(){
  151. $this -> TRUE_COLOR = imagecreatetruecolor($this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT);
  152. $this -> WORD = mb_convert_encoding($this -> FONT_WORD, 'utf-8', 'gb2312');
  153. /**
  154. * 取得使用 TrueType 字体的文本的范围
  155. */
  156. $TEMP = imagettfbbox($this -> FONT_SIZE, 0, $this -> FONT_PATH, $this -> WORD);
  157. $WORD_LENGTH = strlen($this -> WORD);
  158. $WORD_WIDTH = $TEMP[2] - $TEMP[6];
  159. $WORD_HEIGHT = $TEMP[3] - $TEMP[7];
  160. /**
  161. * 文字水印的默认位置为右下角
  162. */
  163. if($this -> WORD_X == ""){
  164. $this -> WORD_X = $this -> PICTURE_WIDTH - $WORD_WIDTH;
  165. }
  166. if($this -> WORD_Y == ""){
  167. $this -> WORD_Y = $this -> PICTURE_HEIGHT - $WORD_HEIGHT;
  168. }
  169. imagesettile($this -> TRUE_COLOR, $this -> PICTURE_CREATE);
  170. imagefilledrectangle($this -> TRUE_COLOR, 0, 0, $this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT, IMG_COLOR_TILED);
  171. $TEXT2 = imagecolorallocate($this -> TRUE_COLOR, $this -> RED, $this -> GREEN, $this -> Blue);
  172. imagettftext($this -> TRUE_COLOR, $this -> FONT_SIZE, $this -> ANGLE, $this -> WORD_X, $this -> WORD_Y, $TEXT2, $this -> FONT_PATH, $this -> WORD);
  173. }
  174. /**
  175. * 水印图片
  176. */
  177. function _mark_picture(){
  178. /**
  179. * 获取水印图片的信息
  180. */
  181. @$SIZE = getimagesize($this -> FORCE_URL);
  182. if(!$SIZE){
  183. exit($this -> ERROR['unalviable']);
  184. }
  185. $FORCE_PICTURE_WIDTH = $SIZE[0];
  186. $FORCE_PICTURE_HEIGHT = $SIZE[1];
  187. // 创建水印图片
  188. switch($SIZE[2]){
  189. case 1:
  190. $FORCE_PICTURE_CREATE = imagecreatefromgif($this -> FORCE_URL);
  191. $FORCE_PICTURE_TYPE = "gif";
  192. break;
  193. case 2:
  194. $FORCE_PICTURE_CREATE = imagecreatefromjpeg($this -> FORCE_URL);
  195. $FORCE_PICTURE_TYPE = "jpg";
  196. break;
  197. case 3:
  198. $FORCE_PICTURE_CREATE = imagecreatefrompng($this -> FORCE_URL);
  199. $FORCE_PICTURE_TYPE = "png";
  200. break;
  201. }
  202. /**
  203. * 判断水印图片的大小,并生成目标图片的大小,如果水印比图片大,则生成图片大小为水印图片的大小。否则生成的图片大小为原图片大小。
  204. */
  205. $this -> NEW_PICTURE = $this -> PICTURE_CREATE;
  206. if($FORCE_PICTURE_WIDTH > $this -> PICTURE_WIDTH){
  207. $CREATE_WIDTH = $FORCE_PICTURE_WIDTH - $this -> FORCE_START_X;
  208. }else{
  209. $CREATE_WIDTH = $this -> PICTURE_WIDTH;
  210. }
  211. if($FORCE_PICTURE_HEIGHT > $this -> PICTURE_HEIGHT){
  212. $CREATE_HEIGHT = $FORCE_PICTURE_HEIGHT - $this -> FORCE_START_Y;
  213. }else{
  214. $CREATE_HEIGHT = $this -> PICTURE_HEIGHT;
  215. }
  216. /**
  217. * 创建一个画布
  218. */
  219. $NEW_PICTURE_CREATE = imagecreatetruecolor($CREATE_WIDTH, $CREATE_HEIGHT);
  220. $WHITE = imagecolorallocate($NEW_PICTURE_CREATE, 255, 255, 255);
  221. /**
  222. * 将背景图拷贝到画布中
  223. */
  224. imagecopy($NEW_PICTURE_CREATE, $this -> PICTURE_CREATE, 0, 0, 0, 0, $this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT);
  225. /**
  226. * 将目标图片拷贝到背景图片上
  227. */
  228. imagecopy($NEW_PICTURE_CREATE, $FORCE_PICTURE_CREATE, $this -> FORCE_X, $this -> FORCE_Y, $this -> FORCE_START_X, $this -> FORCE_START_Y, $FORCE_PICTURE_WIDTH, $FORCE_PICTURE_HEIGHT);
  229. $this -> TRUE_COLOR = $NEW_PICTURE_CREATE;
  230. }
  231. # end of mark
  232. function alpha_(){
  233. $this -> TRUE_COLOR = imagecreatetruecolor($this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT);
  234. $rgb = "#CDCDCD";
  235. $tran_color = "#000000";
  236. for($j = 0;$j <= $this -> PICTURE_HEIGHT-1;$j++){
  237. for ($i = 0;$i <= $this -> PICTURE_WIDTH-1;$i++)
  238. {
  239. $rgb = imagecolorat($this -> PICTURE_CREATE, $i, $j);
  240. $r = ($rgb >> 16) & 0xFF;
  241. $g = ($rgb >> 8) & 0xFF;
  242. $b = $rgb & 0xFF;
  243. $now_color = imagecolorallocate($this -> PICTURE_CREATE, $r, $g, $b);
  244. if ($now_color == $tran_color)
  245. {
  246. continue;
  247. }
  248. else
  249. {
  250. $color = imagecolorallocatealpha($this -> PICTURE_CREATE, $r, $g, $b, $ALPHA);
  251. imagesetpixel($this -> PICTURE_CREATE, $ALPHA_X + $i, $ALPHA_Y + $j, $color);
  252. }
  253. $this -> TRUE_COLOR = $this -> PICTURE_CREATE;
  254. }
  255. }
  256. }
  257. /**
  258. * 图片旋转:
  259. * 沿y轴旋转
  260. */
  261. function turn_y(){
  262. $this -> TRUE_COLOR = imagecreatetruecolor($this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT);
  263. for ($x = 0; $x < $this -> PICTURE_WIDTH; $x++)
  264. {
  265. imagecopy($this -> TRUE_COLOR, $this -> PICTURE_CREATE, $this -> PICTURE_WIDTH - $x - 1, 0, $x, 0, 1, $this -> PICTURE_HEIGHT);
  266. }
  267. }
  268. /**
  269. * 沿X轴旋转
  270. */
  271. function turn_x(){
  272. $this -> TRUE_COLOR = imagecreatetruecolor($this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT);
  273. for ($y = 0; $y < $this -> PICTURE_HEIGHT; $y++){
  274. imagecopy($this -> TRUE_COLOR, $this -> PICTURE_CREATE, 0, $this -> PICTURE_HEIGHT - $y - 1, 0, $y, $this -> PICTURE_WIDTH, 1);
  275. }
  276. }
  277. /**
  278. * 任意角度旋转
  279. */
  280. function turn(){
  281. $this -> TRUE_COLOR = imagecreatetruecolor($this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT);
  282. imageCopyResized($this -> TRUE_COLOR, $this -> PICTURE_CREATE, 0, 0, 0, 0, $this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT, $this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT);
  283. $WHITE = imagecolorallocate($this -> TRUE_COLOR, 255, 255, 255);
  284. $this -> TRUE_COLOR = imagerotate ($this -> TRUE_COLOR, $this -> CIRCUMROTATE, $WHITE);
  285. }
  286. /**
  287. * 图片锐化
  288. */
  289. function sharp(){
  290. $this -> TRUE_COLOR = imagecreatetruecolor($this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT);
  291. $cnt = 0;
  292. for ($x = 0; $x < $this -> PICTURE_WIDTH; $x++){
  293. for ($y = 0; $y < $this -> PICTURE_HEIGHT; $y++){
  294. $src_clr1 = imagecolorsforindex($this -> TRUE_COLOR, imagecolorat($this -> PICTURE_CREATE, $x-1, $y-1));
  295. $src_clr2 = imagecolorsforindex($this -> TRUE_COLOR, imagecolorat($this -> PICTURE_CREATE, $x, $y));
  296. $r = intval($src_clr2["red"] + $this -> SHARP * ($src_clr2["red"] - $src_clr1["red"]));
  297. $g = intval($src_clr2["green"] + $this -> SHARP * ($src_clr2["green"] - $src_clr1["green"]));
  298. $b = intval($src_clr2["blue"] + $this -> SHARP * ($src_clr2["blue"] - $src_clr1["blue"]));
  299. $r = min(255, max($r, 0));
  300. $g = min(255, max($g, 0));
  301. $b = min(255, max($b, 0));
  302. if (($DST_CLR = imagecolorexact($this -> PICTURE_CREATE, $r, $g, $b)) == -1)
  303. $DST_CLR = imagecolorallocate($this -> PICTURE_CREATE, $r, $g, $b);
  304. $cnt++;
  305. if ($DST_CLR == -1) die("color allocate faile at $x, $y ($cnt).");
  306. imagesetpixel($this -> TRUE_COLOR, $x, $y, $DST_CLR);
  307. }
  308. }
  309. }
  310. /**
  311. * 将图片反色处理??
  312. */
  313. function return_color(){
  314. /**
  315. * 创建一个画布
  316. */
  317. $NEW_PICTURE_CREATE = imagecreate($this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT);
  318. $WHITE = imagecolorallocate($NEW_PICTURE_CREATE, 255, 255, 255);
  319. /**
  320. * 将背景图拷贝到画布中
  321. */
  322. imagecopy($NEW_PICTURE_CREATE, $this -> PICTURE_CREATE, 0, 0, 0, 0, $this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT);
  323. $this -> TRUE_COLOR = $NEW_PICTURE_CREATE;
  324. }
  325. /**
  326. * 生成目标图片并显示
  327. */
  328. function show(){
  329. // 判断浏览器,若是IE就不发送头
  330. if(isset($_SERVER['HTTP_USER_AGENT']))
  331. {
  332. $ua = strtoupper($_SERVER['HTTP_USER_AGENT']);
  333. if(!preg_match('/^.*MSIE.*\)$/i', $ua))
  334. {
  335. header("Content-type:$this->PICTURE_MIME");
  336. }
  337. }
  338. $OUT = $this -> PICTURE_TYPE;
  339. $OUT($this -> TRUE_COLOR);
  340. }
  341. /**
  342. * 生成目标图片并保存
  343. */
  344. function save_picture(){
  345. // 以 JPEG 格式将图像输出到浏览器或文件
  346. $OUT = $this -> PICTURE_TYPE;
  347. if(function_exists($OUT)){
  348. // 判断浏览器,若是IE就不发送头
  349. if(isset($_SERVER['HTTP_USER_AGENT']))
  350. {
  351. $ua = strtoupper($_SERVER['HTTP_USER_AGENT']);
  352. if(!preg_match('/^.*MSIE.*\)$/i', $ua))
  353. {
  354. header("Content-type:$this->PICTURE_MIME");
  355. }
  356. }
  357. if(!$this -> TRUE_COLOR){
  358. exit($this -> ERROR['unavilable']);
  359. }else{
  360. $OUT($this -> TRUE_COLOR, $this -> DEST_URL);
  361. $OUT($this -> TRUE_COLOR);
  362. }
  363. }
  364. }
  365. /**
  366. * 析构函数:释放图片
  367. */
  368. function __destruct(){
  369. /**
  370. * 释放图片
  371. */
  372. imagedestroy($this -> TRUE_COLOR);
  373. imagedestroy($this -> PICTURE_CREATE);
  374. }
  375. # end of class
  376. }
  377. ?>

这就是非常强大的php图片处理类,好好收藏,亲,相信以后一定会派上用场的。