Mask an image with imagemagick


September 10, 2014 at 10:45
PHP

This is the task: I have an image with the sizes 500x500. From this image I want to create a new Image, 200x200, where the former Image is cut from a circle. Everything outside the circle is supposed to be transparent. This can be nicely done with imagemagick:
//the base 500x500 image
$baseImage = 'img/base.jpg';

//the mask image, where the circle is transparent and everything outsite is black
$maskImage = 'img/kreis.png';

//step1: resize the base image to 200x200
$minFile = $path.'tmp/'.uniqid("tmp_").'.png';
$cmd = sprintf('convert %s -resize 200x200^ %s', $baseImage, $baseImage);
exec($cmd);

//step2: create a new temporary image in /tmp/ folder
$temp = 'tmp/' . uniqid("tmp_") . '.png' ;
touch( $temp );

//step3: place the mask over it
$cmd = sprintf( "convert %s %s -alpha off -compose CopyOpacity -composite %s", $baseImage, $maskImage, $temp );
exec( $cmd );
And that's it.

Tags: imagemagick


Hinterlasse einen Kommentar:
Search