diff --git a/decorator/assets/img/depau.png b/decorator/assets/img/depau.png new file mode 100644 index 0000000..04ee798 Binary files /dev/null and b/decorator/assets/img/depau.png differ diff --git a/decorator/public/image.php b/decorator/public/image.php new file mode 100644 index 0000000..9b064ef --- /dev/null +++ b/decorator/public/image.php @@ -0,0 +1,9 @@ +draw()); \ No newline at end of file diff --git a/decorator/src/FrameDecoratorJpeg.php b/decorator/src/FrameDecoratorJpeg.php new file mode 100644 index 0000000..435f1e4 --- /dev/null +++ b/decorator/src/FrameDecoratorJpeg.php @@ -0,0 +1,29 @@ +thickness = $thickness; + } + + public function draw($image) + { + return $this->addBorder($image); + } + + public function addBorder($img) + { + for ($i = 0; $i < $this->thickness; $i++) { + imagerectangle($img, $i, $i, imagesx($img) - $i - 1, imagesy($img) - $i - 1, imagecolorallocate($img, 255, 255, 255)); + } + + return $img; + } +} \ No newline at end of file diff --git a/decorator/src/GrayscaleDecorator.php b/decorator/src/GrayscaleDecorator.php new file mode 100644 index 0000000..9b0fb5a --- /dev/null +++ b/decorator/src/GrayscaleDecorator.php @@ -0,0 +1,15 @@ +decorators[] = $decorator; + } + + public function draw() + { + foreach ($this->decorators as $decorator){ + $this->image = $decorator->draw($this->image); + } + } +} \ No newline at end of file diff --git a/decorator/src/ImageDecorator.php b/decorator/src/ImageDecorator.php new file mode 100644 index 0000000..f3ceeb6 --- /dev/null +++ b/decorator/src/ImageDecorator.php @@ -0,0 +1,10 @@ +path = $path; + $this->image = imagecreatefromjpeg($this->path); + } + + public function draw() + { + parent::draw(); + return $this->image; + } +} \ No newline at end of file diff --git a/decorator/src/ResizeDecorator.php b/decorator/src/ResizeDecorator.php new file mode 100644 index 0000000..4d3d9e0 --- /dev/null +++ b/decorator/src/ResizeDecorator.php @@ -0,0 +1,22 @@ +width = $width; + $this->height = $height; + } + + public function draw($image) + { + return imagescale($image, $this->width, $this->height); + } +} \ No newline at end of file diff --git a/decorator/src/WatermarkDecorator.php b/decorator/src/WatermarkDecorator.php new file mode 100644 index 0000000..0612abb --- /dev/null +++ b/decorator/src/WatermarkDecorator.php @@ -0,0 +1,38 @@ +bottonMargin = $bottonMargin; + $this->rightMargin = $rightMargin; + } + + public function draw($image) + { + $estampa = imagecreatefrompng(assets_path('img/depau.png')); + + $sx = imagesx($estampa); + $sy = imagesy($estampa); + + imagecopy( + $image, + $estampa, + imagesx($image) - $sx - $this->rightMargin, + imagesy($image) - $sy - $this->bottonMargin, + 0, + 0, + imagesx($estampa), + imagesy($estampa) + ); + + return $image; + } +} \ No newline at end of file diff --git a/decorator/tests/ImageTest.php b/decorator/tests/ImageTest.php new file mode 100644 index 0000000..b509d78 --- /dev/null +++ b/decorator/tests/ImageTest.php @@ -0,0 +1,117 @@ +assertImageEquals($image, 'basic-image.jpeg'); + } + /** + * @test + */ + function it_can_draw_a_resized_image() + { + $image = new ImageJpeg(assets_path('img/decorator.jpeg')); + $image->setDecorator(new ResizeDecorator(1000, 666)); + + $this->assertImageEquals($image, 'resized-image.jpeg'); + } + /** + * @test + */ + function it_can_draw_a_greyscale_image() + { + $image = new ImageJpeg(assets_path('img/decorator.jpeg')); + + $image->setDecorator(new GrayscaleDecorator()); + + $this->assertImageEquals($image, 'greyscale-image.jpeg'); + } + /** + * @test + */ + function it_can_draw_a_framed_image() + { + $image = new ImageJpeg(assets_path('img/decorator.jpeg')); + + $image->setDecorator(new FrameDecoratorJpeg(10)); + + $this->assertImageEquals($image, 'framed-image.jpeg'); + } + /** + * @test + */ + function it_can_draw_a_framed__resized_image() + { + $image = new ImageJpeg(assets_path('img/decorator.jpeg')); + + $image->setDecorator(new ResizeDecorator(1000, 666)); + + $image->setDecorator(new FrameDecoratorJpeg(10)); + + $this->assertImageEquals($image, 'framed-sized-image.jpeg'); + } + /** + * @test + */ + function it_can_draw_a_resize_greyscale_image() + { + $image = new ImageJpeg(assets_path('img/decorator.jpeg')); + + $image->setDecorator(new ResizeDecorator(1000, 600)); + + $image->setDecorator(new GrayscaleDecorator()); + + $this->assertImageEquals($image, 'grey-size-image.jpeg'); + } + /** + * @test + */ + function it_can_draw_a_watermarked_image() + { + $image = new ImageJpeg(assets_path('img/decorator.jpeg')); + + $image->setDecorator(new WatermarkDecorator( 10, 10)); + + $this->assertImageEquals($image, 'watermarked-image.jpeg'); + } + + /** + * @param ImageJpeg $image + * @param string $filename + */ + public function assertImageEquals($image, string $filename): void + { + imagejpeg($image->draw(), storage_path($filename)); + + if (!file_exists($this->snapshotPath($filename))) { + imagejpeg($image->draw(), $this->snapshotPath($filename)); + $this->markTestIncomplete('No exisitia la captura y se creó'); + } + + $this->assertTrue(file_get_contents($this->snapshotPath($filename)) === file_get_contents(storage_path($filename)), "Las imagenes son diferentes"); + } + + /** + * @param string $filename + * @return string + */ + public function snapshotPath(string $filename): string + { + return __DIR__ . '/snapshots/' . $filename; + } +} diff --git a/decorator/tests/snapshots/basic-image.jpeg b/decorator/tests/snapshots/basic-image.jpeg new file mode 100644 index 0000000..6941bde Binary files /dev/null and b/decorator/tests/snapshots/basic-image.jpeg differ diff --git a/decorator/tests/snapshots/framed-image.jpeg b/decorator/tests/snapshots/framed-image.jpeg new file mode 100644 index 0000000..2eaebcc Binary files /dev/null and b/decorator/tests/snapshots/framed-image.jpeg differ diff --git a/decorator/tests/snapshots/framed-sized-image.jpeg b/decorator/tests/snapshots/framed-sized-image.jpeg new file mode 100644 index 0000000..73088d0 Binary files /dev/null and b/decorator/tests/snapshots/framed-sized-image.jpeg differ diff --git a/decorator/tests/snapshots/grey-size-image.jpeg b/decorator/tests/snapshots/grey-size-image.jpeg new file mode 100644 index 0000000..9399ea7 Binary files /dev/null and b/decorator/tests/snapshots/grey-size-image.jpeg differ diff --git a/decorator/tests/snapshots/greyscale-image.jpeg b/decorator/tests/snapshots/greyscale-image.jpeg new file mode 100644 index 0000000..46574eb Binary files /dev/null and b/decorator/tests/snapshots/greyscale-image.jpeg differ diff --git a/decorator/tests/snapshots/resized-image.jpeg b/decorator/tests/snapshots/resized-image.jpeg new file mode 100644 index 0000000..642cc74 Binary files /dev/null and b/decorator/tests/snapshots/resized-image.jpeg differ diff --git a/decorator/tests/snapshots/watermarked-image.jpeg b/decorator/tests/snapshots/watermarked-image.jpeg new file mode 100644 index 0000000..8605d4a Binary files /dev/null and b/decorator/tests/snapshots/watermarked-image.jpeg differ