Reputation: 11533
I have a simple HTML
form where the user can upload their image and I need to convert this uploaded image into a specific color with the help of PHP
For example, user upload some image I need to convert the entire image into a specific color (this color is dynamic)
Is there any PHPGD
library that can help me to achieve this?
EDIT
For example, user is uploading this kind of Image,
then I need to convert into below type of Image,
Upvotes: 2
Views: 653
Reputation: 3764
I have another solution for you here, again of course with Imagick:
$im = new Imagick('path/to/start-image')
$im->transformimagecolorspace(Imagick::IMGTYPE_GRAYSCALE);
$im->writeImage('path/to/gray.jpg'));
$im->clear();
$im = new Imagick('path/to/gray.jpg'));
$im->blackThresholdImage( "#cdcdcd" );
$im->writeImage('path/to/black-white.jpg'));
$im->clear();
$im = new Imagick('path/to/black-white.jpg'));
$im->colorizeImage('rgba(209, 15, 16, 1)', 1, true);
$im->writeImage('path/to/red.jpg'));
$im->clear();
Then just delete the intermediate files gray.jpg and black-white.jpg and you will achieve the result you are looking for as here:
Upvotes: 1
Reputation: 425
I have tried to produce something similar. Please test the following code if that satisfies your requirement.
<?PHP
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
/*
@desc replaces target color of an image with the provided fill color
*/
function color_replace($img,$target,$fill,$fuzz){
$img->opaquePaintImage($target, $fill, $fuzz, false, Imagick::CHANNEL_DEFAULT);
return $img;
}
$img = new Imagick('source.png');
$img->setImageAlphaChannel(Imagick::ALPHACHANNEL_DEACTIVATE);
$fuzz = 0.44 * $img->getQuantumRange()['quantumRangeLong'];
$img=color_replace($img,'rgba(50,173,186,255)','red',$fuzz); // replace paste like color with red
$img=color_replace($img,'rgb(230,218,30)','#9c1f24',$fuzz); // replace golden like color with dark red
$img->setImageAlphaChannel(Imagick::ALPHACHANNEL_ACTIVATE);
$img->setImageFormat ("jpeg");
file_put_contents ("test_1.jpg", $img);
?>
Produced output with my program:
With this program, you will be able to change each individual color by calling the color_replace
method every time you want to change color. The following image is an example of that.
Upvotes: 2
Reputation: 207425
I'm still unsure exactly what you are trying to do, but think one of the following may be close. Maybe you can try them in the Terminal until we can finally work out the correct operations then we can hopefully translate them into PHP. This is ImageMagick v7 syntax:
magick image.png -channel RGB -colorspace gray +level-colors red, result.png
Or this:
magick image.png -fill red +opaque white result2.png
You can specify the colour in hex like this for magenta:
magick image.png -channel RGB -colorspace gray -auto-level +level-colors '#ff00ff', result.png
If using v6 ImageMagick, replace magick
with convert
.
My PHP is pretty rusty, but something like this:
#!/usr/local/bin/php -f
<?php
// Emulating something akin to this ImageMagick command:
// magick image.png -fill red +opaque white result.png
// Open input image and get dimensions
$im = new \Imagick('image.png');
// Temporarily deactivate alpha channel
$im->setImageAlphaChannel(Imagick::ALPHACHANNEL_DEACTIVATE);
// Apply colour to non-white areas
$im->opaquePaintImage('white','red', 0, true);
// Reactivate alpha channel
$im->setImageAlphaChannel(Imagick::ALPHACHANNEL_ACTIVATE);
// Save
$im->writeImage('result.png');
?>
Upvotes: 8