Skip to content

Commit

Permalink
set opacity for canvas in copyResampled and add alpha example script
Browse files Browse the repository at this point in the history
  • Loading branch information
antonlukin committed Jul 6, 2024
1 parent 10ef426 commit 2c31ec7
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 21 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "antonlukin/poster-editor",
"version": "5.14",
"version": "5.15",
"description": "Wrapper for PHP's GD Library for easy image manipulation",
"keywords": ["php", "image", "text", "gd"],
"homepage": "https://github.com/antonlukin/poster-editor",
Expand Down
50 changes: 50 additions & 0 deletions example/alpha.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Opacity image and text support example.
* php version 7.1
*
* @category PHP
* @package PosterEditor
* @author Anton Lukin <anton@lukin.me>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
* @link https://github.com/antonlukin/poster-editor
*/

require_once __DIR__ . '/../vendor/autoload.php';

try {
$image = new PosterEditor\PosterEditor();
$image->canvas(500, 500, array('color' => '#fff'));

$image->insert(
'images/icon.png',
array(
'y' => 50,
'opacity' => 0,
)
);

$image->insert(
file_get_contents('images/icon.png'),
array(
'y' => 200,
'opacity' => 40,
)
);

$icon = new PosterEditor\PosterEditor();
$icon->make(file_get_contents('images/icon.png'));

$image->insert(
$icon,
array(
'y' => 350,
'opacity' => 80,
)
);

$image->show('png');

} catch(Exception $e) {
echo $e->getMessage();
}
Binary file added example/images/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 1 addition & 7 deletions example/insert.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@
$image = new PosterEditor\PosterEditor();
$image->make('images/bridge.jpg')->fit(1000, 600, 'bottom-left');
$image->grayscale()->invert();

$image->insert(
'images/logo.png',
array(
'width' => 500,
)
);
$image->insert('images/logo.png');

$image->show();

Expand Down
9 changes: 5 additions & 4 deletions example/make.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
require_once __DIR__ . '/../vendor/autoload.php';

try {
$file = file_get_contents('images/bridge.jpg');

$image = new PosterEditor\PosterEditor();
$image->make($file)->downsize(null, 200)->invert()->show();
$image->make(file_get_contents('images/bridge.jpg'))->downsize(null, 1000)->invert();

$logo = new PosterEditor\PosterEditor();
$logo->make('images/logo.png')->downsize(null, 100)->invert();

$image->insert('images/logo.png')->show('png');
$image->insert($logo, array('y' => 0))->show('png');

} catch(Exception $e) {
echo $e->getMessage();
Expand Down
5 changes: 1 addition & 4 deletions example/opacity.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
array(
'x' => 600,
'y' => 0,
'height' => 100,
'opacity' => 0,
)
);
Expand All @@ -31,7 +30,6 @@
array(
'x' => 600,
'y' => 120,
'height' => 100,
'opacity' => 40,
)
);
Expand All @@ -41,7 +39,6 @@
array(
'x' => 600,
'y' => 240,
'height' => 100,
'opacity' => 80,
)
);
Expand Down Expand Up @@ -88,7 +85,7 @@
)
);

$image->show();
$image->show('png');

} catch(Exception $e) {
echo $e->getMessage();
Expand Down
Binary file added example/samples/alpha.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 5 additions & 5 deletions src/PosterEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* @package PosterEditor
* @author Anton Lukin <anton@lukin.me>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
* @version Release: 5.14
* @version Release: 5.15
* @link https://github.com/antonlukin/poster-editor
*/
class PosterEditor
Expand Down Expand Up @@ -155,12 +155,12 @@ public function insert($data, $options = array(), &$boundary = array())

list($width, $height, $type, $source) = $image;

// We need to reverse opacity format for imagecopymergeAlpha function.
// We need to reverse opacity format for copyMergeAlpha function.
$opacity = 100 - $options['opacity'];

$options = $this->calcPosition($options, $width, $height);

$this->imagecopymergeAlpha($this->resource, $source, $options['x'], $options['y'], 0, 0, $width, $height, $opacity);
$this->copyMergeAlpha($this->resource, $source, $options['x'], $options['y'], 0, 0, $width, $height, $opacity);

imagedestroy($source);

Expand Down Expand Up @@ -1254,7 +1254,7 @@ protected function calcPosition($options, $width, $height)
*/
protected function copyResampled($source, $dx, $dy, $sx, $sy, $dw, $dh, $sw, $sh)
{
$this->canvas($dw, $dh);
$this->canvas($dw, $dh, array('opacity' => 100));

imagecopyresampled($this->resource, $source, $dx, $dy, $sx, $sy, $dw, $dh, $sw, $sh);
imagedestroy($source);
Expand All @@ -1277,7 +1277,7 @@ protected function copyResampled($source, $dx, $dy, $sx, $sy, $dw, $dh, $sw, $sh
*
* @return void
*/
public function imagecopymergeAlpha($resource, $source, $dx, $dy, $sx, $sy, $sw, $sh, $pct)
public function copyMergeAlpha($resource, $source, $dx, $dy, $sx, $sy, $sw, $sh, $pct)
{
$cut = imagecreatetruecolor($sw, $sh);

Expand Down

0 comments on commit 2c31ec7

Please sign in to comment.