/** * @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)); }
/** * @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()); }
/** * @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 save(Photo $photo) { $sentence = $this->pdo->prepare("INSERT INTO Photo(\"uuid\", \"resourceId\", \"name\", \"httpUrl\", \"position\",\"filePath\") " . "VALUES(:uuid, :resourceId, :name, :httpUrl, :position, :filePath)"); $sentence->bindValue(':uuid', $photo->id()); $sentence->bindValue(':resourceId', $photo->resourceId()); $sentence->bindValue(':name', $photo->name()); $sentence->bindValue(':httpUrl', $photo->getPhotoHttpUrl()); $sentence->bindValue(':position', $photo->position()); $filePath = is_null($photo->photoFile()) ? null : $photo->photoFile()->filePath(); $filePathType = is_null($photo->photoFile()) ? \PDO::PARAM_NULL : \PDO::PARAM_STR; $sentence->bindValue(':filePath', $filePath, $filePathType); $sentence->execute(); foreach ($photo->altCollection() as $photoAlt) { $this->saveAlternativeText(new PhotoId($photo->id()), $photoAlt); } }
/** * @param Photo $photo * @return void */ public function save(Photo $photo) { $sentence = $this->pdo->prepare("INSERT INTO `photo`(`id`, `resourceId`, `name`, `httpUrl`, `position`, `filePath`) " . "VALUES(:id, :resourceId, :name, :httpUrl, :position, :filePath)"); $sentence->bindValue(':id', $photo->id()); $sentence->bindValue(':resourceId', $photo->resourceId()); $sentence->bindValue(':name', $photo->name()); $sentence->bindValue(':httpUrl', $photo->getPhotoHttpUrl()); $position = $photo->position(); if ($photo->position() === 9999) { $result = $this->pdo->query("SELECT COUNT(*) as amount FROM `photo` WHERE `resourceId`='" . $photo->resourceId() . "'")->fetch(\PDO::FETCH_ASSOC); $position = $result['amount'] + 1; } $sentence->bindValue(':position', $position); $filePath = is_null($photo->photoFile()) ? null : $photo->photoFile()->filePath(); $filePathType = is_null($photo->photoFile()) ? \PDO::PARAM_NULL : \PDO::PARAM_STR; $sentence->bindValue(':filePath', $filePath, $filePathType); $sentence->execute(); foreach ($photo->altCollection() as $photoAlt) { $this->saveAlternativeText(new PhotoId($photo->id()), $photoAlt); } }
/** * @param Photo $photo * @return boolean */ public function remove(Photo $photo) { $itemsUri = explode('/', $photo->getPhotoHttpUrl()); $response = $this->s3->deleteObject(['Bucket' => $this->config->bucket(), 'Key' => trim(array_pop($itemsUri))]); return !is_null($response->get('RequestCharged')); }