Exemple #1
0
 /**
  * Modify image opacity. To work correctly image must be saved in PNG or GIF format.
  *
  * @param   mixed    $alpha  Default is float in range [0-1], however setting omega allows for custom input range.
  * @param   integer  $omega  Adjustment sensitivity. User may enter +/- this value and the output will be normalized for safe function usage
  * @return  mixed            On success, current object to allow function chaining. False otherwise.
  */
 public function alpha($alpha = 0.5, $omega = false)
 {
     # Normalize input if desired.
     if ($omega !== false) {
         $alpha = Number::normalize($alpha, 0, 1, $omega);
     }
     # Convert CSS alpha to GD ranges
     $alpha = Color::alpha2gd(Number::minmax($alpha, 0, 1));
     # Set image boundaries
     $width = imagesx($this->img);
     $height = imagesy($this->img);
     for ($x = 0; $x < $width; $x++) {
         for ($y = 0; $y < $height; $y++) {
             # Extract Bits
             $rgb = imagecolorat($this->img, $x, $y);
             $r = $rgb >> 16 & 0xff;
             $g = $rgb >> 8 & 0xff;
             $b = $rgb & 0xff;
             $a = ($rgb & 0xff000000) >> 24;
             # Modify alpha
             $a += $alpha;
             # Boundary checks
             $a = $a > 127 ? 127 : ($a < 0 ? 0 : $a);
             # Inject pixel with alpha
             if (!imagesetpixel($this->img, $x, $y, imagecolorallocatealpha($this->img, $r, $g, $b, $a))) {
                 return false;
             }
         }
     }
     # Apply filter. On success update filename and return current object for function chaining, false on error.
     return $this->update(__FUNCTION__, func_get_args());
 }