PHP如何產生自適應大小的縮圖

WBOY
發布: 2016-07-25 09:12:55
原創
981 人瀏覽過
不錯

產生縮圖的php類,新建一個檔案叫做 thumbnailimage.php,檔案名稱最好不要用大寫。

  1. define ('MAX_IMG_SIZE', 100000 );
  2. //支援的影像類型
  3. define ('THUMB_J🎜>//支援的圖片類型
  4. define ('THUMBPEG,', ' /jpeg' );
  5. define ( 'THUMB_PNG', 'image/png' );
  6. define ( 'THUMB_GIF', 'image/gif' );
  7. // 隔行掃描模式
  8. define (gif' );
  9. // 隔行掃描模式
  10. define (gif ' INTERLACE_OFF', 0 );
  11. define ( 'INTERLACE_ON', 1 );
  12. //輸出模式
  13. define ('STDOUT', '' );
  14. //空常數
  15. define ( 'NO_LOGO', '' );
  16. define ( 'NO_LABEL', '' );
  17. //Logo和標籤定位
  18. define ('POS_LEFT', 0 );
  19. define ('POS_RIGHT ' , 1 );
  20. define ( 'POS_CENTER', 2 );
  21. define ('POS_TOP', 3 );
  22. define ('POS_BOTTOM', 4 );
  23. // 錯誤訊息
  24. define ( 'E_001', '檔案%s不存在' );
  25. define ( 'E_002', '從%s讀取影像資料失敗' ) ;
  26. define ( 'E_003', '無法建立%s 的副本' );
  27. define ( 'E_004', '無法複製標誌圖片' );
  28. define ( ' E_005', '無法建立最終映像' );
  29. // ****************************** **** **********************************************
  30. / / 類別定義
  31. // ****************************************** **********************************
  32. class ThumbnailImage
  33. {
  34. // ** ************************************************** **** **********************
  35. // 公有屬性
  36. // ************ ** ************************************************ ******************
  37. var $src_file; // 來源映像檔
  38. var $dest_file; // 目標映像檔
  39. var $dest_type; / / 目標圖片類型
  40. var $interlace; // 目標影像交錯模式
  41. var $jpeg_quality; // 產生的JPEG 品質
  42. var $max_width; // 最大縮圖寬度
  43. var $max_height; // 最大縮圖高度
  44. var $fit_to_max; // 放大小圖?
  45. var $logo; // 標誌參數陣列
  46. var $label; // 標籤參數陣列
  47. // ******************* *********************** *************************** **********
  48. // 類別建構子
  49. // * ************************** **************************** ********************** ***
  50. /*
  51. 描述:
  52. 定義屬性的預設值。
  53. 原型:
  54. void ThumbImg ( string src_file = '' )
  55. 參數:
  56. src_file - 來源影像檔案名稱
  57. */
  58. '' function Thumbnail. > {
  59. $this -> src_file = $src_file;
  60. $this->dest_file = STDOUT;
  61. $this->dest_type = THUMB_JPEG;
  62. $this->interlace = INTERLACE_OFFOFF $this- >jpeg_quality = -1;
  63. $this->max_width = 100;
  64. $this->max_height = 90;
  65. $this->fit_to_max = FALSE;
  66. $this-> ; logo['file'] = NO_LOGO;
  67. $this->logo['vert_pos'] = POS_TOP;
  68. $this->logo['horz_pos'] = POS_LEFT;
  69. $this- >label[ 'text'] = NO_LABEL;
  70. $this->label['vert_pos'] = POS_BOTTOM;
  71. $this->label['horz_pos'] = POS_RIGHT;
  72. $this ->label['font '] = '';
  73. $this->label['size'] = 20;
  74. $this->label['color'] = '#000000';
  75. $this->label[ 'angle'] = 0;
  76. }
  77. // ************************ ********** ******************************************** **
  78. / / 私有方法
  79. // **************************************** ** ****************************************
  80. /*
  81. 說明:
  82. 從十六進位顏色字串中提取十進位顏色分量。
  83. 原型:
  84. array ParseColor ( string hex_color )
  85. 參數:
  86. hex_color - '#rrggbb' 格式的顏色
  87. 傳回:
  88. 紅色、綠色和藍色分量的十進位值。
  89. */
  90. function ParseColor ( $hex_color )
  91. {
  92. if ( strpos ( $hex_color, '#' ) === 0 )
  93. $hex_color = substr ( $hex_color, 1) ;
  94. $r = hexdec ( substr ( $hex_color, 0, 2 ) );
  95. $g = hexdec ( substr ( $hex_color, 2, 2 ) ) ;
  96. $b = hexdec ( substr ( $ hex_color, 4, 2 ) );
  97. 傳回陣列( $r, $g, $b );
  98. }
  99. /*
  100. 說明:
  101. 以字串形式擷取影像資料。
  102. 感謝 Luis Larrateguy 提出此函式的想法。
  103. 原型:
  104. string GetImageStr ( string image_file )
  105. 參數:
  106. image_file - 檔案名稱image
  107. 傳回:
  108. 映像檔內容字串。
  109. */
  110. function GetImageStr ( $image_file )
  111. {
  112. if ( function_exists ( 'file_get_contents' ) )
  113. {
  114. $str = @file_get_contents ($image); if ( ! $str )
  115. {
  116. $err = sprintf( E_002, $image_file );
  117. trigger_error( $err, E_USER_ERROR );
  118. }
  119. return $str
  120. $f = fopen ( $image_file, 'rb' );
  121. if ( ! $f )
  122. {
  123. $err = sprintf( E_002, $image_file );
  124. trigger_error( $err , E_USER_ERROR ) ;
  125. }
  126. $fsz = @filesize ( $image_file );
  127. if ( ! $fsz )
  128. $fsz = MAX_IMG_SIZE;
  129. $str = fread ( $f, $fsz ); $str = fread ( $f, $fsz );
  130. fclose $f );
  131. return $str;
  132. }
  133. /*
  134. 說明:
  135. 從檔案載入圖片。
  136. 原型:
  137. resources LoadImage ( string image_file, int &image_width, int &image_height )
  138. 參數:
  139. image_file - 映像的檔案名稱載入🎜> image_widight的高度
  140. 回傳:
  141. 表示從給定檔案。
  142. */
  143. function LoadImage ( $image_file, &$image_width, &$image_height )
  144. {
  145. $image_width = 0;
  146. $image_height = 0; ->GetImageStr( $image_file );
  147. $image = imagecreatefromstring ( $image_data );
  148. if ( ! $image )
  149. {
  150. $err = sprintf( E_003,$image_file ) ; trigger_error( $err, E_USER_ERROR );
  151. }
  152. $image_width = imagesx ( $image );
  153. $image_height = imagesy ( $image );
  154. return $image;
  155. /*
  156. 說明:
  157. 根據來源影像的寬度和高度計算縮圖尺寸。
  158. 原型:
  159. array GetThumbSize ( int src_width, int src_height )
  160. 參數:
  161. src_width - 來源影像的寬度
  162. src_height - 來源影像的高度傳回2 個元素的陣列。索引 0 包含縮圖
  163. 的寬度,索引 1 包含縮圖
  164. 的高度。
  165. */ 產生縮圖
  166. function GetThumbSize ( $src_width, $src_height )
  167. {
  168. $max_width = $this ->max_width;
  169. $max_height = $ > $x_ratio = $max_width / $src_width;
  170. $y_ratio = $max_height / $src_height;
  171. $is_small = ( $ src_width fit_to_max && $is_small )
  172. {
  173. $dest_width = $src_width;
  174. $dest_height = $src_height;
  175. }
  176. elseif( $max_ratioif. 🎜> {
  177. $dest_width = $max_width;
  178. $dest_height = ceil ( $x_ratio * $src_height ) ;
  179. }
  180. else
  181. { $dest_height = $max_height;
  182. }
  183. 返回數組( $dest_width, $dest_height );
  184. }
  185. /*
  186. 描述:
  187. 將徽標圖像新增到縮圖。
  188. 原型:
  189. void AddLogo ( intthumb_width, intthumb_height, resources &thumb_img )
  190. 參數:
  191. thumb_width - 縮略圖的寬度 */
  192. function AddLogo ( $thumb_width, $thumb_height, &$thumb_img )
  193. {
  194. extract ( $this->logo );
  195. $logo_image = $this-this file, $logo_width, $logo_height );
  196. if ( $vert_pos == POS_CENTER )
  197. $y_pos = ceil ( $thumb_height / 2 - $logo_height / 2 ); else==P.
  198. $y_pos = $thumb_height - $logo_height;
  199. else
  200. $ y_pos = 0;
  201. if ( $horz_pos == POS_CENTER )
  202. $x_pos = cewidwumb. / 2 );
  203. elseif ( $horz_pos == POS_RIGHT )
  204. $x_pos = $thumb_width - $logo_width;
  205. else
  206. $x_pos = 0; else
  207. $x_pos = 0; $logo_width, $logo_height ) )
  208. trigger_error( E_004, E_USER_ERROR ); 原型:
  209. void AddLabel ( intthumb_width, intthumb_height, resources &thumb_img )
  210. 參數:
  211. thumb_width - 縮略圖的寬度 */
  212. function AddLabel ( $thumb_width, $thumb_height, &$thumb_img )
  213. {
  214. extract ( $this->label );
  215. list( $r, $g, $bb ) = $this->ParseColor ( $color );
  216. $color_id = imagecolorallocate ( $thumb_img, $r, $g, $b );
  217. $text_box = imagettfbbox ( $size, $angle, $font, $ text );
  218. $text_width = $text_box [ 2 ] - $text_box [ 0 ];
  219. $text_height = abs ( $text_box [ 1 ] - $text_box [ 7 ] );
  220. if ( $vert_pos = = POS_TOP )
  221. $y_pos = 5 + $text_height;
  222. elseif ( $vert_pos == POS_CENTER )
  223. $y_pos = ceil( $thumb_height / 2 - $text_height / 2 ); vert_pos == POS_BOTTOM )
  224. $y_pos = $thumb_height - $text_height;
  225. if ( $horz_pos == POS_LEFT )
  226. $x_pos = 5 ;
  227. elseif (Ilhorz_DisalfDENDat = 5 ; elseif (Hhorz_pos)> 問題 elseif ( $horz_pos == POS_RIGHT )
  228. $x_pos = $thumb_width - $text_width - $text_width - > size, $angle, $x_pos, $y_pos,
  229. $color_id, $font, $text );
  230. }
  231. /*
  232. 說明:
  233. 將縮圖輸出到瀏覽器。
  234. 原型:
  235. void OutputThumbImage ( resource dest_image )
  236. 參數:
  237. dest_img - 縮圖標識符
  238. */ 輸出
  239. function OutputTputimage (> imageinterlace ( $dest_image, $this->interlace );
  240. header ( 'Content-type: ' . $this-> ;dest_type );
  241. if ( $this->dest_type == THUMB_JPEG )
  242. if ( $this->dest_type == THUMB_JPEG )
  243. jpage ( $dest_image, '', $this->jpeg_quality );
  244. elseif ( $this->dest_type = = THUMB_GIF )
  245. imagegif($dest_image); elseif ( $this->3_Ptypeifdest_image); imagepng ( $dest_image );
  246. }
  247. /*
  248. 說明:
  249. 將縮圖儲存到光碟檔案中。
  250. 原型:
  251. void SaveThumbImage ( string image_file, resource dest_image )
  252. 參數:
  253. image_file - 目標檔案名稱
  254. dest_img - 縮圖符> $image_file, $dest_image )
  255. {
  256. imageinterlace ( $dest_image, $this->interlace );
  257. if ( $this->dest_type == THUMB_JPEG )
  258. $dimage, $dest >dest_file, $this->jpeg_quality );
  259. elseif ( $this->dest_type == THUMB_GIF )
  260. imagegif ( $dest_image, $this- >dest_file );
  261. gifeldse ( = THUMB_PNG )
  262. imagepng ( $dest_image, $this->dest_file );
  263. }
  264. // ***** ****************** ****************************************************** *******
  265. // 公用方法
  266. // ***************** ************** **************************************** *********
  267. /*
  268. 說明:
  269. 依照
  270. 參數值將縮圖輸出到瀏覽器或光碟檔案。
  271. 原型:
  272. void Output ( )
  273. */ 產生每一片
  274. function Output()
  275. {
  276. $src_image = $this->LoadImage($this->src_file, $>src_file, $ src_width, $src_height);
  277. $dest_size = $this->GetThumbSize($src_width, $src_height);
  278. $dest_width=$dest_size[0];
  279. $dest_width=$dest_size[0];
  280. $ 🎜> $dest_image= imagecreatetruecolor($dest_width, $dest_height);
  281. if (!$dest_image)
  282. trigger_error(E_005, E_USER_ERROR); $dest_width, $dest_height, $src_width, $src_height );
  283. if ($this->logo['file'] != NO_LOGO)
  284. $this->AddLogo($dest_wid, $ dest_height, $ dest_image);
  285. if ($this->label['text'] != NO_LABEL)
  286. $this->AddLabel($dest_width, $dest_height, $dest_image);
  287. if ($ this ->dest_file == STDOUT)
  288. $this->OutputThumbImage ( $dest_image );
  289. else
  290. $this->SaveThumbImage ( $this->dest_file, $dest_image ); src_image );
  291. imagedestroy ( $dest_image );
  292. }
  293. } // 類別定義結束
  294. ?>
  295. 複製程式碼
  296. ?>
使用方法: 1.先引用該php檔(不要告訴我不會) 2、呼叫程式碼

$tis = new ThumbnailImage();
$tis->src_file = "這裡寫原始檔的路徑"
$tis- > dest_type = THUMB_JPEG;//產生圖片的類型為jpg
    $tis->dest_file = '這裡寫目標檔案的路徑';
  1. $tis->max_width = 120;//自適應大小,但最大寬度為120
  2. $tis->max_height = 4000; // 自適應大小,但最大高度為4000
  3. $tis->Output();
  4. 複製程式碼
複製程式碼
程式碼所在: 最大寬度和最大高度, 一般放棄圖片賦予個性,不然的話生成的還是很的。


來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!