A versatile php image processing class

Source: Internet
Author: User
Tags imagecopy
A versatile php image processing class

Image processing function functions: scaling, cropping, photo frames, watermarks, sharpening, rotation, turning, transparency, reversed colors, and ideas for processing and saving historical records: when an image is changed, a new image is automatically generated. you can add steps to the original image based on the name method, for example, the first step of the image name +. You can refer to this class in some Web applications that require advanced image processing.

  1. /*
  2. Image processing functions: scaling, cropping, photo frames, watermarks, sharpening, rotation, turning, transparency, and reversed colors
  3. The idea of processing and saving historical records: when an image is changed, a new image is automatically generated. the naming method can be based on the original image. for example: picture name + _ step 1
  4. */
  5. Class picture
  6. {
  7. Var $ PICTURE_URL; // the image to be processed
  8. Var $ DEST_URL = "temp000001.jpg"; // Generate the target image location
  9. Var $ PICTURE_CREATE; // the image to be created
  10. Var $ TURE_COLOR; // create a true color image
  11. Var $ PICTURE_WIDTH; // original image width
  12. Var $ PICTURE_HEIGHT; // original image height
  13. /*
  14. Watermark type. the default value is watermark text.
  15. */
  16. Var $ MARK_TYPE = 1;
  17. Var $ WORD; // text after UTF-8
  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; // The text ANGLE. the default value is 0.
  24. Var $ FONT_COLOR = "#000000"; // text color
  25. Var $ FONT_PATH = "font/simkai. ttf"; // The font Library. the default value is .
  26. Var $ FORCE_URL; // watermark image
  27. Var $ FORCE_X = 0; // horizontal coordinate of the watermark
  28. Var $ FORCE_Y = 0; // watermark ordinate
  29. Var $ FORCE_START_X = 0; // the horizontal coordinate of the watermark image.
  30. Var $ FORCE_START_Y = 0; // cut the vertical coordinate of the watermark image.
  31. Var $ PICTURE_TYPE; // image type
  32. Var $ PICTURE_MIME; // output header
  33. /*
  34. If the scaling ratio is 1, scale by the zoom height and width.
  35. */
  36. Var $ ZOOM = 1; // scaling type
  37. Var $ ZOOM_MULTIPLE; // scaling ratio
  38. Var $ ZOOM_WIDTH; // zoom width
  39. Var $ ZOOM_HEIGHT; // zoom height
  40. /*
  41. Crop, according to ratio and fixed length, width
  42. */
  43. Var $ CUT_TYPE = 1; // cropping type
  44. Var $ CUT_X = 0; // the abscissa of the cropping.
  45. Var $ CUT_Y = 0; // y coordinate of the cropping
  46. Var $ CUT _; // width of the cropping
  47. Var $ CUT_HEIGHT = 100; // crop height
  48. /*
  49. Sharpen
  50. */
  51. Var $ SHARP = "7.0"; // sharpen degree
  52. /*
  53. Transparency processing
  54. */
  55. Var $ ALPHA = '000000'; // The transparency is between 0 and 100.
  56. Var $ ALPHA_X = "90 ";
  57. Var $ ALPHA_Y = "50 ";
  58. /*
  59. Rotate at any angle
  60. */
  61. Var $ CIRCUMROTATE = "90.0"; // note that the value must be a floating point.
  62. /*
  63. Error message
  64. */
  65. Var $ ERROR = array (
  66. 'Unalviable' => 'no related image 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 the image does not exist, the corresponding information is displayed.
  79. */
  80. @ $ SIZE = getimagesize ($ PICTURE_URL );
  81. If (! $ SIZE)
  82. {
  83. Exit ($ this-> ERROR ['unalviable']);
  84. }
  85. // Obtain the information type, width, and height of the original image.
  86. $ This-> PICTURE_MIME = $ SIZE ['Mime '];
  87. $ This-> PICTURE _;
  88. $ This-> PICTURE_HEIGHT = $ SIZE [1];
  89. // Create an image
  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 to hexadecimal
  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 the hexadecimal color to the hexadecimal (R, G, B) color)
  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. // Scaling type
  134. Function zoom_type ($ ZOOM_TYPE)
  135. {
  136. $ This-> ZOOM = $ ZOOM_TYPE;
  137. }
  138. // Scale the image without specifying the height or width
  139. Function zoom ()
  140. {
  141. // Resize
  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 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, $ this-> ZOOM_WIDTH, $ this-> ZOOM_HEIGHT, $ this-> PICTURE_WIDTH, $ this-> PICTURE_HEIGHT );
  152. }
  153. # End of zoom
  154. // Crop the image by 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 image on the image
  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. Obtain the text range using the TrueType font
  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 bottom 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 Image
  194. */
  195. Function _ mark_picture ()
  196. {
  197. /*
  198. Obtain watermark image 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 a 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 watermark image size and generate the target image size. if the watermark is larger than the image size, the generated image size is the watermark image size. Otherwise, the size of the generated image is the size of the original image.
  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. Create a canvas
  245. */
  246. $ NEW_PICTURE_CREATE = imagecreatetruecolor ($ CREATE_WIDTH, $ CREATE_HEIGHT );
  247. $ Whitelist = imagecolorallocate ($ NEW_PICTURE_CREATE, 255,255,255 );
  248. /*
  249. Copy the background image to the canvas.
  250. */
  251. Imagecopy ($ NEW_PICTURE_CREATE, $ this-> PICTURE_CREATE, 0, 0, 0, 0, $ this-> PICTURE_WIDTH, $ this-> PICTURE_HEIGHT );
  252. /*
  253. Copy the target image to the background image.
  254. */
  255. Imagecopy ($ NEW_PICTURE_CREATE, $ FORCE_PICTURE_CREATE, $ this-> FORCE_X, $ this-> FORCE_Y, $ this-> FORCE_START_X, $ this-> FORCE_START_Y, $ region, $ region );
  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. Image rotation:
  288. Rotate along y axis
  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. Rotate along the x axis
  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. Rotate at any angle
  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, $ 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. Image Sharpening
  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. Will the image be reversed ??
  348. */
  349. Function return_color ()
  350. {
  351. /*
  352. Create a canvas
  353. */
  354. $ NEW_PICTURE_CREATE = imagecreate ($ this-> PICTURE_WIDTH, $ this-> PICTURE_HEIGHT );
  355. $ Whitelist = imagecolorallocate ($ NEW_PICTURE_CREATE, 255,255,255 );
  356. /*
  357. Copy the background image to the canvas.
  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. Generate and display the target image
  364. */
  365. Function show ()
  366. {
  367. // Judge the browser. if IE is used, no headers are sent.
  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. Generate and save the target image
  381. */
  382. Function save_picture ()
  383. {
  384. // Output the image to a browser or file in JPEG format
  385. $ OUT = $ this-> PICTURE_TYPE;
  386. If (function_exists ($ OUT ))
  387. {
  388. // Judge the browser. if IE is used, no headers are sent.
  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. Destructor: Release Images
  410. */
  411. Function _ destruct ()
  412. {
  413. /* Release the image */
  414. Imagedestroy ($ this-> TRUE_COLOR );
  415. Imagedestroy ($ this-> PICTURE_CREATE );
  416. }
  417. # End of class
  418. }
  419. ?>


Multi-function, image processing, php

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.