php实现的通用图片处理类

这篇文章主要介绍了php实现的通用图片处理类,可实现针对图片的缩放、剪切、相框、水印、锐化、旋转、翻转、透明度、反色等功能,具有一定参考借鉴价值,需要的朋友可以参考下。

本文实例讲述了php实现的通用图片处理类,分享给大家供大家参考,具体如下:

该图片处理函数功能:缩放、剪切、相框、水印、锐化、旋转、翻转、透明度、反色,处理并保存历史记录的思路:当有图片有改动时自动生成一张新图片,命名方式可以考虑在原图片的基础上加上步骤,例如:图片名称+__第几步。 在一些需要图片高级处理功能的Web应用中,可以参考这个类。

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