Home > Backend Development > PHP Tutorial > A multifunctional php image processing class

A multifunctional php image processing class

WBOY
Release: 2016-07-25 08:43:29
Original
851 people have browsed it

Picture processing function functions: zoom, cut, photo frame, watermark, sharpen, rotate, flip, transparency, invert color, processing and saving history idea: automatically generate a new picture when a picture is changed, naming method You can consider adding steps based on the original picture, for example: picture name + __ which step. In some web applications that require advanced image processing functions, you can refer to this class.

  1. /*
  2. Image processing function functions: zoom, cut, photo frame, watermark, sharpen, rotate, flip, transparency, invert color
  3. The idea of ​​processing and saving history: when there is a picture A new picture is automatically generated when making changes. The naming method can consider adding steps based on the original picture, for example: picture name + __ steps
  4. */
  5. class picture
  6. {
  7. var $PICTURE_URL;//To be processed Picture
  8. var $DEST_URL="temp__01.jpg";//Generate the target picture location
  9. var $PICTURE_CREATE;//The picture to be created
  10. var $TURE_COLOR;//Create a new true color image
  11. var $PICTURE_WIDTH;// Original picture width
  12. var $PICTURE_HEIGHT;//Original picture height
  13. /*
  14. Type of watermark, the default is watermark text
  15. */
  16. var $MARK_TYPE=1;
  17. var $WORD;//After UTF-8 Text
  18. var $WORD_X; // Text abscissa
  19. var $WORD_Y; // Text ordinate
  20. var $FONT_TYPE; // Font type
  21. var $FONT_SIZE="12"; // Font size
  22. var $FONT_WORD; // Text
  23. var $ANGLE=0;//Angle of text, default is 0
  24. var $FONT_COLOR="#000000";//Color of text
  25. var $FONT_PATH="font/simkai.ttf";//Font library, default It is Song Dynasty
  26. var $FORCE_URL;//Watermark image
  27. var $FORCE_X=0;//Abscissa coordinate of watermark
  28. var $FORCE_Y=0;//Vertical coordinate of watermark
  29. var $FORCE_START_X=0;//Cut the horizontal axis of the watermarked image Coordinates
  30. var $FORCE_START_Y=0;//The vertical coordinate of the picture where the watermark is cut
  31. var $PICTURE_TYPE;//Picture type
  32. var $PICTURE_MIME;//The output header
  33. /*
  34. If the scaling ratio is 1, press Zoom height and width zoom
  35. */
  36. var $ZOOM=1;//Zoom type
  37. var $ZOOM_MULTIPLE;//Zoom ratio
  38. var $ZOOM_WIDTH;//Zoom width
  39. var $ZOOM_HEIGHT;//Zoom height
  40. /*
  41. Crop, proportional and fixed length and width
  42. */
  43. var $CUT_TYPE=1;//Cut type
  44. var $CUT_X=0;//Abscissa of cropping
  45. var $CUT_Y=0;//Crop The ordinate of the cut
  46. var $CUT_;//The width of the cut
  47. var $CUT_HEIGHT=100;//The height of the cut
  48. /*
  49. Sharpening
  50. */
  51. var $SHARP="7.0";//Sharp Degree of transformation
  52. /*
  53. Transparency processing
  54. */
  55. var $ALPHA='100';//Transparency is between 0-127
  56. var $ALPHA_X="90";
  57. var $ALPHA_Y="50";
  58. /*
  59. Rotate at any angle
  60. */
  61. var $CIRCUMROTATE="90.0";//Note that it must be a floating point number
  62. /*
  63. Error message
  64. */
  65. var $ERROR=array(
  66. 'unalviable'=> 'No related pictures found!'
  67. );
  68. /*
  69. Constructor: function initialization
  70. */
  71. function __construct($PICTURE_URL)
  72. {
  73. $this->get_info($PICTURE_URL);
  74. }
  75. function get_info ($PICTURE_URL)
  76. {
  77. /*
  78. Process the information of the original image, first check whether the image exists, if not, the corresponding information will be given
  79. */
  80. @$SIZE=getimagesize($PICTURE_URL);
  81. if(!$SIZE )
  82. {
  83. exit($this->ERROR['unalviable']);
  84. }
  85. //Get the information type, width, and height of the original picture
  86. $this->PICTURE_MIME=$SIZE['mime'] ;
  87. $this->PICTURE_;
  88. $this->PICTURE_HEIGHT=$SIZE[1];
  89. //Create picture
  90. switch($SIZE[2])
  91. {
  92. case 1:
  93. $this-> PICTURE_CREATE=imagecreatefromgif($PICTURE_URL);
  94. $this->PICTURE_TYPE="imagejpeg";
  95. $this->PICTURE_EXT="jpg";
  96. break;
  97. case 2:
  98. $this->PICTURE_CREATE=imagecreatefromjpeg($ PICTURE_URL);
  99. $this->PICTURE_TYPE="imagegif";
  100. $this->PICTURE_EXT="gif";
  101. break;
  102. case 3:
  103. $this->PICTURE_CREATE=imagecreatefrompng($PICTURE_URL);
  104. $ this->PICTURE_TYPE="imagepng";
  105. $this->PICTURE_EXT="png";
  106. break;
  107. }
  108. /*
  109. Convert text color from hexadecimal to decimal
  110. */
  111. preg_match_all(" /([0-f]){2,2}/i",$this->FONT_COLOR,$MATCHES);
  112. if(count($MATCHES)==3)
  113. {
  114. $this->RED= hexdec($MATCHES[0][0]);
  115. $this->GREEN=hexdec($MATCHES[0][1]);
  116. $this->BLUE=hexdec($MATCHES[0][2] );
  117. }
  118. }
  119. #end of __construct
  120. /*
  121. Convert hexadecimal color to decimal (R, G, B)
  122. */
  123. function hex2dec()
  124. {
  125. preg_match_all(" /([0-f]){2,2}/i",$this->FONT_COLOR,$MATCHES);
  126. if(count($MATCHES)==3)
  127. {
  128. $this->RED= hexdec($MATCHES[0][0]);
  129. $this->GREEN=hexdec($MATCHES[0][1]);
  130. $this->BLUE=hexdec($MATCHES[0][2] );
  131. }
  132. }
  133. //Zoom type
  134. function zoom_type($ZOOM_TYPE)
  135. {
  136. $this->ZOOM=$ZOOM_TYPE;
  137. }
  138. //Zoom the picture, if the height and width are not specified, it will be zoomed
  139. function zoom( )
  140. {
  141. //Zoom size
  142. if($this->ZOOM==0)
  143. {
  144. $this->ZOOM_;gt;PICTURE_WIDTH * $this->ZOOM_MULTIPLE;
  145. $this->ZOOM_HEIGHT =$this->PICTURE_HEIGHT * $this->ZOOM_MULTIPLE;
  146. }
  147. //Create a new true color image
  148. $this->TRUE_COLOR=imagecreatetruecolor($this->ZOOM_WIDTH,$this->ZOOM_HEIGHT) ;
  149. $WHITE=imagecolorallocate($this->TRUE_COLOR,255,255,255);
  150. imagefilledrectangle($this->TRUE_COLOR,0,0,$this->ZOOM_WIDTH,$this->ZOOM_HEIGHT,$WHITE);
  151. imagecopyresized($this->TRUE_COLOR,$this->PICTURE_CREATE,0,0,0,0,$this->ZOOM_WIDTH,$this->ZOOM_HEIGHT,$this->PICTURE_WIDTH,$this-> PICTURE_HEIGHT);
  152. }
  153. #end of zoom
  154. //Crop the picture, according to coordinates or automatically
  155. function cut()
  156. {
  157. $this->TRUE_COLOR=imagecreatetruecolor($this->CUT_WIDTH,$this-> ;CUT_WIDTH);
  158. imagecopy($this->TRUE_COLOR,$this->PICTURE_CREATE, 0, 0, $this->CUT_X, $this->CUT_Y,$this->CUT_WIDTH,$this-> ;CUT_HEIGHT);
  159. }
  160. #end of cut
  161. /*
  162. Put text or pictures on the picture
  163. Watermark text
  164. */
  165. function _mark_text()
  166. {
  167. $this->TRUE_COLOR=imagecreatetruecolor($this-> ;PICTURE_WIDTH,$this->PICTURE_HEIGHT);
  168. $this->WORD=mb_convert_encoding($this->FONT_WORD,'utf-8','gb2312');
  169. /*
  170. Get the text using TrueType font Range
  171. */
  172. $TEMP = imagettfbbox($this->FONT_SIZE,0,$this->FONT_PATH,$this->WORD);
  173. $WORD_LENGTH=strlen($this->WORD);
  174. $ WORD_WIDTH =$TEMP[2] - $TEMP[6];
  175. $WORD_HEIGHT =$TEMP[3] - $TEMP[7];
  176. /*
  177. The default position of the text watermark is the lower right corner
  178. */
  179. if($this ->WORD_X=="")
  180. {
  181. $this->WORD_X=$this->PICTURE_WIDTH-$WORD_WIDTH;
  182. }
  183. if($this->WORD_Y=="")
  184. {
  185. $this ->WORD_Y=$this->PICTURE_HEIGHT-$WORD_HEIGHT;
  186. }
  187. imagesettile($this->TRUE_COLOR,$this->PICTURE_CREATE);
  188. imagefilledrectangle($this->TRUE_COLOR,0,0,$ this->PICTURE_WIDTH,$this->PICTURE_HEIGHT,IMG_COLOR_TILED);
  189. $TEXT2=imagecolorallocate($this->TRUE_COLOR,$this->RED,$this->GREEN,$this->Blue) ;
  190. imagettftext($this->TRUE_COLOR,$this->FONT_SIZE,$this->ANGLE,$this->WORD_X,$this->WORD_Y,$TEXT2,$this->FONT_PATH,$ this->WORD);
  191. }
  192. /*
  193. Watermark picture
  194. */
  195. function _mark_picture()
  196. {
  197. /*
  198. Get watermark picture information
  199. */
  200. @$SIZE=getimagesize($this-> FORCE_URL);
  201. if(!$SIZE)
  202. {
  203. exit($this->ERROR['unalviable']);
  204. }
  205. $FORCE_PICTURE_;
  206. $FORCE_PICTURE_HEIGHT=$SIZE[1];
  207. //Create watermark image
  208. switch($SIZE[2])
  209. {
  210. case 1:
  211. $FORCE_PICTURE_CREATE=imagecreatefromgif($this->FORCE_URL);
  212. $FORCE_PICTURE_TYPE="gif";
  213. break;
  214. case 2:
  215. $FORCE_PICTURE_CREATE=imagecreatefromjpeg( $this->FORCE_URL);
  216. $FORCE_PICTURE_TYPE="jpg";
  217. break;
  218. case 3:
  219. $FORCE_PICTURE_CREATE=imagecreatefrompng($this->FORCE_URL);
  220. $FORCE_PICTURE_TYPE="png";
  221. break;
  222. }
  223. /*
  224. Determine the size of the watermark image and generate the size of the target image. If the watermark is larger than the image, the size of the generated image will be the size of the watermark image. Otherwise, the size of the generated image will be the original image size.
  225. */
  226. $this->NEW_PICTURE=$this->PICTURE_CREATE;
  227. if($FORCE_PICTURE_WIDTH>$this->PICTURE_WIDTH)
  228. {
  229. $CREATE_;gt;FORCE_START_X;
  230. }
  231. else
  232. {
  233. $CREATE_;gt;PICTURE_WIDTH;
  234. }
  235. if($FORCE_PICTURE_HEIGHT>$this->PICTURE_HEIGHT)
  236. {
  237. $CREATE_HEIGHT=$FORCE_PICTURE_HEIGHT-$this->FORCE_START_Y;
  238. }
  239. else
  240. {
  241. $CREATE_HEIGHT=$this->PICTURE_HEIGHT;
  242. }
  243. /*
  244. 创建一个画布
  245. */
  246. $NEW_PICTURE_CREATE=imagecreatetruecolor($CREATE_WIDTH,$CREATE_HEIGHT);
  247. $WHITE=imagecolorallocate($NEW_PICTURE_CREATE,255,255,255);
  248. /*
  249. 将背景图拷贝到画布中
  250. */
  251. imagecopy($NEW_PICTURE_CREATE, $this->PICTURE_CREATE, 0, 0, 0, 0,$this->PICTURE_WIDTH,$this->PICTURE_HEIGHT);
  252. /*
  253. 将目标图片拷贝到背景图片上
  254. */
  255. 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);
  256. $this->TRUE_COLOR=$NEW_PICTURE_CREATE;
  257. }
  258. #end of mark
  259. function alpha_()
  260. {
  261. $this->TRUE_COLOR=imagecreatetruecolor($this->PICTURE_WIDTH,$this->PICTURE_HEIGHT);
  262. $rgb="#CDCDCD";
  263. $tran_color="#000000";
  264. for($j=0;$j<=$this->PICTURE_HEIGHT-1;$j++)
  265. {
  266. for ($i=0;$i<=$this->PICTURE_WIDTH-1;$i++)
  267. {
  268. $rgb = imagecolorat($this->PICTURE_CREATE,$i,$j);
  269. $r = ($rgb >> 16) & 0xFF;
  270. $g = ($rgb >> 8) & 0xFF;
  271. $b = $rgb & 0xFF;
  272. $now_color=imagecolorallocate($this->PICTURE_CREATE,$r,$g,$b);
  273. if ($now_color==$tran_color)
  274. {
  275. continue;
  276. }
  277. else
  278. {
  279. $color=imagecolorallocatealpha($this->PICTURE_CREATE,$r,$g,$b,$ALPHA);
  280. imagesetpixel($this->PICTURE_CREATE,$ALPHA_X+$i,$ALPHA_Y+$j,$color);
  281. }
  282. $this->TRUE_COLOR=$this->PICTURE_CREATE;
  283. }
  284. }
  285. }
  286. /*
  287. 图片旋转:
  288. 沿y轴旋转
  289. */
  290. function turn_y()
  291. {
  292. $this->TRUE_COLOR=imagecreatetruecolor($this->PICTURE_WIDTH,$this->PICTURE_HEIGHT);
  293. for ($x = 0; $x < $this->PICTURE_WIDTH; $x++)
  294. {
  295. imagecopy($this->TRUE_COLOR, $this->PICTURE_CREATE, $this->PICTURE_WIDTH - $x - 1, 0, $x, 0, 1, $this->PICTURE_HEIGHT);
  296. }
  297. }
  298. /*
  299. 沿X轴旋转
  300. */
  301. function turn_x()
  302. {
  303. $this->TRUE_COLOR=imagecreatetruecolor($this->PICTURE_WIDTH,$this->PICTURE_HEIGHT);
  304. for ($y = 0; $y < $this->PICTURE_HEIGHT; $y++)
  305. {
  306. imagecopy($this->TRUE_COLOR, $this->PICTURE_CREATE, 0, $this->PICTURE_HEIGHT - $y - 1, 0, $y, $this->PICTURE_WIDTH, 1);
  307. }
  308. }
  309. /*
  310. 任意角度旋转
  311. */
  312. function turn()
  313. {
  314. $this->TRUE_COLOR=imagecreatetruecolor($this->PICTURE_WIDTH,$this->PICTURE_HEIGHT);
  315. imageCopyResized($this->TRUE_COLOR,$this->PICTURE_CREATE,0,0,0,0,$this->PICTURE_WIDTH,$this->PICTURE_HEIGHT,$this->PICTURE_WIDTH,$this->PICTURE_HEIGHT);
  316. $WHITE=imagecolorallocate($this->TRUE_COLOR,255,255,255);
  317. $this->TRUE_COLOR=imagerotate ($this->TRUE_COLOR, $this->CIRCUMROTATE, $WHITE);
  318. }
  319. /*
  320. 图片锐化
  321. */
  322. function sharp()
  323. {
  324. $this->TRUE_COLOR=imagecreatetruecolor($this->PICTURE_WIDTH,$this->PICTURE_HEIGHT);
  325. $cnt=0;
  326. for ($x=0; $x<$this->PICTURE_WIDTH; $x++)
  327. {
  328. for ($y=0; $y<$this->PICTURE_HEIGHT; $y++)
  329. {
  330. $src_clr1 = imagecolorsforindex($this->TRUE_COLOR, imagecolorat($this->PICTURE_CREATE, $x-1, $y-1));
  331. $src_clr2 = imagecolorsforindex($this->TRUE_COLOR, imagecolorat($this->PICTURE_CREATE, $x, $y));
  332. $r = intval($src_clr2["red"]+$this->SHARP*($src_clr2["red"]-$src_clr1["red"]));
  333. $g = intval($src_clr2["green"]+$this->SHARP*($src_clr2["green"]-$src_clr1["green"]));
  334. $b = intval($src_clr2["blue"]+$this->SHARP*($src_clr2["blue"]-$src_clr1["blue"]));
  335. $r = min(255, max($r, 0));
  336. $g = min(255, max($g, 0));
  337. $b = min(255, max($b, 0));
  338. if (($DST_CLR=imagecolorexact($this->PICTURE_CREATE, $r, $g, $b))==-1)
  339. $DST_CLR = imagecolorallocate($this->PICTURE_CREATE, $r, $g, $b);
  340. $cnt++;
  341. if ($DST_CLR==-1) die("color allocate faile at $x, $y ($cnt).");
  342. imagesetpixel($this->TRUE_COLOR, $x, $y, $DST_CLR);
  343. }
  344. }
  345. }
  346. /*
  347. 将图片反色处理??
  348. */
  349. function return_color()
  350. {
  351. /*
  352. 创建一个画布
  353. */
  354. $NEW_PICTURE_CREATE=imagecreate($this->PICTURE_WIDTH,$this->PICTURE_HEIGHT);
  355. $WHITE=imagecolorallocate($NEW_PICTURE_CREATE,255,255,255);
  356. /*
  357. 将背景图拷贝到画布中
  358. */
  359. imagecopy($NEW_PICTURE_CREATE, $this->PICTURE_CREATE, 0, 0, 0, 0,$this->PICTURE_WIDTH,$this->PICTURE_HEIGHT);
  360. $this->TRUE_COLOR=$NEW_PICTURE_CREATE;
  361. }
  362. /*
  363. 生成目标图片并显示
  364. */
  365. function show()
  366. {
  367. // 判断浏览器,若是IE就不发送头
  368. if(isset($_SERVER['HTTP_USER_AGENT']))
  369. {
  370. $ua = strtoupper($_SERVER['HTTP_USER_AGENT']);
  371. if(!preg_match('/^.*MSIE.*)$/i',$ua))
  372. {
  373. header("Content-type:$this->PICTURE_MIME");
  374. }
  375. }
  376. $OUT=$this->PICTURE_TYPE;
  377. $OUT($this->TRUE_COLOR);
  378. }
  379. /*
  380. 生成目标图片并保存
  381. */
  382. function save_picture()
  383. {
  384. // 以 JPEG 格式将图像输出到浏览器或文件
  385. $OUT=$this->PICTURE_TYPE;
  386. if(function_exists($OUT))
  387. {
  388. // 判断浏览器,若是IE就不发送头
  389. if(isset($_SERVER['HTTP_USER_AGENT']))
  390. {
  391. $ua = strtoupper($_SERVER['HTTP_USER_AGENT']);
  392. if(!preg_match('/^.*MSIE.*)$/i',$ua))
  393. {
  394. header("Content-type:$this->PICTURE_MIME");
  395. }
  396. }
  397. if(!$this->TRUE_COLOR)
  398. {
  399. exit($this->ERROR['unavilable']);
  400. }
  401. else
  402. {
  403. $OUT($this->TRUE_COLOR,$this->DEST_URL);
  404. $OUT($this->TRUE_COLOR);
  405. }
  406. }
  407. }
  408. /*
  409. 析构函数:释放图片
  410. */
  411. function __destruct()
  412. {
  413. /*释放图片*/
  414. imagedestroy($this->TRUE_COLOR);
  415. imagedestroy($this->PICTURE_CREATE);
  416. }
  417. #end of class
  418. }
  419. ?>
复制代码

多功能, 图片处理, php


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template