/** * Transfer local images to cloud storage. */ public function transferLocalImages() { $this->imageResource->orWhere(function (Builder $query) { $query->where('filename', '!=', ''); $query->whereNotNull('filename'); })->get()->each(function (Image $image) { $this->dispatcher->fire(new NewImageEvent($image)); }); }
/** * @param Image $image * * @return ImagickCollection */ public function preProcess(Image $image) : ImagickCollection { $this->imagick->readImage($image->storageLocation()); $this->imagick->setFilename($image->filename()); $collection = new ImagickCollection([$this->imagick]); foreach ($this->transformers as $transformer) { $transformer->applyTo($collection); } return $collection; }
/** * Should pre-process and transfer an image. */ public function testHandle() { $filename = '/tmp/' . uniqid(); file_put_contents($filename, ''); $this->image->expects($this->atLeastOnce())->method('storageLocation')->willReturn($filename); $preProcessed = $this->makeMock(ImagickContract::class); $this->imagePreProcessor->expects($this->atLeastOnce())->method('preProcess')->with($this->image)->willReturn(new ImagickCollection([$preProcessed])); $this->config->expects($this->atLeastOnce())->method('get')->willReturn('foobar_endpoint'); $fileContent = 'foo bar'; $preProcessed->expects($this->atLeastOnce())->method('getImageBlob')->willReturn($fileContent); $this->filesystem->expects($this->atLeastOnce())->method('put')->with($this->isType('string'), $fileContent, $this->isType('array')); $this->newImageListener->handle($this->event); }
/** * @param string $filename * * @return MockInterface */ private function expectImageCreation(string $filename = null) : MockInterface { if ($filename === null) { $filename = $this->generator()->anyInteger(); } $newImage = $this->mockery(Image::class); $newImage->shouldReceive('getAttribute')->with('filename')->andReturn($filename); $newImage->shouldReceive('filename')->andReturn($filename); $newImage->shouldReceive('getAttribute')->with('id')->andReturn($this->generator()->anyInteger()); $this->imageResource->shouldReceive('create')->andReturn($newImage); return $newImage; }
/** * @return Image */ protected function createImage() : Image { return Image::create(['alt_text' => str_random(), 'url' => $this->faker()->slug]); }
/** * Should be able to get the CRUD id for the image. */ public function testCrudID() { $image = new Image(); $image->id = 123; $this->assertEquals($image->id, $image->crudId()); }
/** * @param Product $product * * @return Image */ protected function attachImageToProduct(Product $product) : Image { $image = Image::create(['alt_text' => str_random(), 'url' => $this->generator()->anySlug()]); $product->images()->attach($image->id); return $image; }
/** * @param Image $imageResource * * @return SplFileObject */ private function storageImageFile(Image $imageResource) : SplFileObject { try { return new SplFileObject($imageResource->storageLocation()); } catch (\RuntimeException $e) { return new SplTempFileObject(); } }
/** * @return Generator|Image[] */ private function makeImages() : Generator { for ($i = 0, $count = random_int(1, 2); $i < $count; $i++) { (yield Image::create(['alt_text' => $this->faker()->words(3, true), 'url' => secure_asset("/img/lorem/{$this->faker()->numberBetween(1, 5)}.jpg#" . uniqid('', true))])); } }