Skip to content

Commit 16ed5da

Browse files
committed
add setOpacity
1 parent 98985fa commit 16ed5da

File tree

7 files changed

+78
-21
lines changed

7 files changed

+78
-21
lines changed

docs/classes/DantSu/PHPImageEditor/Image.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ DantSu\PHPImageEditor\Image is PHP library to easily edit image with GD extensio
5555
- [pasteOn](#-pasteon)
5656
- [pasteGdImageOn](#-pastegdimageon)
5757
- [alphaMask](#-alphamask)
58+
- [setOpacity](#-setopacity)
5859
- [grayscale](#-grayscale)
5960
- [writeText](#-writetext)
6061
- [writeTextAndGetBoundingBox](#-writetextandgetboundingbox)
@@ -781,6 +782,31 @@ Use a grayscale image (`$mask`) to apply transparency to the image.
781782

782783

783784

785+
---
786+
### ->setOpacity
787+
788+
change the image opacity
789+
790+
791+
792+
793+
794+
795+
796+
797+
#### Parameters:
798+
799+
| Parameter | Type | Description |
800+
|-----------|------|-------------|
801+
| `opacity` | **float** | Opacity (0 to 1) |
802+
803+
804+
#### Return Value:
805+
806+
**$this** : Fluent interface
807+
808+
809+
784810
---
785811
### ->grayscale
786812

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

14051431

14061432
---
1407-
> Automatically generated from source code comments on 2022-05-24 using [phpDocumentor](http://www.phpdoc.org/)
1433+
> Automatically generated from source code comments on 2022-05-30 using [phpDocumentor](http://www.phpdoc.org/)

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ This is an automatically generated documentation for **PHP Image Editor**.
1919

2020

2121
---
22-
> Automatically generated from source code comments on 2022-05-24 using [phpDocumentor](http://www.phpdoc.org/)
22+
> Automatically generated from source code comments on 2022-05-30 using [phpDocumentor](http://www.phpdoc.org/)

src/Image.php

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public function resetCanvas(int $width, int $height): Image
136136

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

141141
$this->width = $width;
142142
$this->height = $height;
@@ -478,14 +478,9 @@ public function resize(int $width, int $height): Image
478478
return $this;
479479
}
480480

481-
if (
482-
($image = \imagecreatetruecolor($width, $height)) !== false &&
483-
\imagealphablending($image, false) !== false &&
484-
\imagesavealpha($image, true) !== false &&
485-
($transparent = $this->colorAllocate('#000000FF')) !== false &&
486-
\imagefill($image, 0, 0, $transparent) !== false &&
487-
\imagecopyresampled($image, $this->image, 0, 0, 0, 0, $width, $height, $this->width, $this->height) !== false
488-
) {
481+
$image = Image::newCanvas($width, $height)->getImage();
482+
483+
if (\imagecopyresampled($image, $this->image, 0, 0, 0, 0, $width, $height, $this->width, $this->height) !== false) {
489484
$this->image = $image;
490485
$this->width = $width;
491486
$this->height = $height;
@@ -512,7 +507,6 @@ public function downscaleAndCrop(int $width, int $height, $posX = Image::ALIGN_C
512507
$height = $this->height;
513508
}
514509

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

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

725719
if ($alpha != 127) {
726720
$rgb = \imagecolorat($this->image, $j, $i);
721+
$alpha = 127 - \ceil((127 - (($rgb >> 24) & 0x7F)) * (127 - $alpha) / 127);
722+
}
723+
724+
if ($alpha != 127) {
727725
$red = ($rgb >> 16) & 0xFF;
728726
$green = ($rgb >> 8) & 0xFF;
729727
$blue = $rgb & 0xFF;
@@ -746,6 +744,25 @@ public function alphaMask(Image $mask): Image
746744
return $this;
747745
}
748746

747+
/**
748+
* change the image opacity
749+
*
750+
* @param float $opacity Opacity (0 to 1)
751+
* @return $this Fluent interface
752+
*/
753+
public function setOpacity(float $opacity): Image
754+
{
755+
if (!$this->isImageDefined()) {
756+
return $this;
757+
}
758+
759+
\imagealphablending($this->image, false);
760+
\imagesavealpha($this->image, true);
761+
\imagefilter($this->image, IMG_FILTER_COLORIZE, 0, 0, 0, 127 * (1 - $opacity));
762+
763+
return $this;
764+
}
765+
749766
//===============================================================================================================================
750767
//=========================================================POST PROD=============================================================
751768
//===============================================================================================================================
@@ -1204,18 +1221,17 @@ public function displayGIF()
12041221
/**
12051222
* Get image raw data
12061223
*
1207-
* @param string $nameFunction Image function to be called
1208-
* @param int $quality JPG quality : 0 to 100
1224+
* @param callable $imgFunction Image function to be called
12091225
* @return string Data
12101226
*/
1211-
private function getData(string $nameFunction, int $quality = -1): string
1227+
private function getData(callable $imgFunction): string
12121228
{
12131229
if (!$this->isImageDefined()) {
12141230
return '';
12151231
}
12161232

12171233
\ob_start();
1218-
$nameFunction($this->image, null, $quality, -1);
1234+
$imgFunction();
12191235
$image_data = \ob_get_contents();
12201236
\ob_end_clean();
12211237

@@ -1229,7 +1245,7 @@ private function getData(string $nameFunction, int $quality = -1): string
12291245
*/
12301246
public function getDataPNG(): string
12311247
{
1232-
return $this->getData('imagepng');
1248+
return $this->getData(function () {$this->displayPNG();});
12331249
}
12341250

12351251
/**
@@ -1240,7 +1256,7 @@ public function getDataPNG(): string
12401256
*/
12411257
public function getDataJPG(int $quality = -1): string
12421258
{
1243-
return $this->getData('imagejpeg', $quality);
1259+
return $this->getData(function () use ($quality) {$this->displayJPG($quality);});
12441260
}
12451261

12461262
/**
@@ -1250,7 +1266,7 @@ public function getDataJPG(int $quality = -1): string
12501266
*/
12511267
public function getDataGIF(): string
12521268
{
1253-
return $this->getData('imagegif');
1269+
return $this->getData(function () {$this->displayGIF();});
12541270
}
12551271

12561272
/**
5.83 KB
Loading
5.97 KB
Loading

src/samples/sample4.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
->drawCircle(450, 600, 200, '#FFFFFF88')
1515
->pasteOn(
1616
Image::newCanvas(1920, 1080)
17-
->drawPolygon([1110, 500, 1240, 250, 1400, 140, 1650, 280, 1400, 400, 1800, 510, 1400, 950, 1180, 620, 1230, 540], '#88229988')
18-
->drawCircle(1450, 600, 200, '#FFFFFF88'),
17+
->drawPolygon([1110, 500, 1240, 250, 1400, 140, 1650, 280, 1400, 400, 1800, 510, 1400, 950, 1180, 620, 1230, 540], '#882299')
18+
->drawCircle(1450, 600, 200)
19+
->setOpacity(0.6),
1920
0,
2021
0
2122
)

src/samples/sample5.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
require_once '../Geometry2D.php';
4+
require_once '../Image.php';
5+
6+
use \DantSu\PHPImageEditor\Image;
7+
8+
\header('Content-type: image/png');
9+
10+
Image::fromPath(__DIR__ . '/resources/sample5_base.png')
11+
->alphaMask(Image::fromPath(__DIR__ . '/resources/sample5_mask.png'))
12+
->crop(494, 494, 9, 12)
13+
->setOpacity(0.5)
14+
->displayPNG();

0 commit comments

Comments
 (0)