目前支持三种图片格式 png , gif , jpeg
class image_str
{
private $img_src; //图片路径 eg: 'logo.png'
private $img_info_type; //construcr type : string
private $font_size; // ... type : int
private $the_image; //图像标识符
private $x; // width int
private $y; // height int
function __construct($src)
{
$this->img_src = $src;
$img_info = getimagesize($this->img_src);
$this->x = $img_info[0];
$this->y = $img_info[1];
$this->img_info_type = $img_info['mime'];
$this->font_size = 2;
}
function get_info()
{
echo $this->img_info_type;
}
public function select()
{
switch ($this->img_info_type)
{
case 'image/png':
$this->the_image = imagecreatefrompng($this->img_src);
break;
case 'image/gif':
$this->the_image = imagecreatefromgif($this->img_src);
break;
case 'image/jpeg':
$this->the_image = imagecreatefromjpeg($this->img_src);
break;
default :
$this->image_error();
}
$this->image();
}
private function image() // main function
{
if(! $this->the_image )
{
$this->the_image = imagecreate(200,300);
$bg = imagecolorallocate($this->the_image,255,0,0);
}
$color_str = imagecolorallocate($this->the_image,255,255,255);
imagestring( $this->the_image, $this->font_size, 15, $this->y - 20, "hello,world", $color_str );
}
public function get_image()
{
header('Content-type:'.$this->img_info_type);
imagepng($this->the_image);
}
}
$image = new image_str('logo.png');
$image->select();
$image->get_image();
$image->get_info();我不清楚为毛我把 getinfo()的调用放在select()之下的时候就500错误。
不过无关紧要,get_info()仅是用来测试的。
好吧,就到这,这很简单
本文介绍了一个简单的PHP类,用于处理png、gif和jpeg格式的图片文件。该类能够获取图片的基本信息,如尺寸和类型,并能加载这些图片进行进一步处理。
466

被折叠的 条评论
为什么被折叠?



