Esempio n. 1
0
 /**
  * Creates blank image.
  *
  * @param int $width Image width (px)
  * @param int $height Image height (px)
  * @param float[]|null $fill Fill color (array with keys `r`, `g`, `b` and optional `a`). If null is provided image 
  *  will be fully transparent.
  * @return Image
  * @throws \Exception
  *
  * @since 1.0.0
  */
 public function blank($width, $height, array $fill = null)
 {
     $bitmap = @imagecreatetruecolor($width, $height);
     if (!$bitmap) {
         throw new \Exception('Can\'t create blank image. Perhaps not enough RAM.');
     }
     $color = Image::allocateColor($bitmap, $fill);
     $colorParts = imagecolorsforindex($bitmap, $color);
     $isTransparent = !empty($colorParts['alpha']);
     @imagealphablending($bitmap, false);
     @imagefill($bitmap, 0, 0, $color);
     return new Image($bitmap, $isTransparent);
 }
Esempio n. 2
0
 /**
  * Insert other image into this image.
  *
  * @param self $image Inserted image
  * @param int $dstX X-position of the inserted image on this image (px)
  * @param int $dstY Y-position of the inserted image on this image (px)
  * @param int|null $dstWidth New width of the taken area from the inserted image. If null, will be the same as
  *  $srcWidth.
  * @param int|null $dstHeight New height of the taken area from the inserted image. If null, will be the same as
  *  $srcHeight.
  * @param float $opacity Opacity of the inserted image. 0 — fully transparent, 1 — fully opaque.
  * @param int $srcX X-position of the taken area from the inserted image. Default 0.
  * @param int $srcY Y-position of the taken area from the inserted image. Default 0.
  * @param int|null $srcWidth Width of the taken area of the inserted image. If null, the full width is taken.
  * @param int|null $srcHeight Height of the taken area of the inserted image. If null, the full height is taken.
  * @return static
  * @throws \Exception
  *
  * @since 1.0.0
  */
 function insertImage(self $image, $dstX = 0, $dstY = 0, $dstWidth = null, $dstHeight = null, $opacity = 1.0, $srcX = 0, $srcY = 0, $srcWidth = null, $srcHeight = null)
 {
     if (!is_numeric($dstX)) {
         $dstX = 0;
     }
     if (!is_numeric($dstY)) {
         $dstY = 0;
     }
     if (!is_numeric($srcX)) {
         $srcX = 0;
     }
     if (!is_numeric($srcY)) {
         $srcY = 0;
     }
     if (!is_numeric($srcWidth)) {
         $srcWidth = $image->getWidth() - $srcX;
     }
     if (!is_numeric($srcHeight)) {
         $srcHeight = $image->getHeight() - $srcY;
     }
     if (!is_numeric($dstWidth)) {
         $dstWidth = $srcWidth;
     }
     if (!is_numeric($dstHeight)) {
         $dstHeight = $srcHeight;
     }
     if (!is_numeric($opacity)) {
         $opacity = 1;
     }
     if ($opacity <= 0) {
         return $this;
     }
     if ($opacity != 1) {
         if ($srcX !== 0 || $srcY !== 0 || $srcWidth !== $image->getWidth() || $srcHeight !== $image->getHeight()) {
             $image = $image->crop($srcX, $srcY, $srcWidth, $srcHeight);
             $srcX = 0;
             $srcY = 0;
         }
         // The setOpacity method is expensive, so we have to use it on the inserted image when it has the minimum
         // size. Here, before setting opacity, we find out if it has less pixels after resizing and resize it if
         // it does.
         if ($srcWidth * $srcHeight > $dstWidth * $dstHeight) {
             $image = $image->resize($dstWidth, $dstHeight, true, static::SIZING_EXEC);
             $srcWidth = $dstWidth;
             $srcHeight = $dstHeight;
         }
         $image = $image->setOpacity($opacity);
     }
     $bitmap = static::copyBitmap($this->bitmap);
     @imagealphablending($bitmap, true);
     if ($srcWidth === $dstWidth && $srcHeight === $dstHeight) {
         $result = @imagecopy($bitmap, $image->bitmap, $dstX, $dstY, $srcX, $srcY, $srcWidth, $srcHeight);
     } else {
         $result = @imagecopyresampled($bitmap, $image->bitmap, $dstX, $dstY, $srcX, $srcY, $dstWidth, $dstHeight, $srcWidth, $srcHeight);
     }
     if (!$result) {
         throw new \Exception('Can\'t insert image due to an unknown reason.');
     }
     $newImage = static::construct($bitmap);
     $newImage->isTransparent = $this->isTransparent;
     return $newImage;
 }