开发者

How to wrap all this up into a PHP class?

$image='xiaofl.jpg';
$img=getimagesize($image);
switch ($img[2])
{
    case 1:
    $im =imagecreatef开发者_Python百科romgif($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);


There are a lot of ways to do this. You'll generally want to decide on a single responsibility for each of your classes. Then you will code the class to meet that responsibility in as general a way as possible. In this case, the responsibility might be to display an image with some text added(?).

Here's one way to do it:

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

 }


Here's one way:

<?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;
  }

}

?>

Using the class:

<?php

header("content-type: image/jpeg");
$img = new ImageWorker('xiaofl.jpg');
$img->render()->output();

?>


Here's what I'm thinking your class would look like:

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

This is somewhat limited since I'm not sure how you're processing the image.

So, you create the object and then you set what it points to, and then you set what image its pointing to, and then you make it process the file. Here's what the driver file might look like:

<?php
    $img = new ImageThing();
    $image->setImage("images/this.jpg");
    $image->process();
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜