首页 > 语言 > PHP > 正文

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

2024-09-04 11:44:20
字体:大 中 小
来源:转载
供稿:网友

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表