PHP generated thumbnails (text converted to graphics) _php tutorial

Source: Internet
Author: User
Tags transparent image word wrap
PHP generated thumbnails (text converted to graphics) This is a version of the classic PHP generated thumbnail class function, the file is flexible and practical, can generate any style of the picture, and can convert the text into graphics.

PHP tutorials Generate thumbnails (text converted to graphics)
This is a version of the classic PHP generated thumbnail class function, the file is flexible and practical, can generate any style of the picture, and can be converted to graphics.

Copyright:smart-info Limited. All right reserved.
Author:jacky Cheung
version:1.1
Create date:24 September 2008
Last modify:15 March 2009
*/

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, "
");
$text = Split ("
", $text);
} else if ($all = = = True) {
$temp = Array ();
for ($i =0; $i 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->fo Nt_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-& Gt;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->crea Tefromjpeg ($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;}}
}
}

Calling methods

$imgresize = new Imagetransform ();
$imgresize->sourcefile = $source. $file;
$imgresize->targetfile = $destination. $file;
$imgresize->chmodvalue = 0777;
$imgresize->resizetowidth = $TW;
$imgresize->resizetoheight = $th;
$imgresize->jpegoutputquality = 100;
$imgresize->resizeifsmaller = false;
$imgresize->resize ();

?>

http://www.bkjia.com/PHPjc/633025.html www.bkjia.com true http://www.bkjia.com/PHPjc/633025.html techarticle PHP generated thumbnails (text converted to graphics) This is a version of the classic PHP generated thumbnail class function, the file is flexible and practical, can generate any style of the picture, and can put the text ...

  • 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.