A multi-function PHP image processing class

Source: Internet
Author: User
Tags imagecopy

Image processing functions: Zoom, cut, frame, watermark, sharpen, rotate, flip, transparency, invert color, process and save the history of the idea: when there are changes in the picture to automatically generate a new picture, naming method can be considered in the original image based on the addition of steps, such as: Picture name +__ step. You can refer to this class in some Web applications that require image advanced processing capabilities.

  1. /*
  2. Image processing functions: Zoom, cut, frame, watermark, sharpen, rotate, flip, transparency, invert color
  3. Process and save the history of the idea: when there are changes to the image automatically generated a new picture, naming method can be considered in the original image based on the addition of steps, for example: Picture name +__ First Steps
  4. */
  5. Class picture
  6. {
  7. var $PICTURE _url;//Pictures to process
  8. var $DEST _url= "temp__01.jpg";//Generate target picture position
  9. var $PICTURE _create;//The picture you want to create
  10. var $TURE _color;//A new true color image
  11. var $PICTURE _width;//Original image width
  12. var $PICTURE _height;//Original Image Height
  13. /*
  14. The type of watermark, which is the default watermark text
  15. */
  16. var $MARK _type=1;
  17. var $WORD;//The text after UTF-8
  18. var $WORD _x;//Text Horizontal
  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 the angle of =0;//text, which defaults to 0
  24. var $FONT _color= "#000000";//Text color
  25. var $FONT _path= "Font/simkai.ttf";//font library, default to Arial
  26. var $FORCE _url;//Watermark Image
  27. var $FORCE _x=0;//Watermark Horizontal
  28. var $FORCE _y=0;//Watermark Ordinate
  29. var $FORCE _start_x=0;//-Cut watermark image
  30. var $FORCE _start_y=0;//-Cut watermark image ordinate
  31. var $PICTURE _type;//Picture Type
  32. var $PICTURE the head of the _mime;//output
  33. /*
  34. Scale to zoom height and width by 1
  35. */
  36. var $ZOOM =1;//Scaling Type
  37. var $ZOOM _multiple;//Zoom ratio
  38. var $ZOOM _width;//Scale Relaxation
  39. var $ZOOM _height;//Zoom Height
  40. /*
  41. Cut, proportional and fixed length, width
  42. */
  43. var $CUT _type=1;//Cut Type
  44. var $CUT the horizontal axis of _x=0;//cutting
  45. var $CUT the ordinate of _y=0;//trimming
  46. var $CUT _;//width of the crop
  47. var $CUT _height=100;//Cutting Height
  48. /*
  49. Sharpening
  50. */
  51. var $SHARP = "7.0";//Sharpening degree
  52. /*
  53. Transparency processing
  54. */
  55. var $ALPHA = ' 100 ';//transparency 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 a floating-point number must be
  62. /*
  63. Error message
  64. */
  65. var $ERROR =array (
  66. ' unalviable ' = ' didn't find the relevant picture! '
  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. Processing the information of the original picture, first detect whether the picture exists, does not exist to give the corresponding information
  79. */
  80. @ $SIZE =getimagesize ($PICTURE _url);
  81. if (! $SIZE)
  82. {
  83. Exit ($this->error[' unalviable ');
  84. }
  85. Get the information type, width, height of the original picture
  86. $this->picture_mime= $SIZE [' MIME '];
  87. $this->picture_;
  88. $this->picture_height= $SIZE [1];
  89. Create a 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. Text color conversion 16 binary conversion to 10 binary
  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. Converts 16 colors into 10 binary (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. Scale type
  134. function Zoom_type ($ZOOM _type)
  135. {
  136. $this->zoom= $ZOOM _type;
  137. }
  138. Scale the picture if you don't specify a height and width
  139. function Zoom ()
  140. {
  141. Size of the scale
  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 pictures, 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 pictures on a 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 range of text that uses TrueType fonts
  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. $TEXT 2=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, $TEXT 2 , $this->font_path, $this->word);
  191. }
  192. /*
  193. Watermark Picture
  194. */
  195. function _mark_picture ()
  196. {
  197. /*
  198. Get information about a watermark picture
  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 Picture
  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 picture, the image size is the size of the watermark picture. Otherwise, the resulting picture size is the original picture 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. Create a canvas
  245. */
  246. $NEW _picture_create=imagecreatetruecolor ($CREATE _width, $CREATE _height);
  247. $WHITE =imagecolorallocate ($NEW _picture_create,255,255,255);
  248. /*
  249. Copy the background map 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 picture 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, $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 >>) & 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. Picture rotation:
  288. Rotate along the 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->pictur E_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,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. Reverse color processing of pictures??
  348. */
  349. function Return_color ()
  350. {
  351. /*
  352. Create a canvas
  353. */
  354. $NEW _picture_create=imagecreate ($this->picture_width, $this->picture_height);
  355. $WHITE =imagecolorallocate ($NEW _picture_create,255,255,255);
  356. /*
  357. Copy the background map 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 a target picture and display
  364. */
  365. Function Show ()
  366. {
  367. Judge Browser, if IE does not send headers
  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. Create a target picture and save it
  381. */
  382. function Save_picture ()
  383. {
  384. Output images to a browser or file in JPEG format
  385. $OUT = $this->picture_type;
  386. if (function_exists ($OUT))
  387. {
  388. Judge Browser, if IE does not send headers
  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. Destructors: Releasing pictures
  410. */
  411. function __destruct ()
  412. {
  413. /* Release Picture */
  414. Imagedestroy ($this->true_color);
  415. Imagedestroy ($this->picture_create);
  416. }
  417. #end of Class
  418. }
  419. ?>
Copy Code
Multifunction, 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.