Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added decorator/assets/img/depau.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions decorator/public/image.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
require __DIR__ . '/../vendor/autoload.php';

use Styde\ImageJpeg;

$image = ImageJpeg::make(assets_path('img/decorator.jpeg'),1000,666,false,20);

header('Content-type: jpeg');
imagejpeg($image->draw());
29 changes: 29 additions & 0 deletions decorator/src/FrameDecoratorJpeg.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php


namespace Styde;


class FrameDecoratorJpeg extends ImageDecorator
{
protected $thickness;

public function __construct($thickness = 1)
{
$this->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;
}
}
15 changes: 15 additions & 0 deletions decorator/src/GrayscaleDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php


namespace Styde;


class GrayscaleDecorator extends ImageDecorator
{
public function draw($image)
{
imagefilter($image, IMG_FILTER_GRAYSCALE);

return $image;
}
}
23 changes: 23 additions & 0 deletions decorator/src/Image.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php


namespace Styde;


class Image
{
protected $image;
protected $decorators = [];

public function setDecorator(ImageDecorator $decorator)
{
$this->decorators[] = $decorator;
}

public function draw()
{
foreach ($this->decorators as $decorator){
$this->image = $decorator->draw($this->image);
}
}
}
10 changes: 10 additions & 0 deletions decorator/src/ImageDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php


namespace Styde;


abstract class ImageDecorator
{
abstract public function draw($image);
}
23 changes: 23 additions & 0 deletions decorator/src/ImageJpeg.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php


namespace Styde;


class ImageJpeg extends Image
{
protected $path;
protected $image;

public function __construct($path)
{
$this->path = $path;
$this->image = imagecreatefromjpeg($this->path);
}

public function draw()
{
parent::draw();
return $this->image;
}
}
22 changes: 22 additions & 0 deletions decorator/src/ResizeDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php


namespace Styde;


class ResizeDecorator extends ImageDecorator
{
protected $width;
protected $height;

public function __construct($width, $height)
{
$this->width = $width;
$this->height = $height;
}

public function draw($image)
{
return imagescale($image, $this->width, $this->height);
}
}
38 changes: 38 additions & 0 deletions decorator/src/WatermarkDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php


namespace Styde;


class WatermarkDecorator extends ImageDecorator
{
private $bottonMargin;
private $rightMargin;

public function __construct($bottonMargin = 0, $rightMargin = 0)
{
$this->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;
}
}
117 changes: 117 additions & 0 deletions decorator/tests/ImageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

namespace Styde\Tests;


use Styde\FrameDecoratorJpeg;
use Styde\GrayscaleDecorator;
use Styde\ImageJpeg;
use Styde\ResizeDecorator;
use Styde\WatermarkDecorator;

class ImageTest extends TestCase
{
/**
* @test
*/
function it_can_draw_an_image()
{
$image = new ImageJpeg(assets_path('img/decorator.jpeg'));

$this->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;
}
}
Binary file added decorator/tests/snapshots/basic-image.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added decorator/tests/snapshots/framed-image.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added decorator/tests/snapshots/grey-size-image.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added decorator/tests/snapshots/greyscale-image.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added decorator/tests/snapshots/resized-image.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added decorator/tests/snapshots/watermarked-image.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.