Skip to content

Commit

Permalink
Merge pull request #2 from lohanidamodar/feat-new-functionalities
Browse files Browse the repository at this point in the history
Feat new functionalities
  • Loading branch information
eldadfux authored Mar 18, 2021
2 parents 66e38db + 0d3ad96 commit 3b22910
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,25 @@ composer require utopia-php/image
require_once '../vendor/autoload.php';

use Utopia\Image\Image;

//crop image
$image = new Image(\file_get_contents('image.jpg'));
$target = 'image_100x100.jpg';
$image->crop(100, 100);
$image->save($target, 'jpg', 100);

$image = new Image(\file_get_contents('image.jpg'));
$target = 'image_border.jpg';
$image->setBorder(2, "#ff0000"); //add border 2 px, red
$image->setRotation(45); //rotate 45 degree
$image->save($target, 'jpg', 100);


$image = new Image(\file_get_contents('image.jpg'));
$target = 'image_border.jpg';
$image->setOpacity(0.2); //set opacity
$image->save($target, 'png', 100);

```

## System Requirements
Expand Down
78 changes: 78 additions & 0 deletions src/Image/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Exception;
use Imagick;
use ImagickDraw;
use ImagickPixel;

class Image
{
Expand Down Expand Up @@ -64,10 +66,86 @@ public function crop(int $width, int $height)
} else {
$this->image->cropThumbnailImage($width, $height);
}
$this->height = $height;
$this->width = $width;
return $this;
}

/**
* @param integer $borderWidth The size of the border in pixels
* @param string $borderColor The color of the border in hex format
*
* @return Image
*
* @throws \ImagickException
*/
public function setBorder(int $borderWidth, string $borderColor): self
{
$width = $height = $borderWidth;

$this->image->borderImage($borderColor, $width, $height);

return $this;
}

/**
* Applies rounded corners, background to an image
* @param integer $cornerRadius: The radius for the corners
* @param string $background: A valid HEX string representing the background color
* @return Image $image: The processed image
*
* @throws \ImagickException
*/
public function setBorderRadius(int $cornerRadius): self
{
$mask = new Imagick();
$mask->newImage($this->width, $this->height, new ImagickPixel('transparent'), 'png');

$shape = new ImagickDraw();
$shape->setFillColor(new ImagickPixel('black'));
$shape->roundRectangle(0, 0, $this->width, $this->height, $cornerRadius, $cornerRadius);

$mask->drawImage($shape);

$this->image->compositeImage($mask, Imagick::COMPOSITE_DSTIN, 0, 0);
return $this;
}

/**
* @param float opacity The opacity of the image
*
* @return Image
*
* @throws \ImagickException
*/
public function setOpacity(float $opacity): self
{
if(empty($opacity) || $opacity == 1) {
return $this;
}
$this->image->setImageAlpha($opacity);
return $this;
}

/**
* Rotates an image to $degree degree
* @param integer $degree: The amount to rotate in degrees
* @return Image $image: The rotated image
*
* @throws \ImagickException
*/
public function setRotation(int $degree): self
{
if (empty($degree) || $degree == 0) {
return $this;
}

$this->image->rotateImage('transparent', $degree);

return $this;
}


/**
* @param mixed $color
*
Expand Down
122 changes: 122 additions & 0 deletions tests/Image/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,126 @@ public function testCrop100x100GIF()
$this->assertEquals('GIF', $image->getImageFormat());
\unlink($target);
}

public function testBorder5Red()
{
$image = new Image(\file_get_contents(__DIR__ . '/../resources/disk-a/kitten-1.jpg'));
$target = __DIR__.'/border_5_red.jpg';
$original = __DIR__.'/../resources/resize/border_5_red.jpg';

$image->setBorder(5, "#ff0000");

$image->save($target, 'jpg', 100);

$this->assertEquals(\is_readable($target), true);
$this->assertEquals(\mime_content_type($target), \mime_content_type($original));
$this->assertNotEmpty(\md5(\file_get_contents($target)));

$image = new \Imagick($target);
$this->assertEquals('JPEG', $image->getImageFormat());
\unlink($target);
}

public function testRotate45()
{
$image = new Image(\file_get_contents(__DIR__ . '/../resources/disk-a/kitten-1.jpg'));
$target = __DIR__.'/rotate_45.jpg';
$original = __DIR__.'/../resources/resize/rotate_45.jpg';

$image->setRotation(45);

$image->save($target, 'jpg', 100);

$this->assertEquals(\is_readable($target), true);
$this->assertEquals(\mime_content_type($target), \mime_content_type($original));
$this->assertNotEmpty(\md5(\file_get_contents($target)));

$image = new \Imagick($target);
$this->assertEquals('JPEG', $image->getImageFormat());
$this->assertEquals($image->getImageHeight(), 2658);
$this->assertEquals($image->getImageWidth(), 2659);
\unlink($target);
}

public function testOpacity02()
{
$image = new Image(\file_get_contents(__DIR__ . '/../resources/disk-a/kitten-1.jpg'));
$target = __DIR__.'/opacity_0.2.png';
$original = __DIR__.'/../resources/resize/opacity_0.2.png';

$image->setOpacity(0.2);

$image->save($target, 'png', 100);

$this->assertEquals(\is_readable($target), true);
$this->assertEquals(\mime_content_type($target), \mime_content_type($original));
$this->assertNotEmpty(\md5(\file_get_contents($target)));

$image = new \Imagick($target);
$this->assertEquals('PNG', $image->getImageFormat());
\unlink($target);
}

public function testBorderRadius500()
{
$image = new Image(\file_get_contents(__DIR__ . '/../resources/disk-a/kitten-1.jpg'));
$target = __DIR__.'/border_radius_500.png';
$original = __DIR__.'/../resources/resize/border_radius_500.png';

$image->setBorderRadius(500);

$image->save($target, 'png', 100);

$this->assertEquals(\is_readable($target), true);
$this->assertEquals(\mime_content_type($target), \mime_content_type($original));
$this->assertNotEmpty(\md5(\file_get_contents($target)));

$image = new \Imagick($target);
$this->assertEquals('PNG', $image->getImageFormat());
\unlink($target);
}

public function testCrop100Op05()
{
$image = new Image(\file_get_contents(__DIR__ . '/../resources/disk-a/kitten-1.jpg'));
$target = __DIR__.'/100x100_OP_0.5.png';
$original = __DIR__.'/../resources/resize/100x100_OP_0.5.png';

$image->crop(100,100);
$image->setOpacity(0.5);

$image->save($target, 'png', 100);

$this->assertEquals(\is_readable($target), true);
$this->assertEquals(\mime_content_type($target), \mime_content_type($original));
$this->assertNotEmpty(\md5(\file_get_contents($target)));

$image = new \Imagick($target);
$this->assertEquals($image->getImageWidth(), 100);
$this->assertEquals($image->getImageHeight(), 100);
$this->assertEquals('PNG', $image->getImageFormat());
\unlink($target);
}

public function testCrop100BR50()
{
$image = new Image(\file_get_contents(__DIR__ . '/../resources/disk-a/kitten-1.jpg'));
$target = __DIR__.'/100x100_BR_50.png';
$original = __DIR__.'/../resources/resize/100x100_BR_50.png';

$image->crop(100,100);
$image->setOpacity(0.5);

$image->save($target, 'png', 100);

$this->assertEquals(\is_readable($target), true);
$this->assertEquals(\mime_content_type($target), \mime_content_type($original));
$this->assertNotEmpty(\md5(\file_get_contents($target)));

$image = new \Imagick($target);
$this->assertEquals('PNG', $image->getImageFormat());
\unlink($target);
}


}
Binary file added tests/resources/resize/100x100_BR_50.png
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 tests/resources/resize/100x100_OP_0.5.png
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 tests/resources/resize/border_5_red.jpg
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 tests/resources/resize/border_radius_500.png
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 tests/resources/resize/opacity_0.2.png
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 tests/resources/resize/rotate_45.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 3b22910

Please sign in to comment.