Imbo\EventListener\ImageVariations::chooseVariation PHP Метод

chooseVariation() публичный Метод

Choose an image variation based on the transformations and the original size of the image
public chooseVariation ( Imbo\EventManager\EventInterface $event )
$event Imbo\EventManager\EventInterface The current event
    public function chooseVariation(EventInterface $event)
    {
        $request = $event->getRequest();
        $response = $event->getResponse();
        $user = $request->getUser();
        $imageIdentifier = $request->getImageIdentifier();
        // Fetch the original width / height of the image to use for ratio calculations
        $image = $response->getModel();
        $imageWidth = $image->getWidth();
        $imageHeight = $image->getHeight();
        // Fetch the transformations from the request and find the max width used in the set
        $transformations = $request->getTransformations();
        if (!$transformations) {
            // No transformations in the request
            return;
        }
        $maxWidth = $this->getMaxWidth($imageWidth, $imageHeight, $transformations);
        if (!$maxWidth) {
            // No need to use a variation based on the set of transformations
            return;
        }
        // Fetch the index of the transformation that decided the max width, and the width itself
        list($transformationIndex, $maxWidth) = each($maxWidth);
        if ($maxWidth >= $imageWidth) {
            // The width is the same or above the original, use the original
            return;
        }
        // WE HAVE A WINNER! Find the best variation. The width of the variation is the first
        // available one above the $maxWidth value
        $variation = $this->database->getBestMatch($user, $imageIdentifier, $maxWidth);
        if (!$variation) {
            // Could not find any :(
            return;
        }
        // Now that we have a variation we can use we need to adjust some of the transformation
        // parameters.
        $event->getManager()->trigger('image.transformations.adjust', ['transformationIndex' => $transformationIndex, 'ratio' => $imageWidth / $variation['width']]);
        // Fetch the image variation blob from the storage adapter
        $imageBlob = $this->storage->getImageVariation($user, $imageIdentifier, $variation['width']);
        if (!$imageBlob) {
            // The image blob does not exist in the storage, which it should. Trigger an error and
            // return
            trigger_error('Image variation storage is not in sync with the image variation database', E_USER_WARNING);
            return;
        }
        // Set some data that the storage operations listener usually sets, since that will be
        // skipped since we have an image variation
        $lastModified = $event->getStorage()->getLastModified($user, $imageIdentifier);
        $response->setLastModified($lastModified);
        // Update the model
        $model = $response->getModel();
        $model->setBlob($imageBlob)->setWidth($variation['width'])->setHeight($variation['height']);
        // Set a HTTP header that informs the user agent on which image variation that was used in
        // the transformations
        $response->headers->set('X-Imbo-ImageVariation', $variation['width'] . 'x' . $variation['height']);
        // Stop the propagation of this event
        $event->stopPropagation();
        $event->getManager()->trigger('image.loaded');
    }

Usage Example

Пример #1
0
 /**
  * @covers Imbo\EventListener\ImageVariations::chooseVariation
  * @covers Imbo\EventListener\ImageVariations::getMaxWidth
  */
 public function testUpdatesResponseAndImageModelOnSuccess()
 {
     $width = 1024;
     $height = 768;
     $transformationWidth = 512;
     $variationWidth = 800;
     $variationHeight = 600;
     $variationBlob = 'blob';
     $lastModified = new DateTime();
     $transformations = [['name' => 'desaturate', 'params' => []], ['name' => 'maxSize', 'params' => ['width' => $transformationWidth]]];
     $this->imageModel->expects($this->once())->method('getWidth')->will($this->returnValue($width));
     $this->imageModel->expects($this->once())->method('getHeight')->will($this->returnValue($height));
     $this->request->expects($this->once())->method('getTransformations')->will($this->returnValue($transformations));
     $this->db->expects($this->once())->method('getBestMatch')->with($this->publicKey, $this->imageIdentifier, $transformationWidth)->will($this->returnValue(['width' => $variationWidth, 'height' => $variationHeight]));
     $this->eventManager->expects($this->at(0))->method('trigger')->with('image.transformations.adjust', ['transformationIndex' => 1, 'ratio' => $width / $variationWidth]);
     $this->storage->expects($this->once())->method('getImageVariation')->with($this->publicKey, $this->imageIdentifier, $variationWidth)->will($this->returnValue($variationBlob));
     $this->imageStorage->expects($this->once())->method('getLastModified')->with($this->publicKey, $this->imageIdentifier)->will($this->returnValue($lastModified));
     $this->response->expects($this->once())->method('setLastModified')->with($lastModified);
     $this->imageModel->expects($this->once())->method('setBlob')->with($variationBlob)->will($this->returnValue($this->imageModel));
     $this->imageModel->expects($this->once())->method('setWidth')->with($variationWidth)->will($this->returnValue($this->imageModel));
     $this->imageModel->expects($this->once())->method('setHeight')->with($variationHeight)->will($this->returnValue($this->imageModel));
     $this->responseHeaders->expects($this->once())->method('set')->with('X-Imbo-ImageVariation', $variationWidth . 'x' . $variationHeight);
     $this->event->expects($this->once())->method('stopPropagation');
     $this->eventManager->expects($this->at(1))->method('trigger')->with('image.loaded');
     $this->listener->chooseVariation($this->event);
 }