You can load images using these methods: - open => opens an image from disk by providing an instance of \Webiny\Component\Storage\File\File - load => creates an image from given binary string - create => creates a blank image - resource => create an image from the given resource, e.g. from upload stream
Inheritance: use trait Webiny\Component\StdLib\StdLibTrait
Example #1
0
 public function testOpen()
 {
     // build File mock
     $file = $this->getMockBuilder('\\Webiny\\Component\\Storage\\File\\File')->disableOriginalConstructor()->setMethods(['getAbsolutePath', 'getKey'])->getMock();
     // getAbsolutePath mock
     $file->expects($this->any())->method('getAbsolutePath')->will($this->returnValue(__DIR__ . '/image.gif'));
     // getKey mock
     $file->expects($this->once())->method('getKey')->will($this->returnValue(__DIR__ . '/image.gif'));
     $image = ImageLoader::open($file);
     $this->assertInstanceOf('\\Webiny\\Component\\Image\\ImageInterface', $image);
 }
Example #2
0
 public function provideImage()
 {
     Image::setConfig(realpath(__DIR__ . '/../../' . self::CONFIG));
     // build File mock
     $file = $this->getMockBuilder('\\Webiny\\Component\\Storage\\File\\File')->disableOriginalConstructor()->setMethods(['getAbsolutePath', 'getKey'])->getMock();
     // getAbsolutePath mock
     $file->expects($this->any())->method('getAbsolutePath')->will($this->returnValue(__DIR__ . '/../../image.gif'));
     // getKey mock
     $file->expects($this->once())->method('getKey')->will($this->returnValue(__DIR__ . '/../../image.gif'));
     $image = ImageLoader::open($file);
     return [[$image]];
 }
Example #3
0
 /**
  * Loads the image and returns an instance of Image class.
  *
  * @param string|File    $image             This can either be image file name that corresponds to File $key parameter,
  *                                          or it can be an instance of Webiny\Component\Storage\File\File.
  * @param string|Storage $storage           This can either be the name of the storage service or it can be an
  *                                          instance of Webiny\Component\Storage\Storage.
  *                                          NOTE: This parameter is not required if you pass the $image as
  *                                          instance of Webiny\Component\Storage\File\File.
  *
  * @return ImageInterface
  */
 public function image($image, $storage = 'local')
 {
     if ($image instanceof File) {
         return ImageLoader::load($image->getContents());
     } else {
         if (!$storage instanceof Storage) {
             $storage = ServiceManager::getInstance()->getService('Storage.' . $storage);
         }
         $file = new File($image, $storage);
         return ImageLoader::load($file->getContents());
     }
 }
Example #4
0
 /**
  * This is a method that combines resize, crop and paste methods in order to generate a thumbnail from the given image.
  * The benefit of using this function is that the function can automatically combine crop and resize methods together
  * with the pad feature in order to generate the thumb.
  *
  * @param int         $width     Thumb width.
  * @param int         $height    Thumb height.
  * @param bool|string $cropOrPad If you set this to 'crop' the method will first resize the image to preserve the
  *                               aspect ratio and then it will crop the extra pixels to fit the defined width and height.
  *                               If you set this to 'pad' the method will first do the resize and than
  *                               it wil create a blank image that has the size of defined width and height and fill it
  *                               with $padColor, then it will paste the resized image in the center of the new image.
  * @param null|string $padColor  Parameter that fills the background with the defined color.
  *                               Following formats are acceptable
  *                               - "fff"
  *                               - "ffffff"
  *                               - array(255,255,255)
  *
  * @return $this
  */
 public function thumbnail($width, $height, $cropOrPad = false, $padColor = null)
 {
     // get the aspect ratio
     $currentSize = $this->getSize();
     $ar = round($currentSize['width'] / $currentSize['height'], 3);
     $nar = round($width / $height, 3);
     $newWidth = $width;
     $newHeight = $height;
     if ($ar >= 1) {
         if ($nar > $ar) {
             $newHeight = $width / $ar;
         } else {
             $newWidth = $height * $ar;
         }
     } else {
         if ($nar > $ar) {
             $newHeight = $width / $ar;
         } else {
             $newWidth = $height * $ar;
         }
     }
     $this->resize($newWidth, $newHeight);
     // crop
     if ($cropOrPad == self::CROP) {
         $this->crop($width, $height);
     }
     // pad
     if ($cropOrPad == self::PAD) {
         $padColor = !empty($padColor) ? $padColor : 'ffffff';
         $image = ImageLoader::create($width, $height, $padColor);
         // re-calculate the size based on aspect ratio
         if ($width < $height) {
             $newWidth = $width;
             $newHeight = round($width / $ar, 0);
         } else {
             $newWidth = round($height / $ar, 0);
             $newHeight = $height;
         }
         // center the padded image
         $offsetX = ($width - $newWidth) / 2;
         $offsetY = ($height - $newHeight) / 2;
         // resize the current image
         $this->resize($newWidth, $newHeight);
         $image->paste($this, $offsetX, $offsetY);
         $this->image = $image->getInstance();
         unset($image);
     }
     return $this;
 }