Heim  >  Artikel  >  Backend-Entwicklung  >  PHP如何生成自适应大小的缩略图

PHP如何生成自适应大小的缩略图

WBOY
WBOYOriginal
2016-07-25 09:12:551015Durchsuche

生成缩略图的php类,新建一个文件叫做 thumbnailimage.php,文件名最好不要用大写。

  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 %s do not exist' );
  22. define ( 'E_002', 'Failed reading image data from %s' );
  23. define ( 'E_003', 'Cannot create the copy of %s' );
  24. define ( 'E_004', 'Cannot 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 $jpeg_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->jpeg_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. */ 生成缩略图
  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 if ( ! $this->fit_to_max && $is_small )
  180. {
  181. $dest_width = $src_width;
  182. $dest_height = $src_height;
  183. }
  184. elseif ( $x_ratio * $src_height {
  185. $dest_width = $max_width;
  186. $dest_height = ceil ( $x_ratio * $src_height );
  187. }
  188. else
  189. {
  190. $dest_width = ceil ( $y_ratio * $src_width );
  191. $dest_height = $max_height;
  192. }
  193. return array ( $dest_width, $dest_height );
  194. }
  195. /*
  196. Description:
  197. Adds logo image to thumbnail.
  198. Prototype:
  199. void AddLogo ( int thumb_width, int thumb_height, resource &thumb_img )
  200. Parameters:
  201. thumb_width - width of thumbnail image
  202. thumb_height - height of thumbnail image
  203. thumb_img - thumbnail image identifier
  204. */
  205. function AddLogo ( $thumb_width, $thumb_height, &$thumb_img )
  206. {
  207. extract ( $this->logo );
  208. $logo_image = $this->LoadImage ( $file, $logo_width, $logo_height );
  209. if ( $vert_pos == POS_CENTER )
  210. $y_pos = ceil ( $thumb_height / 2 - $logo_height / 2 );
  211. elseif ($vert_pos == POS_BOTTOM)
  212. $y_pos = $thumb_height - $logo_height;
  213. else
  214. $y_pos = 0;
  215. if ( $horz_pos == POS_CENTER )
  216. $x_pos = ceil ( $thumb_width / 2 - $logo_width / 2 );
  217. elseif ( $horz_pos == POS_RIGHT )
  218. $x_pos = $thumb_width - $logo_width;
  219. else
  220. $x_pos = 0;
  221. if ( ! imagecopy ( $thumb_img, $logo_image, $x_pos, $y_pos, 0, 0,
  222. $logo_width, $logo_height ) )
  223. trigger_error( E_004, E_USER_ERROR );
  224. }
  225. /*
  226. Description:
  227. Adds label text to thumbnail.
  228. Prototype:
  229. void AddLabel ( int thumb_width, int thumb_height, resource &thumb_img )
  230. Parameters:
  231. thumb_width - width of thumbnail image
  232. thumb_height - height of thumbnail image
  233. thumb_img - thumbnail image identifier
  234. */
  235. function AddLabel ( $thumb_width, $thumb_height, &$thumb_img )
  236. {
  237. extract ( $this->label );
  238. list( $r, $g, $b ) = $this->ParseColor ( $color );
  239. $color_id = imagecolorallocate ( $thumb_img, $r, $g, $b );
  240. $text_box = imagettfbbox ( $size, $angle, $font, $text );
  241. $text_width = $text_box [ 2 ] - $text_box [ 0 ];
  242. $text_height = abs ( $text_box [ 1 ] - $text_box [ 7 ] );
  243. if ( $vert_pos == POS_TOP )
  244. $y_pos = 5 + $text_height;
  245. elseif ( $vert_pos == POS_CENTER )
  246. $y_pos = ceil( $thumb_height / 2 - $text_height / 2 );
  247. elseif ( $vert_pos == POS_BOTTOM )
  248. $y_pos = $thumb_height - $text_height;
  249. if ( $horz_pos == POS_LEFT )
  250. $x_pos = 5;
  251. elseif ( $horz_pos == POS_CENTER )
  252. $x_pos = ceil( $thumb_width / 2 - $text_width / 2 );
  253. elseif ( $horz_pos == POS_RIGHT )
  254. $x_pos = $thumb_width - $text_width -5;
  255. imagettftext ( $thumb_img, $size, $angle, $x_pos, $y_pos,
  256. $color_id, $font, $text );
  257. }
  258. /*
  259. Description:
  260. Output thumbnail image into the browser.
  261. Prototype:
  262. void OutputThumbImage ( resource dest_image )
  263. Parameters:
  264. dest_img - thumbnail image identifier
  265. */ 输出缩略图
  266. function OutputThumbImage ( $dest_image )
  267. {
  268. imageinterlace ( $dest_image, $this->interlace );
  269. header ( 'Content-type: ' . $this->dest_type );
  270. if ( $this->dest_type == THUMB_JPEG )
  271. imagejpeg ( $dest_image, '', $this->jpeg_quality );
  272. elseif ( $this->dest_type == THUMB_GIF )
  273. imagegif($dest_image);
  274. elseif ( $this->dest_type == THUMB_PNG )
  275. imagepng ( $dest_image );
  276. }
  277. /*
  278. Description:
  279. Save thumbnail image into the disc file.
  280. Prototype:
  281. void SaveThumbImage ( string image_file, resource dest_image )
  282. Parameters:
  283. image_file - destination file name
  284. dest_img - thumbnail image identifier
  285. */
  286. function SaveThumbImage ( $image_file, $dest_image )
  287. {
  288. imageinterlace ( $dest_image, $this->interlace );
  289. if ( $this->dest_type == THUMB_JPEG )
  290. imagejpeg ( $dest_image, $this->dest_file, $this->jpeg_quality );
  291. elseif ( $this->dest_type == THUMB_GIF )
  292. imagegif ( $dest_image, $this->dest_file );
  293. elseif ( $this->dest_type == THUMB_PNG )
  294. imagepng ( $dest_image, $this->dest_file );
  295. }
  296. // ****************************************************************************
  297. // PUBLIC METHODS
  298. // ****************************************************************************
  299. /*
  300. Description:
  301. Output thumbnail image into the browser or disc file according to the
  302. values of parameters.
  303. Prototype:
  304. void Output ( )
  305. */ 生成缩略图片
  306. function Output()
  307. {
  308. $src_image = $this->LoadImage($this->src_file, $src_width, $src_height);
  309. $dest_size = $this->GetThumbSize($src_width, $src_height);
  310. $dest_width=$dest_size[0];
  311. $dest_height=$dest_size[1];
  312. $dest_image=imagecreatetruecolor($dest_width, $dest_height);
  313. if (!$dest_image)
  314. trigger_error(E_005, E_USER_ERROR);
  315. imagecopyresampled( $dest_image, $src_image, 0, 0, 0, 0,
  316. $dest_width, $dest_height, $src_width, $src_height );
  317. if ($this->logo['file'] != NO_LOGO)
  318. $this->AddLogo($dest_width, $dest_height, $dest_image);
  319. if ($this->label['text'] != NO_LABEL)
  320. $this->AddLabel($dest_width, $dest_height, $dest_image);
  321. if ($this->dest_file == STDOUT)
  322. $this->OutputThumbImage ( $dest_image );
  323. else
  324. $this->SaveThumbImage ( $this->dest_file, $dest_image );
  325. imagedestroy ( $src_image );
  326. imagedestroy ( $dest_image );
  327. }
  328. } // End of class definition
  329. ?>
复制代码

使用方法: 1、首先引用该php文件(不要告诉我不会) 2、调用代码

  1. $tis = new ThumbnailImage();
  2. $tis->src_file = "这里写源文件的路径"
  3. $tis->dest_type = THUMB_JPEG;//生成图片的类型是 jpg
  4. $tis->dest_file = '这里写目标文件的路径';
  5. $tis->max_width = 120;//自适应大小,但是最大宽度为120
  6. $tis->max_height = 4000; //自适应大小,但是最大高度为4000
  7. $tis->Output();
复制代码

代码关键在于: max_width 和max_height, 一般来说除非图片很有个性,否则缩略图生成的还是很不错的。



Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:php curl存储cookie实例 Nächster Artikel:php图片添加水印示例