Exemplo n.º 1
2
 /**
  * Internal
  *
  * Applies options before save or output
  *
  * @param \Gmagick $image
  * @param array    $options
  * @param string   $path
  *
  * @throws InvalidArgumentException
  */
 private function applyImageOptions(\Gmagick $image, array $options, $path)
 {
     if (isset($options['format'])) {
         $format = $options['format'];
     } elseif ('' !== ($extension = pathinfo($path, \PATHINFO_EXTENSION))) {
         $format = $extension;
     } else {
         $format = pathinfo($image->getImageFilename(), \PATHINFO_EXTENSION);
     }
     $format = strtolower($format);
     $options = $this->updateSaveOptions($options);
     if (isset($options['jpeg_quality']) && in_array($format, array('jpeg', 'jpg', 'pjpeg'))) {
         $image->setCompressionQuality($options['jpeg_quality']);
     }
     if ((isset($options['png_compression_level']) || isset($options['png_compression_filter'])) && $format === 'png') {
         // first digit: compression level (default: 7)
         if (isset($options['png_compression_level'])) {
             if ($options['png_compression_level'] < 0 || $options['png_compression_level'] > 9) {
                 throw new InvalidArgumentException('png_compression_level option should be an integer from 0 to 9');
             }
             $compression = $options['png_compression_level'] * 10;
         } else {
             $compression = 70;
         }
         // second digit: compression filter (default: 5)
         if (isset($options['png_compression_filter'])) {
             if ($options['png_compression_filter'] < 0 || $options['png_compression_filter'] > 9) {
                 throw new InvalidArgumentException('png_compression_filter option should be an integer from 0 to 9');
             }
             $compression += $options['png_compression_filter'];
         } else {
             $compression += 5;
         }
         $image->setCompressionQuality($compression);
     }
     if (isset($options['resolution-units']) && isset($options['resolution-x']) && isset($options['resolution-y'])) {
         if ($options['resolution-units'] == ImageInterface::RESOLUTION_PIXELSPERCENTIMETER) {
             $image->setimageunits(\Gmagick::RESOLUTION_PIXELSPERCENTIMETER);
         } elseif ($options['resolution-units'] == ImageInterface::RESOLUTION_PIXELSPERINCH) {
             $image->setimageunits(\Gmagick::RESOLUTION_PIXELSPERINCH);
         } else {
             throw new InvalidArgumentException('Unsupported image unit format');
         }
         $image->setimageresolution($options['resolution-x'], $options['resolution-y']);
     }
 }
Exemplo n.º 2
0
 /**
  * Internal
  *
  * Applies options before save or output
  *
  * @param \Gmagick $image
  * @param array    $options
  */
 private function applyImageOptions(\Gmagick $image, array $options)
 {
     if (isset($options['quality'])) {
         $image->setCompressionQuality($options['quality']);
     }
     if (isset($options['resolution-units']) && isset($options['resolution-x']) && isset($options['resolution-y'])) {
         if ($options['resolution-units'] == ImageInterface::RESOLUTION_PIXELSPERCENTIMETER) {
             $image->setimageunits(\Gmagick::RESOLUTION_PIXELSPERCENTIMETER);
         } elseif ($options['resolution-units'] == ImageInterface::RESOLUTION_PIXELSPERINCH) {
             $image->setimageunits(\Gmagick::RESOLUTION_PIXELSPERINCH);
         } else {
             throw new RuntimeException('Unsupported image unit format');
         }
         $image->setimageresolution($options['resolution-x'], $options['resolution-y']);
     }
 }