background image

classpc_Image {

protected$handle;

functionImageCreate($image) {

if(is_string($image)) {

// simple file type guessing

// grab file suffix

$info=pathinfo($image);

$extension=strtolower($info['extension']);

switch($extension) {

case'jpg':

case'jpeg':

$this->handle = ImageCreateFromJPEG($image);

break;

case'png':

$this->handle = ImageCreateFromPNG($image);

break;

default:

die('Images must be JPEGs or PNGs.');

}

}elseif(is_resource($image)) {

$this->handle =$image;

}else{

die('Variables must be strings or resources.');

}

}

}

在上面的类中,会把任何传递进来的字符串当作文件的位置,进而再使用 pathinfo()

取得文件的扩展名。在取得了扩展名之后,就可以调用不同的 ImageCreateFrom()函数精确

 

地打开图像并创建一个句柄。

如果传递进来的参数不是字符串,就直接作为一个 GD 流来处理,即将其视为一种