tags:

views:

66

answers:

3
$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);
A: 

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();
?>
chustar
- In your process() method, $image is not an available variable. You'll need to access it as $this->image- Method names are case sensitive, so you'll have to call $image->process() not $image->Process()- Instantiating objects means calling a constructor function (even if you don't write that function yourself), so you'll need $img = new ImageThing(); -- with the parentheses.
Scott Saunders
Thanks. I'm not too familiar with how classes work in PHP, good thing someone who knows answered the question
chustar
+3  A: 

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

 }
Scott Saunders
A: 

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

?>
thephpdeveloper