PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, September 19, 2022

[FIXED] How to translate ImageMagick command to Imagick in PHP?

 September 19, 2022     imagemagick, imagick, php     No comments   

Issue

I've tried to translate it, but doesn't work, doesn't anyone know what the wrong is ?

ImageMagick

convert source.jpg \( -size 640x480 xc:white -size 200x200 
xc:black -geometry +200+100 -compose over -composite \) 
+geometry -alpha off -compose copy_opacity -composite result.png

PHP code with Imagick I tried, but didn't work:

//Open your image and get its dimensions
$image = new Imagick('source.png');
$height = $image->getImageHeight();
$width = $image->getImageWidth();

//Create a new transparent image of the same size
$mask = new Imagick();
$mask->newImage($width, $height, new ImagickPixel('white'));

//Draw onto the new image the areas you want to be transparent in the original
$draw = new ImagickDraw();
$draw->setFillColor('black'); 
$draw->rectangle($x, $y, $x + 200, $y + 200);
$mask->drawImage( $draw );

//Composite the images
$image->compositeImage($mask, Imagick::COMPOSITE_COPYOPACITY, 0, 0, Imagick::CHANNEL_ALPHA);
$image->setImageFormat('png');
$image->writeImage("~/images/result.png");

Original Question:

How to make specified area of an image transparent with Imagick?

Another trying

$width = 256;
$height = 256;
$x = 50;
$y = 100;
$fooWidth = 100;
$fooHeight = 60;


$image = new Imagick();
$image->newImage($width, $height, new ImagickPixel('yellow'));


//Create a new transparent image of the same size
$mask = new Imagick();
$mask->newImage($width, $height, new ImagickPixel('white'));
$mask->setImageFormat('png');

//Draw onto the new image the areas you want to be transparent in the original
$draw = new ImagickDraw();
$draw->setFillColor('black');
$draw->rectangle($x, $y, $x + $fooWidth, $y + $fooHeight);
$mask->drawImage($draw);

//Composite the images
$image->compositeImage($mask, Imagick::COMPOSITE_COPYOPACITY, 0, 0);

$image->setImageFormat('png');
$image->writeImage($path);

COMPOSITE_COPYOPACITY looks doesn't work:

enter image description here


Solution

try turning alpha off at the end when you create the mask. This works fine for me:

convert -size 500x500 xc:yellow \( -size 500x500 xc:white -fill black -draw "rectangle 100,100 300,300" -alpha off \) -compose copy_opacity -composite result.png

enter image description here

See http://us3.php.net/manual/en/imagick.setimagematte.php where the example adds

$mask->setImageMatte(false);

After the draw command and before the compositeImage() command



Answered By - fmw42
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing