php生成缩略图,文本转换成图形

这是一款老外的经典的php生成缩略图类函数,文件灵活实用,可以生成任何风格的图片,并且可以把文本转换成图形.

php生成缩略图,文本转换成图形实例代码如下:

  1. copyright : smart-info limited. all right reserved.
  2. author : jacky cheung
  3. version : 1.1
  4. create date : 24 september 2008
  5. last modify : 15 march 2009
  6. */
  7. class gd
  8. {
  9. var $font_face = "";
  10. var $text = "";
  11. var $size = 12;
  12. var $color = "#000000";
  13. var $angle = 0;
  14. var $width = 0;
  15. var $height = 0;
  16. var $line_height = 0;
  17. var $type = "png";
  18. var $chmod = 0777;
  19. var $bg_color = "#ffffff";
  20. var $quality = 95;
  21. var $antialias = true;
  22. var $x = 0;
  23. var $y = 0;
  24. /*___| convert text to image |___*/
  25. public function text2image ( $font_, $text="", $attributes=false, $width=0, $all=false )
  26. {
  27. $this->font_face = $font_face;
  28. $this->text = $text;
  29. $this->width = $width;
  30. $this->size = 12;
  31. $this->color = "#000000";
  32. $this->angle = 0;
  33. $this->line_height = 0;
  34. $this->setprop ( $attributes );
  35. if ( $this->width == 0 )
  36. {
  37. return $this->convert_text2image ( $this->text, $this->check_text_width( $this->text ) );
  38. } else {
  39. // word wrap
  40. if ( $all === false )
  41. {
  42. $text = split(" ", $this->text);
  43. $text = $this->word_wrap($this->text, $this->width, "<br>");
  44. $text = split("<br>", $text );
  45. } else if ( $all === true ) {
  46. $temp = array();
  47. for ( $i=0; $i<strlen($this->text); $i++ )
  48. {
  49. array_push ( $temp, mb_substr($this->text, $i, 1, "utf-8") );
  50. }
  51. $text = array();
  52. $count_width = 0;
  53. $i = 0;
  54. foreach ( $temp as $k => $t )
  55. {
  56. $prop = $this->check_text_width($t);
  57. if ( $count_width + floatval($prop["width"]) < $this->width )
  58. {
  59. $text[$i] = $text[$i] . $t;
  60. $count_width += floatval($prop["width"]);
  61. } else {
  62. $count_width = 0;
  63. $i++;
  64. $text[$i] = "";
  65. }
  66. }
  67. }
  68. $img = array();
  69. foreach ( $text as $k => $t )
  70. {
  71. $img[$k] = $this->convert_text2image ( $t, $this->check_text_width( $t ) );
  72. }
  73. $w = 0;
  74. $h = 0;
  75. foreach ( $img as $k => $v )
  76. {
  77. $w = (imagesx($img[$k]) > $w) ? imagesx($img[$k]) : $w;
  78. if ($this->line_height == 0 ) $h += imagesy($img[$k]);
  79. else $h += ($k < count($img)-1) ? $this->line_height : imagesy($img[$k]);
  80. }
  81. $base_img = $this->createtransparent($w, $h);
  82. $locy = 0;
  83. foreach ( $img as $k => $v )
  84. {
  85. if ($k > 0)
  86. {
  87. $locy = ($this->line_height == 0) ? $locy + imagesy($img[$k]) : $locy + $this->line_height;
  88. }
  89. $base_img = $this->attachgdimage ( $img[$k], $base_img, array ("x"=>0, "y"=>$locy) );
  90. }
  91. return $base_img;
  92. }
  93. }
  94. private function word_wrap( $str, $width, $break )
  95. {
  96. $formatted = '';
  97. $position = -1;
  98. $prev_position = 0;
  99. $last_line = -1;
  100. /// looping the string stop at each space
  101. while( $position = mb_stripos( $str, " ", ++$position, 'utf-8' ) ) {
  102. if( $position > $last_line + $width + 1 ) {
  103. $formatted.= mb_substr( $str, $last_line + 1, $prev_position - $last_line - 1, 'utf-8' ).$break;
  104. $last_line = $prev_position;
  105. }
  106. $prev_position = $position;
  107. }
  108. /// adding last line without the break
  109. $formatted.= mb_substr( $str, $last_line + 1, mb_strlen( $str ), 'utf-8' );
  110. return $formatted;
  111. }
  112. public function convert_text2image ( $text, $prop )
  113. {
  114. $im = imagecreatetruecolor ( $prop["width"], $prop["height"] );
  115. $rgb = $this->getrgb ( $this->color );
  116. $color = imagecolorallocate ( $im, $rgb["red"], $rgb["green"], $rgb["blue"] );
  117. $img = $this->createtransparent ( $prop["width"], $prop["height"] );
  118. imagettftext ( $img, $this->size, $this->angle, 0, $prop["height"] - abs ( $prop["top"] ), $color, $this->font_face, $text );
  119. return $img;
  120. }
  121. public function check_text_width ( $text )
  122. {
  123. $prop = array();
  124. $bbox = imagettfbbox ( $this->size, $this->angle, $this->font_face, $text );
  125. $prop["left"] = $bbox[0];
  126. $prop["right"] = $bbox[2];
  127. $prop["top"] = $bbox[1];
  128. $prop["bottom"] = $bbox[7];
  129. $padding = 2;
  130. $prop["width"] = abs($prop["left"]) + abs($prop["right"]) + $padding;
  131. $prop["height"] = abs($prop["top"]) + abs($prop["bottom"]) + $padding;
  132. return $prop;
  133. }
  134. /*___| save to image file |___*/
  135. public function save($gdimage, $filename, $attributes=false)
  136. {
  137. $this->type = "png";
  138. $this->chmod = 0777;
  139. $this->bg_color = "#ffffff";
  140. $this->quality = 95;
  141. $this->antialias = true;
  142. $this->setprop ( $attributes );
  143. // process
  144. switch ( strtolower ( $this->type ) )
  145. {
  146. case "jpeg":
  147. case "jpg":
  148. $gdimage = $this->createbackground($gdimage, imagesx($gdimage), imagesy($gdimage));
  149. imagejpeg ( $gdimage, $filename, $this->quality );
  150. break;
  151. case "gif":
  152. $gdimage = $this->createbackground($gdimage, imagesx($gdimage), imagesy($gdimage));
  153. imagegif ( $gdimage, $filename );
  154. break;
  155. case "png":
  156. default :
  157. imagepng ( $gdimage, $filename );
  158. break;
  159. }
  160. chmod ( $filename, $this->chmod );
  161. }
  162. /*___| create gd background image |___*/
  163. public function createbackground($gdimage, $width, $height)
  164. {
  165. $img = imagecreatetruecolor ( $width, $height );
  166. $rgb = $this->getrgb ( $this->bg_color );
  167. $color = imagecolorallocate ( $img, $rgb["red"], $rgb["green"], $rgb["blue"] );
  168. imagefill ( $img, 0, 0, $color );
  169. imagecopyresampled ( $img, $gdimage, 0, 0, 0, 0, $width, $height, $width, $height );
  170. return $img;
  171. }
  172. /*___| create gd transparent image |___*/
  173. public function createtransparent($width, $height)
  174. {
  175. $img = imagecreatetruecolor($width, $height);
  176. imagealphablending($img, false);
  177. imagesavealpha($img, true);
  178. $transparent = imagecolorallocatealpha($img, 0, 0, 0, 127);
  179. imagefilledrectangle($img, 0, 0, $width, $height, $transparent);
  180. imagecopyresampled($img, $img, 0, 0, 0, 0, $width, $height, $width, $height);
  181. return $img;
  182. }
  183. /*___| load image |___*/
  184. public function createimagefrom($filename, $alpha=true)
  185. {
  186. if ( function_exists ( "exif_imagetype" ) )
  187. {
  188. if ( exif_imagetype ( $filename ) == imagetype_jpeg ) { return $this->createfromjpeg ( $filename ); }
  189. else if ( exif_imagetype ( $filename ) == imagetype_gif ) { return $this->createfromgif ( $filename ); }
  190. else if ( exif_imagetype ( $filename ) == imagetype_png ) { return $this->createfrompng ( $filename, $alpha ); }
  191. }
  192. else
  193. {
  194. if ( strstr ( strtoupper ( $filename ) , ".jpg" ) || strstr ( strtoupper ( $filename ), ".jpeg" )) { return $this->createfromjpeg ( $filename ); }
  195. else if ( strstr ( strtoupper ( $filename ) , ".gif" )) { return $this->createfromgif ( $filename ); }
  196. else if ( strstr ( strtoupper ( $filename ) , ".png" )) { return $this->createfrompng ( $filename, $alpha ); }
  197. }
  198. return false;
  199. }
  200. private function createfromjpeg ( $filename ) { return imagecreatefromjpeg ( $filename ); }
  201. private function createfromgif ( $filename ) { return imagecreatefromgif ( $filename ); }
  202. private function createfrompng ( $filename, $alpha=true )
  203. {
  204. if ( $alpha )
  205. {
  206. list ( $width, $height ) = getimagesize ( $filename );
  207. $png_img = imagecreatefrompng ( $filename );
  208. $img = imagecreatetruecolor ( $width, $height );
  209. imagealphablending ( $img, false );
  210. imagesavealpha ( $img, true );
  211. imagecopyresampled ( $img, $png_img, 0, 0, 0, 0, $width, $height, $width, $height );
  212. } else {
  213. $img = imagecreatefrompng ( $filename );
  214. }
  215. return $img;
  216. }
  217. /*___| attach background image |___*/
  218. public function attachbackgroundimage ( $gdimage, $filename, $attributes=false )
  219. {
  220. $this->x = 0;
  221. $this->y = 0;
  222. $this->setprop ( $attributes );
  223. $img = $this->createimagefrom ( $filename );
  224. imagecopyresampled ( $img, $gdimage, $this->x, $this->y, 0, 0, imagesx($gdimage), imagesy($gdimage), imagesx($gdimage), imagesy($gdimage) );
  225. return $img;
  226. }
  227. /*___| attach image |___*/
  228. public function attachimage ( $source, $target, $filename, $image_attributes=false, $attributes=false )
  229. {
  230. $source_img = $this->createimagefrom ( $source );
  231. $target_img = $this->attachbackgroundimage ( $source_img, $target, $attributes );
  232. $this->save ( $target_img, $filename, $image_attributes );
  233. }
  234. /*___| attach gd image resource |___*/
  235. public function attachgdimage ( $gd_source, $gd_target, $attributes=false )
  236. {
  237. $this->x = 0;
  238. $this->y = 0;
  239. $this->width = 0;
  240. $this->height = 0;
  241. $this->setprop ( $attributes );
  242. imagealphablending($gd_target, true );
  243. imagealphablending($gd_source, true );
  244. imagecopy ( $gd_target, $gd_source, $this->x, $this->y, 0, 0, imagesx($gd_source), imagesy($gd_source) );
  245. return $gd_target;
  246. }
  247. /*___| get rgb color |___*/
  248. public function getrgb($hex)
  249. {
  250. $rgb["red"] = hexdec ( substr ( $hex, 1, 2 ) ) ;
  251. $rgb["green"] = hexdec ( substr ( $hex, 3, 2 ) ) ;
  252. $rgb["blue"] = hexdec ( substr ( $hex, 5, 2 ) ) ;
  253. return $rgb;
  254. }
  255. /*___| set properties |___*/
  256. private function setprop ( $attributes=false )
  257. {
  258. if ( $attributes ) { foreach ( $attributes as $key => $value ) { $k = strtoupper ( $key ); $this->$k = $value; } }
  259. }
  260. }
  261. //开源代码phpfensi.com
  262. //调用 方法代码如下:
  263. $imgresize = new imagetransform();
  264. $imgresize->sourcefile = $source.$file;
  265. $imgresize->targetfile = $destination.$file;
  266. $imgresize->chmodvalue = 0777;
  267. $imgresize->resizetowidth = $tw;
  268. $imgresize->resizetoheight = $th;
  269. $imgresize->jpegoutputquality = 100;
  270. $imgresize->resizeifsmaller = false;
  271. $imgresize->resize();