How does PHP generate adaptive thumbnails?

Source: Internet
Author: User
Tags image identifier
How does PHP generate adaptive thumbnails?

Create a new file named thumbnailimage. php for the php class that generates thumbnails. it is best not to use uppercase letters for the file name.

  1. Define ('max _ IMG_SIZE ', 100000 );
  2. // Supported image types
  3. Define ('thumb _ JPEG ', 'image/jpeg ');
  4. Define ('thumb _ PNG ', 'image/png ');
  5. Define ('thumb _ GIF', 'image/GIF ');
  6. // Interlacing modes
  7. Define ('interlace _ off', 0 );
  8. Define ('interlace _ on', 1 );
  9. // Output modes
  10. Define ('stdout ','');
  11. // Empty constants
  12. Define ('no _ LOGO ','');
  13. Define ('no _ label ','');
  14. // Logo and label positioning
  15. Define ('Pos _ left', 0 );
  16. Define ('Pos _ right', 1 );
  17. Define ('Pos _ Center', 2 );
  18. Define ('Pos _ top', 3 );
  19. Define ('Pos _ BOTTOM ', 4 );
  20. // Error messages
  21. Define ('e _ 001', 'File% SDo not exist ');
  22. Define ('e _ 002', 'failed reading image data from% S');
  23. Define ('e _ 003 ', 'cannot create the copy% S');
  24. Define ('e _ 004 ', 'could not copy the logo image ');
  25. Define ('e _ 005 ', 'cannot create final image ');
  26. //************************************** **************************************
  27. // CLASS DEFINITION
  28. //************************************** **************************************
  29. Class ThumbnailImage
  30. {
  31. //************************************** **************************************
  32. // PUBLIC PROPERTIES
  33. //************************************** **************************************
  34. Var $ src_file; // source image file
  35. Var $ dest_file; // destination image file
  36. Var $ dest_type; // destination image type
  37. Var $ interlace; // destination image interlacing mode
  38. Var $ pai_quality; // quality of resulting JPEG
  39. Var $ max_width; // maximal thumbnail width
  40. Var $ max_height; // maximal thumbnail height
  41. Var $ fit_to_max; // enlarge small images?
  42. Var $ logo; // array of logo parameters
  43. Var $ label; // array of label parameters
  44. //************************************** **************************************
  45. // CLASS CONSTRUCTOR
  46. //************************************** **************************************
  47. /*
  48. Description:
  49. Defines default values for properties.
  50. Prototype:
  51. Void ThumbImg (string src_file = '')
  52. Parameters:
  53. Src_file-source image filename
  54. */
  55. Function ThumbnailImage ($ src_file = '')
  56. {
  57. $ This-> src_file = $ src_file;
  58. $ This-> dest_file = STDOUT;
  59. $ This-> dest_type = THUMB_JPEG;
  60. $ This-> interlace = INTERLACE_OFF;
  61. $ This-> pai_quality =-1;
  62. $ This-> max_width = 100;
  63. $ This-> max_height = 90;
  64. $ This-> fit_to_max = FALSE;
  65. $ This-> logo ['file'] = NO_LOGO;
  66. $ This-> logo ['vert _ Pos'] = POS_TOP;
  67. $ This-> logo ['horz _ Pos'] = POS_LEFT;
  68. $ This-> label ['text'] = NO_LABEL;
  69. $ This-> label ['vert _ Pos'] = POS_BOTTOM;
  70. $ This-> label ['horz _ Pos'] = POS_RIGHT;
  71. $ This-> label ['font'] = '';
  72. $ This-> label ['size'] = 20;
  73. $ This-> label ['color'] = '#000000 ';
  74. $ This-> label ['angle '] = 0;
  75. }
  76. //************************************** **************************************
  77. // PRIVATE METHODS
  78. //************************************** **************************************
  79. /*
  80. Description:
  81. Extracts decimal color components from hex color string.
  82. Prototype:
  83. Array ParseColor (string hex_color)
  84. Parameters:
  85. Hex_color-color in '# rrggbb' format
  86. Return:
  87. Decimal values for red, green and blue color components.
  88. */
  89. Function ParseColor ($ hex_color)
  90. {
  91. If (strpos ($ hex_color, '#') = 0)
  92. $ Hex_color = substr ($ hex_color, 1 );
  93. $ R = hexdec (substr ($ hex_color, 0, 2 ));
  94. $ G = hexdec (substr ($ hex_color, 2, 2 ));
  95. $ B = hexdec (substr ($ hex_color, 4, 2 ));
  96. Return array ($ r, $ g, $ B );
  97. }
  98. /*
  99. Description:
  100. Retrives image data as a string.
  101. Thanks to Luis Larrateguy for the idea of this function.
  102. Prototype:
  103. String GetImageStr (string image_file)
  104. Parameters:
  105. Image_file-filename of image
  106. Return:
  107. Image file contents string.
  108. */
  109. Function GetImageStr ($ image_file)
  110. {
  111. If (function_exists ('File _ get_contents '))
  112. {
  113. $ Str = @ file_get_contents ($ image_file );
  114. If (! $ Str)
  115. {
  116. $ Err = sprintf (E_002, $ image_file );
  117. Trigger_error ($ err, E_USER_ERROR );
  118. }
  119. Return $ str;
  120. }
  121. $ F = fopen ($ image_file, 'RB ');
  122. If (! $ F)
  123. {
  124. $ Err = sprintf (E_002, $ image_file );
  125. Trigger_error ($ err, E_USER_ERROR );
  126. }
  127. $ Fsz = @ filesize ($ image_file );
  128. If (! $ Fsz)
  129. $ Fsz = MAX_IMG_SIZE;
  130. $ Str = fread ($ f, $ fsz );
  131. Fclose ($ f );
  132. Return $ str;
  133. }
  134. /*
  135. Description:
  136. Loads image from file.
  137. Prototype:
  138. Resource LoadImage (string image_file, int & image_width, int & image_height)
  139. Parameters:
  140. Image_file-filename of image
  141. Image_width-width of loaded image
  142. Image_height-height of loaded image
  143. Return:
  144. Image identifier representing the image obtained from the given file.
  145. */
  146. Function LoadImage ($ image_file, & $ image_width, & $ image_height)
  147. {
  148. $ Image_width = 0;
  149. $ Image_height = 0;
  150. $ Image_data = $ this-> GetImageStr ($ image_file );
  151. $ Image = imagecreatefromstring ($ image_data );
  152. If (! $ Image)
  153. {
  154. $ Err = sprintf (E_003, $ image_file );
  155. Trigger_error ($ err, E_USER_ERROR );
  156. }
  157. $ Image_width = imagesx ($ image );
  158. $ Image_height = imagesy ($ image );
  159. Return $ image;
  160. }
  161. /*
  162. Description:
  163. Calculates thumbnail image sizes from source image width and height.
  164. Prototype:
  165. Array GetThumbSize (int src_width, int src_height)
  166. Parameters:
  167. Src_width-width of source image
  168. Src_height-height of source image
  169. Return:
  170. An array with 2 elements. Index 0 contains the width of thumbnail image
  171. And index 1 contains the height.
  172. */Generate a thumbnail
  173. Function GetThumbSize ($ src_width, $ src_height)
  174. {
  175. $ Max_width = $ this-> max_width;
  176. $ Max_height = $ this-> max_height;
  177. $ X_ratio = $ max_width/$ src_width;
  178. $ Y_ratio = $ max_height/$ src_height;
  179. $ Is_small = ($ src_width <= $ max_width & $ src_height <= $ max_height );
  180. If (! $ This-> fit_to_max & $ is_small)
  181. {
  182. $ Dest_width = $ src_width;
  183. $ Dest_height = $ src_height;
  184. }
  185. Elseif ($ x_ratio * $ src_height <$ max_height)
  186. {
  187. $ Dest_width = $ max_width;
  188. $ Dest_height = ceil ($ x_ratio * $ src_height );
  189. }
  190. Else
  191. {
  192. $ Dest_width = ceil ($ y_ratio * $ src_width );
  193. $ Dest_height = $ max_height;
  194. }
  195. Return array ($ dest_width, $ dest_height );
  196. }
  197. /*
  198. Description:
  199. Adds logo image to thumbnail.
  200. Prototype:
  201. Void AddLogo (int thumb_width, int thumb_height, resource & thumb_img)
  202. Parameters:
  203. Thumb_width-width of thumbnail image
  204. Thumb_height-height of thumbnail image
  205. Thumb_img-thumbnail image identifier
  206. */
  207. Function AddLogo ($ thumb_width, $ thumb_height, & $ thumb_img)
  208. {
  209. Extract ($ this-> logo );
  210. $ Logo_image = $ this-> LoadImage ($ file, $ logo_width, $ logo_height );
  211. If ($ vert_pos = POS_CENTER)
  212. $ Y_pos = ceil ($ thumb_height/2-$ logo_height/2 );
  213. Elseif ($ vert_pos = POS_BOTTOM)
  214. $ Y_pos = $ thumb_height-$ logo_height;
  215. Else
  216. $ Y_pos = 0;
  217. If ($ horz_pos = POS_CENTER)
  218. $ X_pos = ceil ($ thumb_width/2-$ logo_width/2 );
  219. Elseif ($ horz_pos = POS_RIGHT)
  220. $ X_pos = $ thumb_width-$ logo_width;
  221. Else
  222. $ X_pos = 0;
  223. If (! Imagecopy ($ thumb_img, $ logo_image, $ x_pos, $ y_pos, 0, 0,
  224. $ Logo_width, $ logo_height ))
  225. Trigger_error (E_004, E_USER_ERROR );
  226. }
  227. /*
  228. Description:
  229. Adds label text to thumbnail.
  230. Prototype:
  231. Void AddLabel (int thumb_width, int thumb_height, resource & thumb_img)
  232. Parameters:
  233. Thumb_width-width of thumbnail image
  234. Thumb_height-height of thumbnail image
  235. Thumb_img-thumbnail image identifier
  236. */
  237. Function AddLabel ($ thumb_width, $ thumb_height, & $ thumb_img)
  238. {
  239. Extract ($ this-> label );
  240. List ($ r, $ g, $ B) = $ this-> ParseColor ($ color );
  241. $ Color_id = imagecolorallocate ($ thumb_img, $ r, $ g, $ B );
  242. $ Text_box = imagettfbbox ($ size, $ angle, $ font, $ text );
  243. $ Text_width = $ text_box [2]-$ text_box [0];
  244. $ Text_height = abs ($ text_box [1]-$ text_box [7]);
  245. If ($ vert_pos = POS_TOP)
  246. $ Y_pos = 5 + $ text_height;
  247. Elseif ($ vert_pos = POS_CENTER)
  248. $ Y_pos = ceil ($ thumb_height/2-$ text_height/2 );
  249. Elseif ($ vert_pos = POS_BOTTOM)
  250. $ Y_pos = $ thumb_height-$ text_height;
  251. If ($ horz_pos = POS_LEFT)
  252. $ X_pos = 5;
  253. Elseif ($ horz_pos = POS_CENTER)
  254. $ X_pos = ceil ($ thumb_width/2-$ text_width/2 );
  255. Elseif ($ horz_pos = POS_RIGHT)
  256. $ X_pos = $ thumb_width-$ text_width-5;
  257. Imagettftext ($ thumb_img, $ size, $ angle, $ x_pos, $ y_pos,
  258. $ Color_id, $ font, $ text );
  259. }
  260. /*
  261. Description:
  262. Output thumbnail image into the browser.
  263. Prototype:
  264. Void OutputThumbImage (resource dest_image)
  265. Parameters:
  266. Dest_img-thumbnail image identifier
  267. */Output thumbnails
  268. Function OutputThumbImage ($ dest_image)
  269. {
  270. Imageinterlace ($ dest_image, $ this-> interlace );
  271. Header ('content-type: '. $ this-> dest_type );
  272. If ($ this-> dest_type = THUMB_JPEG)
  273. Imagejpeg ($ dest_image, '', $ this-> pai_quality );
  274. Elseif ($ this-> dest_type = THUMB_GIF)
  275. Imagegif ($ dest_image );
  276. Elseif ($ this-> dest_type = THUMB_PNG)
  277. Imagepng ($ dest_image );
  278. }
  279. /*
  280. Description:
  281. Save thumbnail image into the disc file.
  282. Prototype:
  283. Void SaveThumbImage (string image_file, resource dest_image)
  284. Parameters:
  285. Image_file-destination file name
  286. Dest_img-thumbnail image identifier
  287. */
  288. Function SaveThumbImage ($ image_file, $ dest_image)
  289. {
  290. Imageinterlace ($ dest_image, $ this-> interlace );
  291. If ($ this-> dest_type = THUMB_JPEG)
  292. Imagejpeg ($ dest_image, $ this-> dest_file, $ this-> pai_quality );
  293. Elseif ($ this-> dest_type = THUMB_GIF)
  294. Imagegif ($ dest_image, $ this-> dest_file );
  295. Elseif ($ this-> dest_type = THUMB_PNG)
  296. Imagepng ($ dest_image, $ this-> dest_file );
  297. }
  298. //************************************** **************************************
  299. // PUBLIC METHODS
  300. //************************************** **************************************
  301. /*
  302. Description:
  303. Output thumbnail image into the browser or disc file according to
  304. Values of parameters.
  305. Prototype:
  306. Void Output ()
  307. */Generate a thumbnail.
  308. Function Output ()
  309. {
  310. $ Src_image = $ this-> LoadImage ($ this-> src_file, $ src_width, $ src_height );
  311. $ Dest_size = $ this-> GetThumbSize ($ src_width, $ src_height );
  312. $ Dest_width = $ dest_size [0];
  313. $ Dest_height = $ dest_size [1];
  314. $ Dest_image = imagecreatetruecolor ($ dest_width, $ dest_height );
  315. If (! $ Dest_image)
  316. Trigger_error (E_005, E_USER_ERROR );
  317. Imagecopyresampled ($ dest_image, $ src_image, 0, 0, 0, 0,
  318. $ Dest_width, $ dest_height, $ src_width, $ src_height );
  319. If ($ this-> logo ['file']! = NO_LOGO)
  320. $ This-> AddLogo ($ dest_width, $ dest_height, $ dest_image );
  321. If ($ this-> label ['text']! = NO_LABEL)
  322. $ This-> AddLabel ($ dest_width, $ dest_height, $ dest_image );
  323. If ($ this-> dest_file = STDOUT)
  324. $ This-> OutputThumbImage ($ dest_image );
  325. Else
  326. $ This-> SaveThumbImage ($ this-> dest_file, $ dest_image );
  327. Imagedestroy ($ src_image );
  328. Imagedestroy ($ dest_image );
  329. }
  330. } // End of class definition
  331. ?>

Usage: 1. first reference the php file (do not tell me) 2. call the code

  1. $ Tis = new ThumbnailImage ();
  2. $ Tis-> src_file = "path of the source file written here"
  3. $ Tis-> dest_type = THUMB_JPEG; // The image generation type is jpg.
  4. $ Tis-> dest_file = 'write the path of the destination file ';
  5. $ Tis-> max_width = 120; // adaptive size, but the maximum width is 120
  6. $ Tis-> max_height = 4000; // adaptive size, but the maximum height is 4000
  7. $ Tis-> Output ();

The key to the code is: max_width and max_height. generally, thumbnails are generated very well unless the images are personalized.

Related Article

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.