Exemple #1
0
 /**
  * Returns generated name for a image
  *
  * @param \Imager\ImageInfo $image
  * @return string
  */
 public static function createName(ImageInfo $image)
 {
     $source = $image->getSource() ?: $image;
     $id = $image->getParameter('id');
     if (!isset($id)) {
         $name = md5($source->getPathname());
     } else {
         $name = Strings::before($id, '.', -1);
     }
     // dimensions of image only for thumbnail (has source)
     $width = $image->getParameter('width');
     $height = $image->getParameter('height');
     $quality = $image->getParameter('quality');
     $dimensionName = self::createDimensionName($width, $height, $quality);
     if ($source->getExtension() !== '') {
         $ext = '.' . $source->getExtension();
     } else {
         $extensions = [IMAGETYPE_GIF => '.gif', IMAGETYPE_JPEG => '.jpg', IMAGETYPE_PNG => '.png', IMAGETYPE_BMP => '.bmp'];
         if (!array_key_exists($source->getType(), $extensions)) {
             $msg = sprintf('Image "%s" is unsupported type.', $source->getFilename());
             throw new InvalidStateException($msg);
         }
         $ext = $extensions[$source->getType()];
     }
     $fileName = $name . $dimensionName . $ext;
     return $fileName;
 }
Exemple #2
0
 /**
  * Returns options part of command
  *
  * @param mixed $width
  * @param mixed $height
  * @param null|int $quality
  * @return string
  */
 private function getCommandOptions($width, $height, $quality)
 {
     $options = [];
     if (isset($width) && (int) $width === 0) {
         $width = $this->image->getWidth() . '!';
     }
     if (isset($height) && (int) $height === 0) {
         $height = $this->image->getHeight() . '!';
     }
     if (!isset($height)) {
         $options['resize'] = '"' . $width . '"';
     } elseif (!isset($width)) {
         $options['resize'] = '"x' . $height . '"';
     } else {
         $options['resize'] = '"' . Strings::trim($width, '!') . 'x' . Strings::trim($height, '!') . '^"';
     }
     if (isset($width) && isset($height)) {
         $options['gravity'] = 'center';
         $options['crop'] = '"' . $width . 'x' . $height . '+0+0"';
     }
     if (isset($quality) && $quality !== 0) {
         $options['quality'] = $quality;
     }
     $command = [];
     foreach ($options as $opt => $value) {
         $command[] = '-' . $opt . ' ' . $value;
     }
     return implode(' ', $command);
 }
Exemple #3
0
 /**
  * Resize image and save to target file
  *
  * @param null|int|string $width NULL: original width; integer: width in pixel; string: others specific (50%)
  * @param null|int|string $height NULL: calculating by ratio; integer: height in pixel; string: others specific (50%)
  * @param null|int $quality
  * @return \Imager\ImageInfo
  * @throws \Imager\InvalidArgumentException
  * @throws \Imager\InvalidStateException
  */
 public function resize($width = null, $height = null, $quality = null)
 {
     // if $width and $height dot defined, than set $width to zero (generate origin size)
     $helpWidth = !isset($width) && !isset($height) ? 0 : $width;
     $this->checkDimension($helpWidth, 'width');
     $this->checkDimension($height, 'height');
     $this->checkQuality($quality);
     $generateWith = $helpWidth === 0 ? $this->image->getWidth() . '!' : $width;
     $generateHeight = $height === 0 ? $this->image->getHeight() . '!' : $height;
     $generateQuality = $quality ?: 0;
     $source = $this->image->getPathname();
     $options = $this->getCommandOptions($generateWith, $generateHeight, $generateQuality);
     $target = $this->createTempFile();
     $command = sprintf('convert %s %s %s', $source, $options, $target);
     $this->run($command);
     $img = new ImageInfo($target, $this->image);
     $img->setParameter('id', $this->image->getFilename())->setParameter('width', $width)->setParameter('height', $height)->setParameter('quality', $quality);
     return $img;
 }
Exemple #4
0
 /**
  * Moves image to target
  *
  * @param \Imager\ImageInfo $image
  * @param string $target
  * @return \Imager\ImageInfo
  */
 private function moveImage(ImageInfo $image, $target)
 {
     FileSystem::rename($image->getPathname(), $target);
     chmod($target, 0666);
     return new ImageInfo($target);
 }