Rubblewebs

THESE CODES ARE PROVIDED FOR AN EXAMPLE OF HOW TO USE IMAGEMAGICK WITH PHP. CARE SHOULD BE TAKEN WHEN ACCEPTING USER INPUT.

I TAKE NO RESPONSABILTY FOR ANY PROBLEMS THAT MAY OCCURE WHEN USING ANY OF THIS CODE.

IT IS UP TO YOU TO SECURE THE CODE AND VALIDATE USER INPUT.

Green mask

This takes an image and turns all the colours apart from the one defined to grey.

Green mask example
 
// Create a new image with everything transparent apart from the selected colour. 
// This image must be saved as a png due to the transparency.
$cmd = " $input20 -matte ".
" ( +clone -fuzz 20% -transparent rgb(38,134,71) ) ".
" -compose DstOut -composite ";
exec("convert $cmd output_hat.png");

// Another temporary image is made from the original but completely grey.
$cmd = " $input20 -colorspace Gray ";
exec("convert $cmd grey_background.png"); 

// The two images are combined and flattened into one image.
$cmd = " grey_background.png -page +0+0 output_hat.png -flatten ";
exec("convert $cmd green.jpg");
 
// The tempory images are deleted.
unlink ('output_hat.png');
unlink ('grey_background.png');

Do not forget to change this line ( +clone -fuzz 20% -transparent rgb(38,134,71) ) to this on Linux \( +clone -fuzz 20% -transparent rgb\(38,134,71\) \)


Back