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
$prop = $this->check_text_width( $this->TEXT);
if ( $prop["width"] > $this->WIDTH )
{
$text = split(" ", $this->TEXT);
$text = $this->word_wrap($this->TEXT, $this->WIDTH, "
");
$text = split("
", $text );
$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;
} else {
return $this->convert_text2image ( $this->TEXT, $this->check_text_width( $this->TEXT ) );
}
}
}
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; } }
}
}
?>