$image='xiaofl.jpg';
$img=getimagesize($image);
switch ($img[2])
{
case 1:
$im =imagecreatefromgif($image);
break;
case 2:
$im =imagecreatefromjpeg($image);
break;
case 3:
$im =imagecreatefrompng($image);
break;
}
$word=imagecolorallocate($im,212,0,0);
$str=iconv("gbk","utf-8","php100.com");
imagettftext($im,12,0,20,20,$word,'simkai.ttf',$str);
header("content-type: image/jpeg");
Imagejpeg($im);
答案 0 :(得分:3)
有很多方法可以做到这一点。您通常希望决定每个课程的单一责任。然后,您将以尽可能一般的方式对课程进行编码以满足该职责。在这种情况下,责任可能是显示添加了一些文本的图像(?)。
这是一种方法:
$image='xiaofl.jpg';
$imageDisplay = new ImageDisplay($image);
$imageDisplay->show();
class ImageDisplay
{
private $imageName;
public function __construct($imageName)
{
$this->imageName = $imageName;
}
public function show()
{
$img=getimagesize($this->imageName);
switch ($img[2])
{
case 1:
$im =imagecreatefromgif($this->imageName);
break;
case 2:
$im =imagecreatefromjpeg($this->imageName);
break;
case 3:
$im =imagecreatefrompng($this->imageName);
break;
}
$word=imagecolorallocate($im,212,0,0);
$str=iconv("gbk","utf-8","php100.com");
imagettftext($im,12,0,20,20,$word,'simkai.ttf',$str);
header("content-type: image/jpeg");
Imagejpeg($im);
}
}
答案 1 :(得分:0)
这就是我认为你的课程会是这样的:
class ImageThing{
private $image;
public function setImage($string){
$this->image = $string;
}
public function process{
$img=getimagesize($this->image);
switch ($img[2])
{
case 1:
$im =imagecreatefromgif($image);
break;
case 2:
$im =imagecreatefromjpeg($image);
break;
case 3:
$im =imagecreatefrompng($image);
break;
}
$word=imagecolorallocate($im,212,0,0);
$str=iconv("gbk","utf-8","php100.com");
imagettftext($im,12,0,20,20,$word,'simkai.ttf',$str);
header("content-type: image/jpeg");
Imagejpeg($im);
}
};
这有点受限,因为我不确定你是如何处理图像的。
因此,您创建对象然后设置它指向的对象,然后设置它指向的图像,然后让它处理文件。 以下是驱动程序文件的外观:
<?php
$img = new ImageThing();
$image->setImage("images/this.jpg");
$image->process();
?>
答案 2 :(得分:0)
这是一种方式:
<?php
class ImageWorker{
private $image = '';
private $font = 'simkai.ttf';
private $text = 'php100.com';
private $imgobj = false;
function __construct($imgfile){
$this->image = $imgfile;
$this->init();
}
private function init(){
$img=getimagesize($this->image);
switch($img[2]){
case 1:
$this->imgobj =imagecreatefromgif($this->image);
break;
case 2:
$this->imgobj =imagecreatefromjpeg($this->image);
break;
case 3:
$this->imgobj =imagecreatefrompng($this->image);
break;
}
}
public function render(){
if(!$this->imgobj){return false;}
$word=imagecolorallocate($this->imgobj,212,0,0);
$str=iconv("gbk","utf-8",$this->text);
imagettftext($this->imgobj,12,0,20,20,$word,$this->font,$str);
return $this;
}
public function output(){
if(!$this->imgobj){return false;}
imagejpeg($this->imgobj);
return $this;
}
}
?>
使用课程:
<?php
header("content-type: image/jpeg");
$img = new ImageWorker('xiaofl.jpg');
$img->render()->output();
?>