Esempio n. 1
0
 /**
  * Resize to fit fully within the given box, without resizing.  Extra space left around
  * the image will be padded with the background color.
  * @param width
  * @param height
  * @param backgroundColour
  */
 public function paddedResize($width, $height, $backgroundColor = "FFFFFF")
 {
     if (!$this->gd) {
         return;
     }
     $width = round($width);
     $height = round($height);
     // Check that a resize is actually necessary.
     if ($width == $this->width && $height == $this->height) {
         return $this;
     }
     $newGD = imagecreatetruecolor($width, $height);
     // Preserves transparency between images
     imagealphablending($newGD, false);
     imagesavealpha($newGD, true);
     $bg = GD::color_web2gd($newGD, $backgroundColor);
     imagefilledrectangle($newGD, 0, 0, $width, $height, $bg);
     $destAR = $width / $height;
     if ($this->width > 0 && $this->height > 0) {
         // We can't divide by zero theres something wrong.
         $srcAR = $this->width / $this->height;
         // Destination narrower than the source
         if ($destAR > $srcAR) {
             $destY = 0;
             $destHeight = $height;
             $destWidth = round($height * $srcAR);
             $destX = round(($width - $destWidth) / 2);
             // Destination shorter than the source
         } else {
             $destX = 0;
             $destWidth = $width;
             $destHeight = round($width / $srcAR);
             $destY = round(($height - $destHeight) / 2);
         }
         imagecopyresampled($newGD, $this->gd, $destX, $destY, 0, 0, $destWidth, $destHeight, $this->width, $this->height);
     }
     $output = clone $this;
     $output->setImageResource($newGD);
     return $output;
 }