protected function getTransformation(AbstractFileEntity $entity, $file, $width, $height, $mode, $optimize)
 {
     $transformation = new \Imagine\Filter\Transformation();
     if ($optimize) {
         // Optimize for web and rotate the images (after thumbnail creation).
         $transformation->add(new \Imagine\Filter\Basic\Autorotate(), 101)->add(new \Imagine\Filter\Basic\WebOptimization(), 2);
     }
     $watermark = $entity->getCollection()->getWatermark();
     if ($watermark === null) {
         // The image shall not be watermarked.
         return $transformation;
     }
     $imagine = $this->imagineManager->getImagine();
     // Check whether the image is big enough to be watermarked.
     if ($watermark->getMinSizeX() !== null && $width < $watermark->getMinSizeX()) {
         return $transformation;
     }
     if ($watermark->getMinSizeY() !== null && $height < $watermark->getMinSizeY()) {
         return $transformation;
     }
     // Generate the watermark image. It will already be correctly sized
     // for the thumbnail.
     if ($mode == 'outbound') {
         $wWidth = $width;
         $wHeight = $height;
     } elseif ($mode == 'inset') {
         $imageSize = getimagesize($file);
         $ratios = [$width / $imageSize[0], $height / $imageSize[1]];
         $wWidth = min($ratios) * $imageSize[0];
         $wHeight = min($ratios) * $imageSize[1];
     } else {
         throw new \LogicException();
     }
     $watermarkImage = $watermark->getImagineImage($imagine, $this->fontCollection, $wWidth, $wHeight);
     $watermarkSize = $watermarkImage->getSize();
     // Calculate watermark position. If the position is negative, handle
     // it as an offset from the bottom / the right side of the image.
     $x = $watermark->getPositionX();
     $y = $watermark->getPositionY();
     if ($x < 0) {
         $x += $wWidth - $watermarkSize->getWidth();
     }
     if ($y < 0) {
         $y += $wHeight - $watermarkSize->getHeight();
     }
     // If the watermark still exceeds the image's width or height, do
     // not watermark the image.
     // @todo Probably resize watermark instead?
     if ($x < 0 || $y < 0 || $x + $watermarkSize->getWidth() > $wWidth || $y + $watermarkSize->getHeight() > $wHeight) {
         return $transformation;
     }
     $point = new \Imagine\Image\Point($x, $y);
     $transformation->add(new \Imagine\Filter\Basic\Paste($watermarkImage, $point), 100);
     return $transformation;
 }
Example #2
0
 public function processImage($input, $output, $options = array())
 {
     if ($this->debug) {
         $startTime = microtime(true);
     }
     if (!is_readable($input)) {
         $this->debugmessages[] = 'File not ' . (file_exists($input) ? 'readable' : 'found') . ": {$input}  *** Skipping ***";
         return false;
     }
     $this->width = $this->height = null;
     if (is_string($options)) {
         $options = parse_str($options);
     }
     // convert an options string to an array if needed
     $inputParams = array('options' => $options);
     $outputType = pathinfo($output, PATHINFO_EXTENSION);
     $outputIsJpg = strncasecmp('jp', $outputType, 2) === 0;
     try {
         /* initial dimensions */
         $image = $this->imagine->open($input);
         $size = $image->getSize();
         $origWidth = $inputParams['width'] = $size->getWidth();
         $origHeight = $inputParams['height'] = $size->getHeight();
         if (self::$maxsize && $origWidth * $origHeight > self::$maxsize) {
             // if we're using GD we need to check the image will fit in memory
             $this->debugmessages[] = "GD: {$input} may exceed available memory  ** Skipping **";
             return false;
         }
         /* source crop (start) */
         if (isset($options['sw']) || isset($options['sh'])) {
             if (empty($options['sw']) || $options['sw'] > $origWidth) {
                 $newWidth = $origWidth;
             } else {
                 $newWidth = $options['sw'] < 1 ? round($origWidth * $options['sw']) : $options['sw'];
                 // sw < 1 is a %, >= 1 in px
             }
             if (empty($options['sh']) || $options['sh'] > $origHeight) {
                 $newHeight = $origHeight;
             } else {
                 $newHeight = $options['sh'] < 1 ? round($origHeight * $options['sh']) : $options['sh'];
             }
             if ($newWidth !== $origWidth || $newHeight !== $origHeight) {
                 // only if something will actually be cropped
                 if (empty($options['sx'])) {
                     $cropStartX = isset($options['sx']) ? $options['sx'] : (int) (($origWidth - $newWidth) / 2);
                     // 0 or center
                 } else {
                     $cropStartX = $options['sx'] < 1 ? round($origWidth * $options['sx']) : $options['sx'];
                     if ($cropStartX + $newWidth > $origWidth) {
                         $cropStartX = $origWidth - $newWidth;
                     }
                     // crop box can't go past the right edge
                 }
                 if (empty($options['sy'])) {
                     $cropStartY = isset($options['sy']) ? $options['sy'] : (int) (($origHeight - $newHeight) / 2);
                 } else {
                     $cropStartY = $options['sy'] < 1 ? round($origHeight * $options['sy']) : $options['sy'];
                     if ($cropStartY + $newHeight > $origHeight) {
                         $cropStartY = $origHeight - $newHeight;
                     }
                 }
                 $scBox = new Box($newWidth, $newHeight);
                 $origWidth = $newWidth;
                 // update input dimensions to the new cropped size
                 $origHeight = $newHeight;
             }
         }
         $origAR = $origWidth / $origHeight;
         // original image aspect ratio
         $origAspect = round($origAR, 2);
         /* input dimensions */
         // use width/height if specified
         if (isset($options['w'])) {
             $width = $options['w'];
         }
         if (isset($options['h'])) {
             $height = $options['h'];
         }
         // override with any orientation-specific dimensions
         if ($origAspect > 1) {
             // landscape
             if (isset($options['wl'])) {
                 $width = $options['wl'];
             }
             if (isset($options['hl'])) {
                 $height = $options['hl'];
             }
         } elseif ($origAspect < 1) {
             // portrait
             if (isset($options['wp'])) {
                 $width = $options['wp'];
             }
             if (isset($options['hp'])) {
                 $height = $options['hp'];
             }
         } else {
             // square
             if (isset($options['ws'])) {
                 $width = $options['ws'];
             }
             if (isset($options['hs'])) {
                 $height = $options['hs'];
             }
         }
         // fill in a missing dimension
         $bothDims = true;
         if (empty($width)) {
             if (empty($height)) {
                 $height = $origHeight;
                 $width = $origWidth;
             } else {
                 $width = $height * $origAR;
             }
             $bothDims = false;
         }
         if (empty($height)) {
             $height = $width / $origAR;
             $bothDims = false;
         }
         $newAR = $width / $height;
         /* scale */
         if (empty($options['scale'])) {
             $requestedMP = $width * $height;
             // we'll need this for quality scaling
         } else {
             $requestedMP = $width * $options['scale'] * $height * $options['scale'];
             if (empty($options['aoe'])) {
                 // if aoe is off, cap scale so image isn't enlarged
                 $hScale = $origHeight / $height;
                 $wScale = $origWidth / $width;
                 $wRequested = $width * $options['scale'];
                 $hRequested = $height * $options['scale'];
                 $options['scale'] = $hScale > 1 && $wScale > 1 ? min($hScale, $wScale, $options['scale']) : 1;
             }
             $options['w'] = $width *= $options['scale'];
             $options['h'] = $height *= $options['scale'];
         }
         if (empty($options['zc']) || !$bothDims) {
             /* non-zc sizing */
             $newAspect = round($newAR, 2);
             // ignore small differences
             if ($newAspect < $origAspect) {
                 // Make sure AR doesn't change. Smaller dimension...
                 if ($origWidth < $options['w'] && empty($options['aoe'])) {
                     $options['w'] = $width = $origWidth;
                     $options['h'] = $width / $newAR;
                 }
                 $height = $width / $origAR;
             } elseif ($newAspect > $origAspect) {
                 // ...limits larger
                 if ($origHeight < $options['h'] && empty($options['aoe'])) {
                     $options['h'] = $height = $origHeight;
                     $options['w'] = $height * $newAR;
                 }
                 $width = $height * $origAR;
             }
             $width = round($width);
             // clean up
             $height = round($height);
             /* far */
             if (!empty($options['far']) && $bothDims) {
                 $options['w'] = round($options['w']);
                 $options['h'] = round($options['h']);
                 if ($options['w'] > $width || $options['h'] > $height) {
                     $farPoint = Reductionist::startPoint($options['far'], array($options['w'], $options['h']), array($width, $height));
                     $farBox = new Box($options['w'], $options['h']);
                     $this->width = $options['w'];
                     $this->height = $options['h'];
                 }
             }
         } else {
             /* zc */
             if (empty($options['aoe'])) {
                 // if the crop box is bigger than the original image, scale it down
                 if ($width > $origWidth) {
                     $height = $origWidth / $newAR;
                     $width = $origWidth;
                 }
                 if ($height > $origHeight) {
                     $width = $origHeight * $newAR;
                     $height = $origHeight;
                 }
             }
             // make sure final image will cover the crop box
             $width = round($width);
             $height = round($height);
             $newWidth = round($height * $origAR);
             $newHeight = round($width / $origAR);
             if ($newWidth > $width) {
                 // needs horizontal cropping
                 $newHeight = $height;
             } elseif ($newHeight > $height) {
                 // needs vertical cropping
                 $newWidth = $width;
             } else {
                 // no cropping needed, same AR
                 $newWidth = $width;
                 $newHeight = $height;
             }
             $cropStart = Reductionist::startPoint($options['zc'], array($newWidth, $newHeight), array($width, $height));
             $cropBox = new Box($width, $height);
             $this->width = $width;
             $this->height = $height;
             $width = $newWidth;
             $height = $newHeight;
         }
         /* source crop (finish) */
         if (isset($scBox)) {
             $scale = max($width / $origWidth, $height / $origHeight);
             if ($scale <= 0.5 && $this->gLib && $image->getFormat() === IMG_JPG) {
                 $image->resize(new Box(round($inputParams['width'] * $scale), round($inputParams['height'] * $scale)));
                 $scStart = new \Imagine\Image\Point(round($cropStartX * $scale), round($cropStartY * $scale));
                 $scBox = new Box(round($scBox->getWidth() * $scale), round($scBox->getHeight() * $scale));
             } else {
                 $scStart = new \Imagine\Image\Point($cropStartX, $cropStartY);
             }
             $image->crop($scStart, $scBox);
             if (abs($scBox->getWidth() - $width) == 1) {
                 // snap a 1px rounding error to the source crop box
                 $this->width = $width = $scBox->getWidth();
             }
             if (abs($scBox->getHeight() - $height) == 1) {
                 $this->height = $height = $scBox->getHeight();
             }
         }
         /* resize, aoe */
         if ($didScale = $width < $origWidth && $height < $origHeight || !empty($options['aoe'])) {
             $imgBox = new Box($width, $height);
             $image->resize($imgBox);
         } elseif (isset($options['qmax']) && empty($options['aoe']) && isset($options['q']) && $outputIsJpg) {
             // undersized input image. We'll increase q towards qmax depending on how much it's undersized
             $sizeRatio = $requestedMP / (isset($cropBox) ? $this->width * $this->height : $width * $height);
             if ($sizeRatio >= 2) {
                 $options['q'] = $options['qmax'];
             } elseif ($sizeRatio > 1) {
                 $options['q'] += round(($options['qmax'] - $options['q']) * ($sizeRatio - 1));
             }
         }
         /* crop */
         if (isset($cropBox)) {
             $image->crop($cropStart, $cropBox);
         }
         /* filters (start) */
         if (!empty($options['fltr'])) {
             if (!is_array($options['fltr'])) {
                 $options['fltr'] = array($options['fltr']);
                 // in case somebody did fltr= instead of fltr[]=
             }
             $transformation = new \Imagine\Filter\Transformation($this->imagine);
             $filterlog = array($this->debug);
             foreach ($options['fltr'] as $fltr) {
                 $filter = explode('|', $fltr);
                 if ($filter[0] === 'usm') {
                     // right now only unsharp mask is implemented, sort of
                     $image->effects()->sharpen();
                     // radius, amount and threshold are ignored!
                 } elseif ($filter[0] === 'wmt' || $filter[0] === 'wmi') {
                     $doApply = true;
                     $transformation->add(new Filter\Watermark($filter, $filterlog));
                 }
             }
         }
         /* bg */
         if ($hasBG = isset($options['bg']) && !$outputIsJpg || isset($farBox)) {
             if (self::$palette === null) {
                 self::$palette = new \Imagine\Image\Palette\RGB();
             }
             if (isset($options['bg'])) {
                 $bgColor = explode('/', $options['bg']);
                 $bgColor[1] = isset($bgColor[1]) ? $bgColor[1] : 100;
             } else {
                 $bgColor = array(array(255, 255, 255), 100);
             }
             $backgroundColor = self::$palette->color($bgColor[0], 100 - $bgColor[1]);
             if (isset($cropBox)) {
                 $bgBox = $cropBox;
             } elseif (isset($farBox)) {
                 $bgBox = $farBox;
             } elseif (isset($imgBox)) {
                 $bgBox = $imgBox;
             } else {
                 $bgBox = new Box($width, $height);
             }
             $image = $this->imagine->create($bgBox, self::$palette->color($bgColor[0], 100 - $bgColor[1]))->paste($this->gLib ? $image->getImage() : $image, isset($farPoint) ? $farPoint : new \Imagine\Image\Point(0, 0));
         }
         /* filters (finish) */
         if (isset($transformation) && !empty($doApply)) {
             // apply any filters
             try {
                 $transformation->apply($image);
             } catch (\Exception $e) {
                 $this->debugmessages[] = $e->getMessage();
             }
         }
         /* debug info */
         if ($this->debug) {
             $debugTime = microtime(true);
             $this->debugmessages[] = 'Input options:' . self::formatDebugArray($inputParams['options']);
             // print all options, stripping off array()
             $changed = array();
             // note any options which may have been changed during processing
             foreach (array('w', 'h', 'scale', 'q') as $opt) {
                 if (isset($inputParams['options'][$opt]) && $inputParams['options'][$opt] != $options[$opt]) {
                     $changed[$opt] = $options[$opt];
                 }
             }
             if ($changed) {
                 $this->debugmessages[] = 'Modified options:' . self::formatDebugArray($changed, true);
             }
             $this->debugmessages[] = "Original - w: {$inputParams['width']} | h: {$inputParams['height']} " . sprintf("(%2.2f MP)", $inputParams['width'] * $inputParams['height'] / 1000000.0);
             if (isset($image->prescalesize)) {
                 $this->debugmessages[] = "JPEG prescale - w: {$image->prescalesize[0]} | h: {$image->prescalesize[1]} " . sprintf("(%2.2f MP)", $image->prescalesize[0] * $image->prescalesize[1] / 1000000.0);
             }
             if (isset($scBox)) {
                 $this->debugmessages[] = "Source area - start: {$scStart} | box: {$scBox}";
             }
             if (isset($wRequested)) {
                 $this->debugmessages[] = "Requested - w: " . round($wRequested) . ' | h: ' . round($hRequested);
             }
             if (!isset($wRequested) || !$didScale) {
                 $this->debugmessages[] = "New - w: {$width} | h: {$height}" . ($didScale ? '' : ' [Not scaled: same size or insufficient input resolution]');
             }
             if (isset($farPoint)) {
                 $this->debugmessages[] = "FAR - start: {$farPoint} | box: {$options['w']}x{$options['h']} px";
             }
             if (isset($cropBox)) {
                 $this->debugmessages[] = "ZC - start: {$cropStart} | box: {$cropBox}";
             }
             if ($hasBG) {
                 $this->debugmessages[] = "Background color: {$bgColor[0]} | opacity: {$bgColor[1]}";
             }
             $debugTime = microtime(true) - $debugTime;
         }
         if (isset($filterlog[1])) {
             // add any filter debug output
             unset($filterlog[0]);
             $this->debugmessages = array_merge($this->debugmessages, $filterlog);
         }
         /* save */
         $outputOpts = array('jpeg_quality' => empty($options['q']) ? $this->defaultQuality : (int) $options['q'], 'format' => empty($options['f']) ? $outputType : $options['f']);
         $image->save($output, $outputOpts);
         if (!$this->width) {
             $this->width = $width;
         }
         if (!$this->height) {
             $this->height = $height;
         }
     } catch (\Imagine\Exception\Exception $e) {
         $this->debugmessages[] = "Input file: {$input}";
         $this->debugmessages[] = 'Input options: ' . self::formatDebugArray($inputParams['options']);
         $this->debugmessages[] = "*** Error *** {$e->getMessage()}";
         return false;
     }
     /* debug info (timing) */
     if ($this->debug) {
         $this->debugmessages[] = "Wrote {$output}";
         $this->debugmessages[] = "Dimensions: {$this->width}x{$this->height} px";
         $this->debugmessages[] = 'Execution time: ' . round((microtime(true) - $startTime - $debugTime) * 1000.0) . ' ms';
     }
     return true;
 }