php成图,php生成缩略图(文本转换成图形)_PHP教程

这段代码是PHP中一个名为GD的类,主要用于将文本转换成图像。它支持设置字体、颜色、大小、角度、宽度等属性,并能进行文字换行和保存为不同格式的图像(如JPEG、GIF、PNG)。类中包含了创建透明背景、添加文字、调整尺寸、保存图像等方法,适用于生成验证码或者自定义文本图形的需求。
摘要由CSDN通过智能技术生成

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; $itext); $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”]) 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 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; } }

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值