Skip to content

Commit

Permalink
new functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tpmanc committed Feb 5, 2016
1 parent c56961d commit 2b32bbd
Show file tree
Hide file tree
Showing 17 changed files with 280 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
examples/ export-ignore
166 changes: 166 additions & 0 deletions Imagick.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*/
namespace tpmanc\imagick;

use yii\base\InvalidConfigException;

/**
* Working with Imagemagick
*/
Expand All @@ -21,6 +23,23 @@ class Imagick
*/
private $width;

/**
* Get opened image width
* @return integer Image width
*/
public function getWidth()
{
return $this->width;
}

/**
* Get opened image height
* @return integer Image height
*/
public function getHeight()
{
return $this->height;
}

private function __construct() {}

Expand Down Expand Up @@ -49,6 +68,153 @@ public function saveTo($path)
$this->image->destroy();
}

/**
* Add border
* @param integer $width Border width
* @param string $color Border color
* @return tpmanc\imagick\Imagick
*/
public function border($width, $color)
{
$border = new \ImagickDraw();
$border->setFillColor('none');
$border->setStrokeColor(new \ImagickPixel($color));
$border->setStrokeWidth($width);
$widthPart = $width / 2;
$border->line(0, 0 + $widthPart, $this->width, 0 + $widthPart);
$border->line(0, $this->height - $widthPart, $this->width, $this->height - $widthPart);
$border->line(0 + $widthPart, 0, 0 + $widthPart, $this->height);
$border->line($this->width - $widthPart, 0, $this->width - $widthPart, $this->height);
$this->image->drawImage($border);
return $this;
}

/**
* Blur
* @param float $radius
* @param float $delta
* @return tpmanc\imagick\Imagick
*/
public function blur($radius, $delta)
{
$this->image->blurImage($radius, $delta);
return $this;
}

/**
* Crop image part
* @param integer $startX
* @param integer $startY
* @param integer $width
* @param integer $height
* @return tpmanc\imagick\Imagick
*/
public function crop($startX, $startY, $width, $height)
{
$this->image->cropImage($width, $height, $startX, $startY);
return $this;
}

/**
* Vertical mirror image
* @return tpmanc\imagick\Imagick
*/
public function flip()
{
$this->image->flipImage();
return $this;
}

/**
* Horizontal mirror image
* @return tpmanc\imagick\Imagick
*/
public function flop()
{
$this->image->flopImage();
return $this;
}

/**
* Add watermark to image
* @param string $watermarkPath Path to watermark image
* @param string $xPos Horizontal position - 'left', 'right' or 'center'
* @param string $yPos Vertical position - 'top', 'bottom' or 'center'
* @param string|integer $xSize Horizontal watermark size: 100, '50%', 'auto' etc.
* @param string|integer $ySize Vertical watermark size: 100, '50%', 'auto' etc.
* @return tpmanc\imagick\Imagick
*/
public function watermark($watermarkPath, $xPos, $yPos, $xSize = false, $ySize = false, $xOffset = false, $yOffset = false)
{
$watermark = new \Imagick($watermarkPath);

// resize watermark
$newSizeX = false;
$newSizeY = false;
if ($xSize !== false) {
if (is_numeric($xSize)) {
$newSizeX = $xSize;
} elseif (is_string($xSize) && substr($xSize, -1) === '%') {
$float = str_replace('%', '', $xSize) / 100;
$newSizeX = $this->width * ((float) $float);
}
}
if ($ySize !== false) {
if (is_numeric($ySize)) {
$newSizeY = $ySize;
} elseif (is_string($ySize) && substr($ySize, -1) === '%') {
$float = str_replace('%', '', $ySize) / 100;
$newSizeY = $this->height * ((float) $float);
}
}
// var_dump($newSizeX);var_dump($newSizeY);die();
if ($newSizeX !== false && $newSizeY !== false) {
$watermark->adaptiveResizeImage($newSizeX, $newSizeY);
} elseif ($newSizeX !== false && $newSizeY === false) {
$watermark->adaptiveResizeImage($newSizeX, 0);
} elseif ($newSizeX === false && $newSizeY !== false) {
$watermark->adaptiveResizeImage(0, $newSizeY);
}

$startX = false;
$startY = false;
$watermarkSize = $watermark->getImageGeometry();
if ($yPos === 'top') {
$startY = 0;
if ($yOffset !== false) {
$startY += $yOffset;
}
} elseif ($yPos === 'bottom') {
$startY = $this->height - $watermarkSize['height'];
if ($yOffset !== false) {
$startY -= $yOffset;
}
} elseif ($yPos === 'center') {
$startY = ($this->height / 2) - ($watermarkSize['height'] / 2);
} else {
throw new InvalidConfigException('Param $yPos should be "top", "bottom" or "center" insteed "'.$yPos.'"', 1);
}

if ($xPos === 'left') {
$startX = 0;
if ($xOffset !== false) {
$startX += $xOffset;
}
} elseif ($xPos === 'right') {
$startX = $this->width - $watermarkSize['width'];
if ($xOffset !== false) {
$startX -= $xOffset;
}
} elseif ($xPos === 'center') {
$startX = ($this->width / 2) - ($watermarkSize['width'] / 2);
} else {
throw new InvalidConfigException('Param $xPos should be "left", "right" or "center" insteed "'.$xPos.'"', 1);
}

$this->image->compositeImage($watermark, \Imagick::COMPOSITE_OVER, $startX, $startY);
return $this;
}

/**
* Create thumbnail
* @param integer $width
Expand Down
113 changes: 112 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ $ "tpmanc/imagick": "*"

to the require section of your `composer.json` file.

## Get size

```php
$img = Imagick::open('./image.jpg');
$img->getWidth();
$img->getHeight();
```

## Resize image

```php
Expand All @@ -28,4 +36,107 @@ Imagick::open('./image.jpg')->resize(800, false)->saveTo('./resized.jpg');

```php
Imagick::open('./image.jpg')->thumb(64, 64)->saveTo('./thumb.jpg');
```
```

## Add border

```php
$width = 5;
$color = '#000'
Imagick::open('./image.jpg')->border($width, $color)->saveTo('./result.jpg');
```

```php
$width = 10;
$color = '#A91AD4'
Imagick::open('./image.jpg')->border($width, $color)->saveTo('./result.jpg');
```

## Vertical and horizontal mirror image

```php
// vertical
Imagick::open('./image.jpg')->flip()->saveTo('./result.jpg');
// horizontal
Imagick::open('./image.jpg')->flop()->saveTo('./result.jpg');
```

## Crop

```php
$xStart = 0;
$yStart = 0;
$xEnd = 150;
$yEnd = 150;
Imagick::open('./image.jpg')->crop($xStart, $yStart, $xEnd, $yEnd)->saveTo('./result.jpg');
```

## Blur

```php
$radius = 8;
$delta = 5;
Imagick::open('./image.jpg')->blur($radius, $delta)->saveTo('./result.jpg');
```

## Watermark

`xSize` and `ySize`

### Set watermark position

Use `$xPosition` and `$yPosition` to set watermark position.

`$xPosition` should be 'left', 'right' or 'center'; `$yPosition` should be 'top', 'bottom' or 'center'.

```php
$xPosition = 'left';
$yPosition = 'top';
Imagick::open('./image.jpg')->watermark('./watermark.png'), $xPosition, $yPosition)->saveTo('./result.jpg');
```

```php
$xPosition = 'right';
$yPosition = 'center';
Imagick::open('./image.jpg')->watermark('./watermark.png'), $xPosition, $yPosition)->saveTo('./result.jpg');
```
### Set watermark size

Use `$xSize` and `$ySize` to set watermark size. Valid values:

* Number: `$xSize = 100;`, `$ySize = 50`

* Percent of parent: `$xSize = '100%';`, `$ySize = '50%'`

* 'auto' to save proportion: `$xSize = '100%';`, `$ySize = 'auto'`

* False: `$xSize = 100;`, `$ySize = false`

```php
$xPosition = 'center';
$yPosition = 'center';
$xSize = '100%';
$ySize = 'auto';
Imagick::open('./image.jpg')->watermark('./watermark.png'), $xPosition, $yPosition, $xSize, $ySize)->saveTo('./result.jpg');
```

```php
$xPosition = 'center';
$yPosition = 'center';
$xSize = '100%';
$ySize = '100%';
Imagick::open('./image.jpg')->watermark('./watermark.png'), $xPosition, $yPosition, $xSize, $ySize)->saveTo('./result.jpg');
```
### Set watermark offset

Use `$xOffset` and `$yOffset` to set offset from parent image border.

```php
$xPosition = 'right';
$yPosition = 'bottom';
$xSize = false;
$ySize = false;
$xOffset = 50;
$yOffset = 50;
Imagick::open('./image.jpg')->watermark('./watermark.png'), $xPosition, $yPosition, $xSize, $ySize, $xOffset, $yOffset)->saveTo('./result.jpg');
```
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
{
"name": "tpmanc",
"email": "tpxtrime@mail.ru",
"homepage": "https://github.com/tpmanc",
"homepage": "https://tpmanc.github.io",
"role": "Developer"
}
],
Expand Down
Binary file added examples/blur.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 examples/border-1.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 examples/border-2.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 examples/crop.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 examples/flip.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 examples/flop.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 examples/resize.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 examples/thumb.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 examples/watermark-1.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 examples/watermark-2.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 examples/watermark-3.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 examples/watermark-4.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 examples/watermark-5.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 2b32bbd

Please sign in to comment.