public function transform(CGImageBase $src)
 {
     $width = $src['width'];
     $height = $src['height'];
     $radius = $this->_radius;
     $type = $src['type'];
     $color = null;
     $r = $g = $b = 255;
     if ($src->supports_transparency() && !$this->_color) {
         $this->_color = 'transparent';
     }
     if ($this->_color == 'transparent') {
         $type = 'image/png';
     }
     $_dest = new CGImageBase(array($type, $width, $height));
     imagecopy($_dest['rsrc'], $src['rsrc'], 0, 0, 0, 0, $width, $height);
     if ($this->_color) {
         if ($this->_color == 'transparent') {
             // get the dest imagages transparent color.
             $color = $_dest['transparent'];
             if (!$color) {
                 list($r, $g, $b) = cgsi_utils::find_unused_color($_dest);
             }
         } else {
             // use the specified rgb.
             list($r, $g, $b) = cgsi_utils::color_to_rgb($this->_color);
         }
     }
     if (!$color) {
         if ($this->_color == 'transparent') {
             $color = imagecolorallocatealpha($_dest['rsrc'], $r, $g, $b, 127);
         } else {
             $color = imagecolorallocate($_dest['rsrc'], $r, $g, $b);
         }
     }
     if ($this->_color == 'transparent') {
         $_dest['transparent'] = $color;
         imagecolortransparent($_dest['rsrc'], $color);
         imagealphablending($_dest['rsrc'], FALSE);
     }
     // round the corners.
     imagearc($_dest['rsrc'], $radius - 1, $radius - 1, $radius * 2, $radius * 2, 180, 270, $color);
     imagefilltoborder($_dest['rsrc'], 0, 0, $color, $color);
     imagearc($_dest['rsrc'], $width - $radius, $radius - 1, $radius * 2, $radius * 2, 270, 0, $color);
     imagefilltoborder($_dest['rsrc'], $width - 1, 0, $color, $color);
     imagearc($_dest['rsrc'], $radius - 1, $height - $radius, $radius * 2, $radius * 2, 90, 180, $color);
     imagefilltoborder($_dest['rsrc'], 0, $height - 1, $color, $color);
     imagearc($_dest['rsrc'], $width - $radius, $height - $radius, $radius * 2, $radius * 2, 0, 90, $color);
     imagefilltoborder($_dest['rsrc'], $width - 1, $height - 1, $color, $color);
     return $_dest;
 }
 public function transform(CGImageBase $src)
 {
     // quick optimization (nothing to do)
     //if( $this->_dest_w == $src['width'] && $this->_dest_h == $src['height'] ) return $src;
     $type = $src['type'];
     if ($this->_alpha == 127) {
         $this->_color = 'transparent';
     }
     if ($src->supports_transparency() && !$this->_color) {
         $this->_color = 'transparent';
     }
     if ($this->_color == 'transparent' || $this->_alpha > 0) {
         $type = 'image/png';
     }
     if ($this->_color == 'transparent') {
         $this->_alpha = 127;
     }
     // create our destination image.
     $_dest = new CGImageBase(array($type, $this->_dest_w, $this->_dest_h));
     // fill our image with a color (or transparent)
     $color = null;
     if ($this->_color) {
         if ($this->_color == 'transparent') {
             // need to get a transparent color from someplace
             $color = $_dest['transparent'];
         } else {
             $_r = $_g = $_b = 255;
             list($_r, $_g, $_b) = cgsi_utils::color_to_rgb($this->_color);
             $color = imagecolorallocatealpha($_dest['rsrc'], $_r, $_g, $_b, $this->_alpha);
         }
     }
     if ($this->_alpha > 0 && $this->_alpha < 127) {
         $_dest['savealpha'] = TRUE;
         imagealphablending($_dest['rsrc'], TRUE);
         imagesavealpha($_dest['rsrc'], TRUE);
     }
     if ($this->_color == 'transparent') {
         imagecolortransparent($_dest['rsrc'], $color);
         imagealphablending($_dest['rsrc'], FALSE);
     }
     imagefill($_dest['rsrc'], 0, 0, $color);
     if ($this->_dest_w / $this->_dest_h > $src['width'] / $src['height']) {
         // height is greater...
         $new_h = $this->_dest_h;
         $new_w = round($new_h / $src['height'] * $src['width'], 0);
     } else {
         // width is greater.
         $new_w = $this->_dest_w;
         $new_h = round($new_w / $src['width'] * $src['height'], 0);
     }
     $x0 = (int) (($this->_dest_w - $new_w) / 2);
     $y0 = (int) (($this->_dest_h - $new_h) / 2);
     // resize the big image into the temporary transparent image
     $res = imagecopyresampled($_dest['rsrc'], $src['rsrc'], $x0, $y0, 0, 0, $new_w, $new_h, $src['width'], $src['height']);
     if ($res === FALSE) {
         throw new Exception('Resizetofit - stage 1 - failed');
     }
     return $_dest;
 }