storeImageVariationMetadata() public method

Store an image variation
public storeImageVariationMetadata ( string $user, string $imageIdentifier, integer $width, integer $height ) : boolean
$user string The user which the image belongs to
$imageIdentifier string The image identifier of the original
$width integer The width of the variation
$height integer The height of the variation
return boolean
Beispiel #1
0
 public function testCanDeleteAllTransformations()
 {
     $variations = [['width' => 770, 'height' => 564], ['width' => 385, 'height' => 282], ['width' => 192, 'height' => 140]];
     foreach ($variations as $variation) {
         $this->assertTrue($this->adapter->storeImageVariationMetadata('key', 'id', $variation['width'], $variation['height']));
     }
     $this->assertTrue($this->adapter->deleteImageVariations('key', 'id'));
     $this->assertSame(null, $this->adapter->getBestMatch('key', 'id', 100));
 }
Beispiel #2
0
 /**
  * Generate multiple variations based on the configuration
  *
  * If any of the operations fail Imbo will trigger errors
  *
  * @param EventInterface $event
  */
 public function generateVariations(EventInterface $event)
 {
     // Fetch the event manager to trigger events
     $eventManager = $event->getManager();
     $request = $event->getRequest();
     $publicKey = $request->getPublicKey();
     $originalImage = $request->getImage();
     $imageIdentifier = $originalImage->getChecksum();
     $originalWidth = $originalImage->getWidth();
     // Fetch parameters specified in the Imbo configuration related to what sort of variations
     // should be generated
     $minWidth = $this->params['minWidth'];
     $maxWidth = $this->params['maxWidth'];
     $minDiff = $this->params['minDiff'];
     $scaleFactor = $this->params['scaleFactor'];
     // Remove widths which are larger than the original image
     $widths = array_filter($this->params['widths'], function ($value) use($originalWidth) {
         return $value < $originalWidth;
     });
     if ($this->params['autoScale'] === true) {
         // Have Imbo figure out the widths to generate in addition to the ones in the "width"
         // configuration parameter
         $variationWidth = $previousWidth = $originalWidth;
         while ($variationWidth > $minWidth) {
             $variationWidth = round($variationWidth * $scaleFactor);
             if ($variationWidth > $maxWidth) {
                 // Width too big, try again (twss)
                 continue;
             }
             if ($previousWidth - $variationWidth < $minDiff || $variationWidth < $minWidth) {
                 // The diff is too small, or the variation is too small, stop generating more
                 // widths
                 break;
             }
             $previousWidth = $variationWidth;
             $widths[] = $variationWidth;
         }
     }
     foreach ($widths as $width) {
         // Clone the image so that the resize operation will happen on the original every time
         $image = clone $originalImage;
         try {
             // Trigger a loading of the image, using the clone of the original as an argument
             $eventManager->trigger('image.loaded', ['image' => $image]);
             // If configured, use a lossless variation format
             if ($this->params['lossless'] === true) {
                 $eventManager->trigger('image.transformation.convert', ['image' => $image, 'params' => ['type' => 'png']]);
             }
             // Trigger a resize of the image (the transformation handles aspect ratio)
             $eventManager->trigger('image.transformation.resize', ['image' => $image, 'params' => ['width' => $width]]);
             // Trigger an update of the model
             $eventManager->trigger('image.transformed', ['image' => $image]);
             // Store the image
             $this->storage->storeImageVariation($publicKey, $imageIdentifier, $image->getBlob(), $width);
             // Store some data about the variation
             $this->database->storeImageVariationMetadata($publicKey, $imageIdentifier, $image->getWidth(), $image->getHeight());
         } catch (TransformationException $e) {
             // Could not transform the image
             trigger_error(sprintf('Could not generate image variation for %s (%s), width: %d', $publicKey, $imageIdentifier, $width), E_USER_WARNING);
         } catch (StorageException $e) {
             // Could not store the image
             trigger_error(sprintf('Could not store image variation for %s (%s), width: %d', $publicKey, $imageIdentifier, $width), E_USER_WARNING);
         } catch (DatabaseException $e) {
             // Could not store metadata about the variation
             trigger_error(sprintf('Could not store image variation metadata for %s (%s), width: %d', $publicKey, $imageIdentifier, $width), E_USER_WARNING);
             try {
                 $this->storage->deleteImageVariations($publicKey, $imageIdentifier, $width);
             } catch (StorageException $e) {
                 trigger_error('Could not remove the stored variation', E_USER_WARNING);
             }
         }
     }
 }