Store an image variation
public storeImageVariation ( string $user, string $imageIdentifier, string $blob, integer $width ) : boolean | ||
$user | string | The user which the image belongs to |
$imageIdentifier | string | The image identifier of the original |
$blob | string | The image blob to store |
$width | integer | The width of the variation |
return | boolean |
public function testCanDeleteOneOrMoreImageVariations() { $key = 'key'; $id = 'imageId'; $blob = file_get_contents(FIXTURES_DIR . '/colors.png'); $this->assertTrue($this->adapter->storeImageVariation($key, $id, $blob, 100)); $this->assertTrue($this->adapter->storeImageVariation($key, $id, 'blob2', 200)); $this->assertTrue($this->adapter->storeImageVariation($key, $id, 'blob3', 300)); $this->assertSame($blob, $this->adapter->getImageVariation($key, $id, 100)); $this->assertSame('blob2', $this->adapter->getImageVariation($key, $id, 200)); $this->assertSame('blob3', $this->adapter->getImageVariation($key, $id, 300)); $this->assertTrue($this->adapter->deleteImageVariations($key, $id, 100)); $this->assertNull($this->adapter->getImageVariation($key, $id, 100)); $this->assertSame('blob2', $this->adapter->getImageVariation($key, $id, 200)); $this->assertSame('blob3', $this->adapter->getImageVariation($key, $id, 300)); $this->assertTrue($this->adapter->deleteImageVariations($key, $id)); $this->assertNull($this->adapter->getImageVariation($key, $id, 200)); $this->assertNull($this->adapter->getImageVariation($key, $id, 300)); }
/** * 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); } } } }