简单的php给图片加水印类

本文介绍了一个简单的PHP类,用于处理png、gif和jpeg格式的图片文件。该类能够获取图片的基本信息,如尺寸和类型,并能加载这些图片进行进一步处理。


   目前支持三种图片格式 png , gif , jpeg 

class image_str
  {
      private $img_src;      //图片路径 eg: 'logo.png'
      
      private $img_info_type;     //construcr    type : string
      private $font_size;         // ...  type : int
      
      private $the_image;         //图像标识符

      private $x;                 // width    int
      private $y;                 // height   int
      
      function  __construct($src)
      { 
          $this->img_src = $src;
          
          $img_info = getimagesize($this->img_src);
          
          $this->x = $img_info[0];
          $this->y = $img_info[1];
          
          $this->img_info_type = $img_info['mime'];
          
          $this->font_size = 2;
      }
      
      function get_info()
      {
          echo  $this->img_info_type;
      }
      
      public function select()
      {
           switch ($this->img_info_type)
           {
               case 'image/png':
                    $this->the_image = imagecreatefrompng($this->img_src);
                    break;
               case 'image/gif':
                    $this->the_image = imagecreatefromgif($this->img_src);
                    break;     
               case 'image/jpeg':
                    $this->the_image = imagecreatefromjpeg($this->img_src);
                    break;
               default :
                    $this->image_error(); 
                          
           }   
           $this->image(); 
      }
      
      private function image()     // main function
      {
          
          if(! $this->the_image )
              {
               $this->the_image = imagecreate(200,300);
               $bg = imagecolorallocate($this->the_image,255,0,0);
              }
              
          $color_str = imagecolorallocate($this->the_image,255,255,255);
          
          imagestring( $this->the_image, $this->font_size, 15, $this->y - 20, "hello,world", $color_str );
      }
      
      public function get_image()
      {
           header('Content-type:'.$this->img_info_type);
           imagepng($this->the_image); 
      }
  }
  
   

    $image = new image_str('logo.png');
    $image->select();
    $image->get_image();
    $image->get_info();

  我不清楚为毛我把 getinfo()的调用放在select()之下的时候就500错误。

  不过无关紧要,get_info()仅是用来测试的。

  好吧,就到这,这很简单


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值