聘我网

新概念招聘3.0

我是新手最近在训练面向对象,问一个class的问题

vote up0vote downstar
$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);

我想将这个封装成一个类,请问我该怎么写我自己写了一个但是错误之多自己都不好意思了,所以请教高手,我也吧我写的贴出来希望那高手帮帮忙,帮我写一个

class image{
    private $image;
    function __construct($image){
        $this->image=$image;
    }
    function  img(){
        $img=getimagesize($this->image);
        switch ($img[2]){
            case 1:
                $im =imagecreatefromgif($this->image);
                break;
            case 2:
                $im =imagecreatefromjpeg($this->image);
                break;
            case 3:
                $im =imagecreatefrompng($this->image);
                break;
        }
        return $im;
    }
    function  imgcreat($w,$h){
           imagecreatetruecolor($w,$h);
    }
    function   imagelocte($c,$c1,$c2){
          ImageColorAllocate($this->img(),$c,$c1,$c2);
    }
    function  iconv($str){
        iconv("gbk","utf-8","'$str'");
    }
    function text($t1,$t2,$t3,$t4,$word){
        imagettftext($this->img(), $t1,$t2,$t3,$t4,$this->imagelocte(),'$word',$this->iconv);
    }
    function head($type){
        header("content-type: image/$type");
          Imagejpeg($this->img);
    }
}

$image=new image("0001.php");

$image->img();
$image->head('jpeg');
 

1 个答复

vote up1vote downcheck

不同的需求可以写出不同的类,这里提供一种:

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

 }
链接

您的回答





不是您要找的问题? 浏览其他含有标签 的问题或者 自己问个.