How can I have a nice radial transparent gradient applied to an image with PHP? -


how can 1 specify image , apply radial transparent gradient fades out radially. not have imagemagick installed.

marginal example:

gradient transparent image fade php

thanks function linked @baba able alter script allow semi transparent vignette effect.

<?php class photoeffect {   private $_photolocation;   private $_width;   private $_height;   private $_type;    private $_originalimage;   private $_afterimage;    /**    * load image url in constructor    */   final public function __construct($photolocation)   {     $this->_photolocation = $photolocation;     if (!$size = @getimagesize($this->_photolocation)){       throw new exception('image cannot handled');     }     $this->_width = $size[0];     $this->_height = $size[1];     $this->_type = $size[2];       switch ( $this->_type ) {       case imagetype_gif:         $this->_originalimage = imagecreatefromgif($this->_photolocation);       break;       case imagetype_jpeg:         $this->_originalimage = imagecreatefromjpeg($this->_photolocation);       break;       case imagetype_png:         $this->_originalimage = imagecreatefrompng($this->_photolocation);       break;       default:         throw new exception('unknown image type');     }   }    /**    * destroy created images    */   final private function __destruct() {      if (!empty($this->_originalimage))      {        imagedestroy($this->_originalimage);      }       if (!empty($this->_afterimage))      {        imagedestroy($this->_afterimage);      }    }    /**    * apply vignette effect    */   final public function vignette($sharp=0.4, $level=1, $alpha=1)   {     if (empty($this->_originalimage))     {       throw new exception('no image');     }      if (!is_numeric($sharp) || !($sharp>=0 && $sharp<=10))     {       throw new exception('sharp must between 0 , 10');                 }      if (!is_numeric($level) || !($level>=0 && $level<=1))     {       throw new exception('level must between 0 , 10');                 }      if (!is_numeric($alpha) || !($alpha>=0 && $alpha<=10))     {       throw new exception('alpha must between 0 , 1');              }      $this->_afterimage = imagecreatetruecolor($this->_width, $this->_height);     imagesavealpha($this->_afterimage, true);     $trans_colour = imagecolorallocatealpha($this->_afterimage, 0, 0, 0, 127);     imagefill($this->_afterimage, 0, 0, $trans_colour);       for($x = 0; $x < $this->_width; ++$x){       for($y = 0; $y < $this->_height; ++$y){           $index = imagecolorat($this->_originalimage, $x, $y);         $rgb = imagecolorsforindex($this->_originalimage, $index);          $l = sin(m_pi / $this->_width * $x) * sin(m_pi / $this->_height * $y);         $l = pow($l, $sharp);          $l = 1 - $level * (1 - $l);          $rgb['red'] *= $l;         $rgb['green'] *= $l;         $rgb['blue'] *= $l;         $rgb['alpha'] = 127 - (127 * ($l*$alpha));           $color = imagecolorallocatealpha($this->_afterimage, $rgb['red'], $rgb['green'], $rgb['blue'], $rgb['alpha']);          imagesetpixel($this->_afterimage, $x, $y, $color);         }     }    }     /**    * ouput png correct header    */   final public function outputpng()   {     if (empty($this->_afterimage))     {       if (empty($this->_originalimage))       {         throw new exception('no image');       }       $this->_afterimage = $this->_originalimage;     }      header('content-type: image/png');     imagepng($this->_afterimage);   }    /**    * save png    */   final public function savepng($filename)   {     if (empty($filename)) {         throw new exception('filename required');     }      if (empty($this->_afterimage))     {       if (empty($this->_originalimage))       {         throw new exception('no image');       }       $this->_afterimage = $this->_originalimage;     }      imagepng($this->_afterimage, $filename);   }  }   /**  * how use  */ $effect = new photoeffect('test.jpg'); $effect->vignette(); $effect->outputpng(); ?> 

working phpfiddle image find on server, not big.


Comments