setMimeType() public method

Set the mime type
public setMimeType ( string $mimeType ) : Image
$mimeType string The mime type, for instance "image/png"
return Image
Beispiel #1
0
 /**
  * Prepare an image
  *
  * This method should prepare an image object from php://input. The method must also figure out
  * the width, height, mime type and extension of the image.
  *
  * @param EventInterface $event The current event
  * @throws ImageException
  */
 public function prepareImage(EventInterface $event)
 {
     $request = $event->getRequest();
     // Fetch image data from input
     $imageBlob = $request->getContent();
     if (empty($imageBlob)) {
         $e = new ImageException('No image attached', 400);
         $e->setImboErrorCode(Exception::IMAGE_NO_IMAGE_ATTACHED);
         throw $e;
     }
     // Open the image with imagick to fetch the mime type
     $imagick = new Imagick();
     try {
         $imagick->readImageBlob($imageBlob);
         $mime = $imagick->getImageMimeType();
         $size = $imagick->getImageGeometry();
     } catch (ImagickException $e) {
         $e = new ImageException('Invalid image', 415);
         $e->setImboErrorCode(Exception::IMAGE_INVALID_IMAGE);
         throw $e;
     }
     if (!Image::supportedMimeType($mime)) {
         $e = new ImageException('Unsupported image type: ' . $mime, 415);
         $e->setImboErrorCode(Exception::IMAGE_UNSUPPORTED_MIMETYPE);
         throw $e;
     }
     // Store relevant information in the image instance and attach it to the request
     $image = new Image();
     $image->setMimeType($mime)->setExtension(Image::getFileExtension($mime))->setBlob($imageBlob)->setWidth($size['width'])->setHeight($size['height'])->setOriginalChecksum(md5($imageBlob));
     $request->setImage($image);
 }
Beispiel #2
0
 /**
  * @dataProvider getMimeTypes
  */
 public function testSetsTheCorrectMimeTypeWhenAMappedOneIsUsed($set, $get)
 {
     $this->image->setMimeType($set);
     $this->assertSame($get, $this->image->getMimeType());
 }
Beispiel #3
0
 /**
  * Insert some images to test the query functionality
  *
  * All images added is owned by "publickey", unless $alternatePublicKey is set to true
  *
  * @param  boolean $alternatePublicKey Whether to alternate between 'publickey' and
  *                                     'publickey2' when inserting images
  * @return array Returns an array with two elements where the first is the timestamp of when
  *               the first image was added, and the second is the timestamp of when the last
  *               image was added
  */
 private function insertImages($alternatePublicKey = false)
 {
     $now = time();
     $start = $now;
     $images = array();
     foreach (array('image.jpg', 'image.png', 'image1.png', 'image2.png', 'image3.png', 'image4.png') as $i => $fileName) {
         $path = FIXTURES_DIR . '/' . $fileName;
         $info = getimagesize($path);
         $publicKey = 'publickey';
         if ($alternatePublicKey && $i % 2 === 0) {
             $publickey2 = 'publickey2';
         }
         $image = new Image();
         $image->setMimeType($info['mime'])->setExtension(substr($fileName, strrpos($fileName, '.') + 1))->setWidth($info[0])->setHeight($info[1])->setBlob(file_get_contents($path))->setAddedDate(new DateTime('@' . $now++, new DateTimeZone('UTC')))->setOriginalChecksum(md5_file($path));
         $imageIdentifier = md5($image->getBlob());
         // Add the image
         $this->adapter->insertImage($publicKey, $imageIdentifier, $image);
         // Insert some metadata
         $this->adapter->updateMetadata($publicKey, $imageIdentifier, array('key' . $i => 'value' . $i));
     }
     // Remove the last increment to get the timestamp for when the last image was added
     $end = $now - 1;
     return array($start, $end);
 }
Beispiel #4
0
 /**
  * @dataProvider getOriginalMimeTypes
  * @covers Imbo\Http\Response\ResponseFormatter::negotiate
  */
 public function testUsesTheOriginalMimeTypeOfTheImageIfConfigDisablesContentNegotiationForImages($originalMimeType, $expectedFormatter)
 {
     // Use a real object since the code we are testing uses get_class(), which won't work as
     // expected when the object used is a mock
     $image = new Image();
     $image->setMimeType($originalMimeType);
     $requestHeaders = $this->getMock('Symfony\\Component\\HttpFoundation\\HeaderBag');
     $this->request->headers = $requestHeaders;
     $this->contentNegotiation->expects($this->any())->method('isAcceptable')->will($this->returnValue(1));
     $this->response->expects($this->never())->method('setVary');
     $this->response->expects($this->once())->method('getModel')->will($this->returnValue($image));
     $event = $this->getMock('Imbo\\EventManager\\Event');
     $event->expects($this->any())->method('getRequest')->will($this->returnValue($this->request));
     $event->expects($this->any())->method('getResponse')->will($this->returnValue($this->response));
     $event->expects($this->any())->method('getConfig')->will($this->returnValue(['contentNegotiateImages' => false]));
     $this->responseFormatter->negotiate($event);
     $this->assertSame($expectedFormatter, $this->responseFormatter->getFormatter());
 }