/** * @param PhotoThumb $thumb * @param Photo $photo * @return null|PhotoFile */ public function uploadThumb(PhotoThumb $thumb, Photo $photo) { $destinyPath = $this->localStorageConfig->storagePath() . '/' . $this->getMd5Path($photo->resourceId()) . '/' . $photo->id(); $this->createPathIfNotExists($destinyPath); $newPhotoFilePath = sprintf('%s/%s_%dx%d.%s', $destinyPath, $photo->slug(), $thumb->width(), $thumb->height(), $thumb->photoThumbFile()->format()); copy($thumb->photoThumbFile()->filePath(), $newPhotoFilePath); return new PhotoFile($newPhotoFilePath); }
/** * @param ThumbId $thumbId * @param Photo $photo * @param PhotoThumbSize $thumbSize * @param HttpUrl $thumbHttpUrl * @return PhotoThumb */ public function generate(ThumbId $thumbId, Photo $photo, PhotoThumbSize $thumbSize, HttpUrl $thumbHttpUrl) { $photoFile = $photo->photoFile() ? $photo->photoFile() : $this->downloadPhoto($photo->getPhotoHttpUrl()); $thumbFile = $this->thumbGeneratorConfig->tempPath() . '/' . $thumbId->id() . '.' . self::CONVERSION_FORMAT; $target = new Box($thumbSize->width(), $thumbSize->height()); $originalImage = $this->imagine->open($photoFile->filePath()); $img = $originalImage->thumbnail($target, ImageInterface::THUMBNAIL_OUTBOUND); $img->save($thumbFile); return new PhotoThumb($thumbId, new PhotoId($photo->id()), $thumbHttpUrl, $thumbSize, new PhotoFile($thumbFile)); }
/** * @param ThumbRequestCollection $thumbRequestCollection * @param Photo $photo * @return array|PhotoThumbCollection */ public function getThumbsCollectionOfPhoto(ThumbRequestCollection $thumbRequestCollection, Photo $photo) { $thumbCollection = new PhotoThumbCollection(); /** @var ThumbRequest $thumbRequest */ foreach ($thumbRequestCollection as $thumbRequest) { $thumb = $this->thumbRepository->findOneBy(new PhotoId($photo->id()), $thumbRequest->thumbSize()); if (is_null($thumb)) { $thumb = $this->createThumbFromOriginal($photo, $thumbRequest); } $thumbCollection[] = $thumb; } return $thumbCollection; }
/** * @param Photo $photo * @return array */ public function write(Photo $photo) { return ['id' => $photo->id(), 'resourceId' => $photo->resourceId(), 'name' => $photo->name(), 'url' => $photo->getPhotoHttpUrl(), 'alts' => $this->getAlts($photo->altCollection()), 'file' => is_null($photo->photoFile()) ? '' : $photo->photoFile()->filePath()]; }
/** * @param Photo $photo * @return void */ public function delete(Photo $photo) { $sentence = $this->pdo->prepare("DELETE FROM Photo WHERE uuid=:uuid LIMIT 1"); $sentence->bindValue(':uuid', $photo->id()); $sentence->execute(); }
/** * @param ThumbId $thumbId * @param Photo $photo * @param PhotoThumbSize $thumbSize * @param HttpUrl $thumbHttpUrl * @return PhotoThumb */ public function generate(ThumbId $thumbId, Photo $photo, PhotoThumbSize $thumbSize, HttpUrl $thumbHttpUrl) { return new PhotoThumb(new ThumbId(), new PhotoId($photo->id()), $thumbHttpUrl, $thumbSize, $photo->photoFile()); }
/** * @param Photo $photo * @return void */ public function delete(Photo $photo) { $sentence = $this->pdo->prepare("DELETE FROM `photo` WHERE `id`=:id"); $sentence->bindValue(':id', $photo->id()); $sentence->execute(); }
/** * @param PhotoThumb $thumb * @param Photo $photo * @return null|PhotoFile */ public function uploadThumb(PhotoThumb $thumb, Photo $photo) { $this->s3->putObject(['Bucket' => $this->config->bucket(), 'Key' => $this->getThumbUri($photo->id(), $photo->slug(), $thumb->width(), $thumb->height(), $thumb->photoThumbFile()->format()), 'Body' => fopen($thumb->photoThumbFile()->filePath(), 'r'), 'ContentType' => $thumb->photoThumbFile()->mimeType(), 'ACL' => 'public-read']); }
/** * @test */ public function photoWorks() { $photoFile = new PhotoFile(__DIR__ . '/pixel.png'); $photoId = new PhotoId(); $lang = new Lang(Lang::LANGUAGE_EN); $alt = new PhotoAlt($photoId, 'alt', $lang); $photoAltCollection = new PhotoAltCollection([$alt]); $photo = new Photo($photoId, new ResourceId(1), new PhotoName('testing'), new HttpUrl('http://works'), new PhotoAltCollection([new PhotoAlt($photoId, 'alt', new Lang(Lang::LANGUAGE_EN))]), new Position(1), $photoFile); $this->assertEquals($photoId->id(), $photo->id()); $this->assertEquals(1, $photo->resourceId()); $this->assertEquals('testing', $photo->name()); $this->assertEquals('testing', $photo->slug()); $this->assertEquals('http://works', $photo->getPhotoHttpUrl()); $this->assertEquals($photoAltCollection, $photo->altCollection()); $this->assertEquals($alt, $photo->altByLang($lang)); $this->assertNull($photo->altByLang(new Lang(Lang::LANGUAGE_ES))); $this->assertSame($photo->position(), 1); $this->assertSame($photoFile, $photo->photoFile()); }
/** * @test */ public function ensureUniquePhotoIdWorks() { $photoId = new PhotoId($this->photo->id()); $uniquePhotoId = $this->repository->ensureUniquePhotoId($photoId); $this->assertTrue($photoId->id() !== $uniquePhotoId->id()); }