Skip to content

Commit

Permalink
add setOpacity
Browse files Browse the repository at this point in the history
  • Loading branch information
Franck ALARY committed May 30, 2022
1 parent 98985fa commit 16ed5da
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 21 deletions.
28 changes: 27 additions & 1 deletion docs/classes/DantSu/PHPImageEditor/Image.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ DantSu\PHPImageEditor\Image is PHP library to easily edit image with GD extensio
- [pasteOn](#-pasteon)
- [pasteGdImageOn](#-pastegdimageon)
- [alphaMask](#-alphamask)
- [setOpacity](#-setopacity)
- [grayscale](#-grayscale)
- [writeText](#-writetext)
- [writeTextAndGetBoundingBox](#-writetextandgetboundingbox)
Expand Down Expand Up @@ -781,6 +782,31 @@ Use a grayscale image (`$mask`) to apply transparency to the image.



---
### ->setOpacity

change the image opacity








#### Parameters:

| Parameter | Type | Description |
|-----------|------|-------------|
| `opacity` | **float** | Opacity (0 to 1) |


#### Return Value:

**$this** : Fluent interface



---
### ->grayscale

Expand Down Expand Up @@ -1404,4 +1430,4 @@ Get image GIF base64 data for <img src=""> tag.


---
> Automatically generated from source code comments on 2022-05-24 using [phpDocumentor](http://www.phpdoc.org/)
> Automatically generated from source code comments on 2022-05-30 using [phpDocumentor](http://www.phpdoc.org/)
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ This is an automatically generated documentation for **PHP Image Editor**.


---
> Automatically generated from source code comments on 2022-05-24 using [phpDocumentor](http://www.phpdoc.org/)
> Automatically generated from source code comments on 2022-05-30 using [phpDocumentor](http://www.phpdoc.org/)
50 changes: 33 additions & 17 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function resetCanvas(int $width, int $height): Image

\imagealphablending($this->image, false);
\imagesavealpha($this->image, true);
\imagecolortransparent($this->image, \imagecolorallocate($this->image, 0, 0, 0));
\imagefill($this->image, 0, 0, \imagecolorallocatealpha($this->image, 0, 0, 0, 127));

$this->width = $width;
$this->height = $height;
Expand Down Expand Up @@ -478,14 +478,9 @@ public function resize(int $width, int $height): Image
return $this;
}

if (
($image = \imagecreatetruecolor($width, $height)) !== false &&
\imagealphablending($image, false) !== false &&
\imagesavealpha($image, true) !== false &&
($transparent = $this->colorAllocate('#000000FF')) !== false &&
\imagefill($image, 0, 0, $transparent) !== false &&
\imagecopyresampled($image, $this->image, 0, 0, 0, 0, $width, $height, $this->width, $this->height) !== false
) {
$image = Image::newCanvas($width, $height)->getImage();

if (\imagecopyresampled($image, $this->image, 0, 0, 0, 0, $width, $height, $this->width, $this->height) !== false) {
$this->image = $image;
$this->width = $width;
$this->height = $height;
Expand All @@ -512,7 +507,6 @@ public function downscaleAndCrop(int $width, int $height, $posX = Image::ALIGN_C
$height = $this->height;
}


$finalWidth = \round($this->width * $height / $this->height);
$finalHeight = $height;

Expand Down Expand Up @@ -724,6 +718,10 @@ public function alphaMask(Image $mask): Image

if ($alpha != 127) {
$rgb = \imagecolorat($this->image, $j, $i);
$alpha = 127 - \ceil((127 - (($rgb >> 24) & 0x7F)) * (127 - $alpha) / 127);
}

if ($alpha != 127) {
$red = ($rgb >> 16) & 0xFF;
$green = ($rgb >> 8) & 0xFF;
$blue = $rgb & 0xFF;
Expand All @@ -746,6 +744,25 @@ public function alphaMask(Image $mask): Image
return $this;
}

/**
* change the image opacity
*
* @param float $opacity Opacity (0 to 1)
* @return $this Fluent interface
*/
public function setOpacity(float $opacity): Image
{
if (!$this->isImageDefined()) {
return $this;
}

\imagealphablending($this->image, false);
\imagesavealpha($this->image, true);
\imagefilter($this->image, IMG_FILTER_COLORIZE, 0, 0, 0, 127 * (1 - $opacity));

return $this;
}

//===============================================================================================================================
//=========================================================POST PROD=============================================================
//===============================================================================================================================
Expand Down Expand Up @@ -1204,18 +1221,17 @@ public function displayGIF()
/**
* Get image raw data
*
* @param string $nameFunction Image function to be called
* @param int $quality JPG quality : 0 to 100
* @param callable $imgFunction Image function to be called
* @return string Data
*/
private function getData(string $nameFunction, int $quality = -1): string
private function getData(callable $imgFunction): string
{
if (!$this->isImageDefined()) {
return '';
}

\ob_start();
$nameFunction($this->image, null, $quality, -1);
$imgFunction();
$image_data = \ob_get_contents();
\ob_end_clean();

Expand All @@ -1229,7 +1245,7 @@ private function getData(string $nameFunction, int $quality = -1): string
*/
public function getDataPNG(): string
{
return $this->getData('imagepng');
return $this->getData(function () {$this->displayPNG();});
}

/**
Expand All @@ -1240,7 +1256,7 @@ public function getDataPNG(): string
*/
public function getDataJPG(int $quality = -1): string
{
return $this->getData('imagejpeg', $quality);
return $this->getData(function () use ($quality) {$this->displayJPG($quality);});
}

/**
Expand All @@ -1250,7 +1266,7 @@ public function getDataJPG(int $quality = -1): string
*/
public function getDataGIF(): string
{
return $this->getData('imagegif');
return $this->getData(function () {$this->displayGIF();});
}

/**
Expand Down
Binary file added src/samples/resources/sample5_base.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 src/samples/resources/sample5_mask.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions src/samples/sample4.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
->drawCircle(450, 600, 200, '#FFFFFF88')
->pasteOn(
Image::newCanvas(1920, 1080)
->drawPolygon([1110, 500, 1240, 250, 1400, 140, 1650, 280, 1400, 400, 1800, 510, 1400, 950, 1180, 620, 1230, 540], '#88229988')
->drawCircle(1450, 600, 200, '#FFFFFF88'),
->drawPolygon([1110, 500, 1240, 250, 1400, 140, 1650, 280, 1400, 400, 1800, 510, 1400, 950, 1180, 620, 1230, 540], '#882299')
->drawCircle(1450, 600, 200)
->setOpacity(0.6),
0,
0
)
Expand Down
14 changes: 14 additions & 0 deletions src/samples/sample5.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

require_once '../Geometry2D.php';
require_once '../Image.php';

use \DantSu\PHPImageEditor\Image;

\header('Content-type: image/png');

Image::fromPath(__DIR__ . '/resources/sample5_base.png')
->alphaMask(Image::fromPath(__DIR__ . '/resources/sample5_mask.png'))
->crop(494, 494, 9, 12)
->setOpacity(0.5)
->displayPNG();

0 comments on commit 16ed5da

Please sign in to comment.