Generate thumbnails in php (convert text to graphics)

Source: Internet
Author: User
Tags abs foreach chmod transparent image word wrap

Copyright: smart-info limited. all right reserved.
Author: jacky cheung
Version: 1.1
Create date: 24 September 2008
Last modify: 15 March2009
*/

Class gd
{
Var $ font_face = "";
Var $ text = "";
Var $ size = 12;
Var $ color = "#000000 ";
Var $ angle = 0;

Var $ width = 0;
Var $ height = 0;
Var $ line_height = 0;
 
Var $ type = "png ";
Var $ chmod = 0777;
Var $ bg_color = "# ffffff ";
Var $ quality = 95;
Var $ antialias = true;
 
Var $ x = 0;
Var $ y = 0;
 

/* ___ | Convert text to image | ___*/
Public function text2image ($ font_face = "", $ text = "", $ attributes = false, $ width = 0, $ all = false)
  {
$ This-> font_face = $ font_face;
$ This-> text = $ text;
$ This-> width = $ width;
$ This-> size = 12;
$ This-> color = "#000000 ";
$ This-> angle = 0;
$ This-> line_height = 0;
$ This-> setprop ($ attributes );
   
If ($ this-> width = 0)
   {
Return $ this-> convert_text2image ($ this-> text, $ this-> check_text_width ($ this-> text ));
} Else {
// Word wrap
If ($ all = false)
    {
$ Text = split ("", $ this-> text );
$ Text = $ this-> word_wrap ($ this-> text, $ this-> width, "<br> ");
$ Text = split ("<br>", $ text );
} Else if ($ all === true ){
$ Temp = array ();
For ($ I = 0; $ I <strlen ($ this-> text); $ I ++)
     {
Array_push ($ temp, mb_substr ($ this-> text, $ I, 1, "UTF-8 "));
     }
$ Text = array ();
$ Count_width = 0;
$ I = 0;
Foreach ($ temp as $ k => $ t)
     {
$ Prop = $ this-> check_text_width ($ t );
If ($ count_width + floatval ($ prop ["width"]) <$ this-> width)
      {
$ Text [$ I] = $ text [$ I]. $ t;
$ Count_width + = floatval ($ prop ["width"]);
} Else {
$ Count_width = 0;
$ I ++;
$ Text [$ I] = "";
      }
     }
    }
    
$ Img = array ();
Foreach ($ text as $ k => $ t)
    {
$ Img [$ k] = $ this-> convert_text2image ($ t, $ this-> check_text_width ($ t ));
    }
    
$ W = 0;
$ H = 0;
Foreach ($ img as $ k => $ v)
    {
$ W = (imagesx ($ img [$ k])> $ w )? Imagesx ($ img [$ k]): $ w;

If ($ this-> line_height = 0) $ h + = imagesy ($ img [$ k]);
Else $ h + = ($ k <count ($ img)-1 )? $ This-> line_height: imagesy ($ img [$ k]);
    }
    
$ Base_img = $ this-> createtransparent ($ w, $ h );
$ Locy = 0;
Foreach ($ img as $ k => $ v)
    {
If ($ k> 0)
     {
$ Locy = ($ this-> line_height = 0 )? $ Locy + imagesy ($ img [$ k]): $ locy + $ this-> line_height;
     }
$ Base_img = $ this-> attachgdimage ($ img [$ k], $ base_img, array ("x" => 0, "y" => $ locy ));
    }
Return $ base_img;
   }
  }
Private function word_wrap ($ str, $ width, $ break)
  {
$ Formatted = '';
$ Position =-1;
$ Prev_position = 0;
$ Last_line =-1;
   
/// Looping the string stop at each space
While ($ position = mb_stripos ($ str, "", ++ $ position, 'utf-8 ')){
If ($ position> $ last_line + $ width + 1 ){
$ Formatted. = mb_substr ($ str, $ last_line + 1, $ prev_position-$ last_line-1, 'utf-8'). $ break;
$ Last_line = $ prev_position;
    }
$ Prev_position = $ position;
   }
   
/// Adding last line without the break
$ Formatted. = mb_substr ($ str, $ last_line + 1, mb_strlen ($ str), 'utf-8 ');
Return $ formatted;
  }


Public function convert_text2image ($ text, $ prop)
  {
$ Im = imagecreatetruecolor ($ prop ["width"], $ prop ["height"]);
$ Rgb = $ this-> getrgb ($ this-> color );
$ Color = imagecolorallocate ($ im, $ rgb ["red"], $ rgb ["green"], $ rgb ["blue"]);
$ Img = $ this-> createtransparent ($ prop ["width"], $ prop ["height"]);
Imagettftext ($ img, $ this-> size, $ this-> angle, 0, $ prop ["height"]-abs ($ prop ["top"]), $ color, $ this-> font_face, $ text );
Return $ img;
  }
Public function check_text_width ($ text)
  {
$ Prop = array ();
$ Bbox = imagettfbbox ($ this-> size, $ this-> angle, $ this-> font_face, $ text );
$ Prop ["left"] = $ bbox [0];
$ Prop ["right"] = $ bbox [2];
$ Prop ["top"] = $ bbox [1];
$ Prop ["bottom"] = $ bbox [7];
$ Padding = 2;
  
$ Prop ["width"] = abs ($ prop ["left"]) + abs ($ prop ["right"]) + $ padding;
$ Prop ["height"] = abs ($ prop ["top"]) + abs ($ prop ["bottom"]) + $ padding;
   
Return $ prop;
  }


/* ___ | Save to image file | ___*/
Public function save ($ gdimage, $ filename, $ attributes = false)
  {
$ This-> type = "png ";
$ This-> chmod = 0777;
$ This-> bg_color = "# ffffff ";
$ This-> quality = 95;
$ This-> antialias = true;
   
$ This-> setprop ($ attributes );
   
// Process
Switch (strtolower ($ this-> type ))
   {
Case "jpeg ":
Case "jpg ":
$ Gdimage = $ this-> createbackground ($ gdimage, imagesx ($ gdimage), imagesy ($ gdimage ));
Imagejpeg ($ gdimage, $ filename, $ this-> quality );
Break;
Case "gif ":
$ Gdimage = $ this-> createbackground ($ gdimage, imagesx ($ gdimage), imagesy ($ gdimage ));
Imagegif ($ gdimage, $ filename );
Break;
Case "png ":
Default:
Imagepng ($ gdimage, $ filename );
Break;
   }
Chmod ($ filename, $ this-> chmod );
  }


/* ___ | Create gd background image | ___*/
Public function createbackground ($ gdimage, $ width, $ height)
  {
$ Img = imagecreatetruecolor ($ width, $ height );
$ Rgb = $ this-> getrgb ($ this-> bg_color );
$ Color = imagecolorallocate ($ img, $ rgb ["red"], $ rgb ["green"], $ rgb ["blue"]);
Imagefill ($ img, 0, 0, $ color );
Imagecopyresampled ($ img, $ gdimage, 0, 0, 0, 0, $ width, $ height, $ width, $ height );
Return $ img;
  }


/* ___ | Create gd transparent image | ___*/
Public function createtransparent ($ width, $ height)
  {
$ Img = imagecreatetruecolor ($ width, $ height );
Imagealphablending ($ img, false );
Imagesavealpha ($ img, true );
$ Transparent = imagecolorallocatealpha ($ img, 0, 0, 0,127 );
Imagefilledrectangle ($ img, 0, 0, $ width, $ height, $ transparent );
Imagecopyresampled ($ img, $ img, 0, 0, 0, 0, $ width, $ height, $ width, $ height );
Return $ img;
  }

 
/* ___ | Load image | ___*/
Public function createimagefrom ($ filename, $ alpha = true)
  {
If (function_exists ("exif_imagetype "))
   {
If (exif_imagetype ($ filename) = imagetype_jpeg) {return $ this-> createfromjpeg ($ filename );}
Else if (exif_imagetype ($ filename) = imagetype_gif) {return $ this-> createfromgif ($ filename );}
Else if (exif_imagetype ($ filename) = imagetype_png) {return $ this-> createfrompng ($ filename, $ alpha );}
   }
Else
   {
If (strstr (strtoupper ($ filename ),". jpg ") | strstr (strtoupper ($ filename ),". jpeg ") {return $ this-> createfromjpeg ($ filename );}
Else if (strstr (strtoupper ($ filename), ". gif") {return $ this-> createfromgif ($ filename );}
Else if (strstr (strtoupper ($ filename), ". png") {return $ this-> createfrompng ($ filename, $ alpha );}
   }
Return false;
  }
Private function createfromjpeg ($ filename) {return imagecreatefromjpeg ($ filename );}
Private function createfromgif ($ filename) {return imagecreatefromgif ($ filename );}
Private function createfrompng ($ filename, $ alpha = true)
  {
If ($ alpha)
   {
List ($ width, $ height) = getimagesize ($ filename );
$ Png_img = imagecreatefrompng ($ filename );
$ Img = imagecreatetruecolor ($ width, $ height );
Imagealphablending ($ img, false );
Imagesavealpha ($ img, true );
Imagecopyresampled ($ img, $ png_img, 0, 0, 0, 0, $ width, $ height, $ width, $ height );
} Else {
$ Img = imagecreatefrompng ($ filename );
   } 
Return $ img;
  }
 
 
/* ___ | Attach background image | ___*/
Public function attachbackgroundimage ($ gdimage, $ filename, $ attributes = false)
  {
$ This-> x = 0;
$ This-> y = 0;
$ This-> setprop ($ attributes );
  
$ Img = $ this-> createimagefrom ($ filename );
Imagecopyresampled ($ img, $ gdimage, $ this-> x, $ this-> y, 0, 0, imagesx ($ gdimage), imagesy ($ gdimage ), imagesx ($ gdimage), imagesy ($ gdimage ));
Return $ img;
  }


/* ___ | Attach image | ___*/
Public function attachimage ($ source, $ target, $ filename, $ image_attributes = false, $ attributes = false)
  {
$ Source_img = $ this-> createimagefrom ($ source );
$ Target_img = $ this-> attachbackgroundimage ($ source_img, $ target, $ attributes );
$ This-> save ($ target_img, $ filename, $ image_attributes );
  }
  
 
/* ___ | Attach gd image resource | ___*/
Public function attachgdimage ($ gd_source, $ gd_target, $ attributes = false)
  {
$ This-> x = 0;
$ This-> y = 0;
$ This-> width = 0;
$ This-> height = 0;
$ This-> setprop ($ attributes );
   
Imagealphablending ($ gd_target, true );
Imagealphablending ($ gd_source, true );
Imagecopy ($ gd_target, $ gd_source, $ this-> x, $ this-> y, 0, 0, imagesx ($ gd_source), imagesy ($ gd_source ));
Return $ gd_target;
  }
 
 
/* ___ | Get rgb color | ___*/
Public function getrgb ($ hex)
  {
$ Rgb ["red"] = hexdec (substr ($ hex, 1, 2 ));
$ Rgb ["green"] = hexdec (substr ($ hex, 3, 2 ));
$ Rgb ["blue"] = hexdec (substr ($ hex, 5, 2 ));
Return $ rgb;
  }
 
 
/* ___ | Set properties | ___*/
Private function setprop ($ attributes = false)
  {
If ($ attributes) {foreach ($ attributes as $ key => $ value) {$ k = strtoupper ($ key); $ this-> $ k = $ value ;}}
  }
}

// Call method

The code is as follows: Copy code
$ Imgresize = new imagetransform ();
$ Imgresize-> sourcefile = $ source. $ file;
$ Imgresize-> targetfile = $ destination. $ file;
$ Imgresize-> chmodvalue = 0777;
$ Imgresize-> resizetowidth = $ tw;
$ Imgresize-> resizetoheight = $ th;
$ Imgresize-> export outputquality = 100;
$ Imgresize-> resizeifsmaller = false;
$ Imgresize-> resize ();

?>

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.