/** * Upload Image to Imgur * * @param string $image A binary file (path to file), base64 data, or a URL for an image. (up to 10MB) * @param string $type Image type. Use Mechpave\ImgurClient\Model\ImageModel * @param string $title The title of the image * @param string $description The description of the image * @param string $album The id of the album you want to add the image to. For anonymous albums, {album} should be the deletehash that is returned at creation. * @param string $name The name of the file * @throws \UnexpectedValueException * @return ImageInterface */ public function upload($image, $type, $title = null, $description = null, $album = null, $name = null) { if ($type == ImageModel::TYPE_FILE) { //check if file exists and get its contents if (!file_exists($image)) { throw new \UnexpectedValueException('Provided file does not exist'); } $contents = file_get_contents($image); if (!$contents) { throw new \UnexpectedValueException('Could not get file contents'); } $image = $contents; } $postData = ['image' => $image, 'type' => $type]; if ($title) { $postData['title'] = $title; } if ($name) { $postData['name'] = $name; } if ($description) { $postData['description'] = $description; } if ($album) { $postData['album'] = $album; } $response = $this->httpClient->post('image', $postData); $image = $this->imageMapper->buildImage($response->getBody()['data']); return $image; }
public function testMapToCustomClass() { $imageClass = 'Mechpave\\ImgurClient\\Tests\\Entity\\CustomImage'; $objectMapper = $this->getMockBuilder('Mechpave\\ImgurClient\\Mapper\\ObjectMapper')->disableOriginalConstructor()->getMock(); $objectMapper->expects($this->once())->method('getImageClass')->willReturn($imageClass); $uploadedAt = new \DateTime(); $uploadedAt->setTimestamp((int) $this->imageData['datetime']); /** @noinspection PhpParamsInspection */ $imageMapper = new ImageMapper($objectMapper); $image = $imageMapper->buildImage($this->imageData); $this->assertTrue($image instanceof $imageClass); $this->assertEquals($image->getImageId(), $this->imageData['id']); $this->assertEquals($image->getTitle(), $this->imageData['title']); $this->assertEquals($image->getDescription(), $this->imageData['description']); $this->assertEquals($image->getType(), $this->imageData['type']); $this->assertEquals($image->isAnimated(), $this->imageData['animated']); $this->assertEquals($image->getWidth(), $this->imageData['width']); $this->assertEquals($image->getHeight(), $this->imageData['height']); $this->assertEquals($image->getSize(), $this->imageData['size']); $this->assertEquals($image->getViews(), $this->imageData['views']); $this->assertEquals($image->getBandwith(), $this->imageData['bandwith']); $this->assertEquals($image->getDeleteHash(), $this->imageData['deletehash']); $this->assertEquals($image->getName(), $this->imageData['name']); $this->assertEquals($image->getSection(), $this->imageData['section']); $this->assertEquals($image->getLink(), $this->imageData['link']); $this->assertEquals($image->getGifv(), $this->imageData['gifv']); $this->assertEquals($image->getMp4(), $this->imageData['mp4']); $this->assertEquals($image->getWebm(), $this->imageData['webm']); $this->assertEquals($image->isLooping(), $this->imageData['looping']); $this->assertEquals($image->isFavorite(), $this->imageData['favorite']); $this->assertEquals($image->isNsfw(), $this->imageData['nsfw']); $this->assertEquals($image->getVote(), $this->imageData['vote']); $this->assertEquals($image->getUploadedAt(), $uploadedAt); }
/** * Build Album object from raw data * @param array $albumData * @return AlbumInterface */ public function buildAlbum(array $albumData) { if (empty($albumData['id'])) { throw new \InvalidArgumentException(); } $insertedIntoGallery = null; if (!empty($albumData['datetime'])) { $insertedIntoGallery = new \DateTime(); $insertedIntoGallery->setTimestamp((int) $albumData['datetime']); } $albumClass = $this->objectMapper->getAlbumClass(); /** @var AlbumInterface $album */ $album = new $albumClass(); $album->setAlbumId($albumData['id'])->setTitle(!empty($albumData['title']) ? $albumData['title'] : null)->setDescription(!empty($albumData['description']) ? $albumData['description'] : null)->setInsertedIntoGallery($insertedIntoGallery)->setCover(!empty($albumData['cover']) ? $albumData['cover'] : null)->setCoverWidth(!empty($albumData['cover_width']) ? $albumData['cover_width'] : null)->setCoverHeight(!empty($albumData['cover_height']) ? $albumData['cover_height'] : null)->setAccountUsername(!empty($albumData['account_url']) ? $albumData['account_url'] : null)->setAccountId(!empty($albumData['account_id']) ? $albumData['account_id'] : null)->setPrivacy(!empty($albumData['privacy']) ? $albumData['privacy'] : null)->setLayout(!empty($albumData['layout']) ? $albumData['layout'] : null)->setViews(!empty($albumData['views']) ? $albumData['views'] : null)->setLink(!empty($albumData['link']) ? $albumData['link'] : null)->setFavorite(!empty($albumData['favorite']) ? $albumData['favorite'] : null)->setNsfw(!empty($albumData['nsfw']) ? $albumData['nsfw'] : null)->setSection(!empty($albumData['section']) ? $albumData['section'] : null)->setOrder(!empty($albumData['order']) ? $albumData['order'] : null)->setDeleteHash(!empty($albumData['deletehash']) ? $albumData['deletehash'] : null)->setImagesCount(!empty($albumData['images_count']) ? $albumData['images_count'] : null); $images = []; if (!empty($albumData['images'])) { foreach ($albumData['images'] as $imageData) { $image = $this->imageMapper->buildImage($imageData); $images[] = $image; } } $album->setImages($images); return $album; }