【问题标题】:How to wrap all this up into a PHP class?如何将所有这些包装到 PHP 类中?
【发布时间】:2009-11-03 16:10:55
【问题描述】:
$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);

【问题讨论】:

  • 方法太多了。让我试试。

标签: php class


【解决方案1】:

有很多方法可以做到这一点。您通常希望为每个类确定一个职责。然后,您将对类进行编码以尽可能一般地满足该职责。在这种情况下,职责可能是显示添加了一些文本的图像(?)。

这是一种方法:

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

 }

【讨论】:

    【解决方案2】:

    这是一种方法:

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

    【讨论】:

      【解决方案3】:

      这就是我认为你的班级的样子:

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

      【讨论】:

      • - 在您的 process() 方法中,$image 不是可用变量。您需要以 $this->image 的形式访问它 - 方法名称区分大小写,因此您必须调用 $image->process() 而不是 $image->Process() - 实例化对象意味着调用构造函数(即使你自己不写那个函数),所以你需要 $img = new ImageThing(); -- 带括号。
      • 谢谢。我不太熟悉 PHP 中的类是如何工作的,幸好有人回答了这个问题
      猜你喜欢
      • 2019-11-07
      • 2023-03-09
      • 1970-01-01
      • 2014-04-23
      • 2011-05-27
      • 2014-03-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多