user2356747 user2356747 - 4 months ago 27
PHP Question

How to have a function like color overlay with color blend mode without using ImageMagick

I want to manipulate an image using php only with the result like the color blend mode in photoshop. I hope you can help me.

I have this code but it returns an image that is like a screen blend mode.

<?php
$img="text.png";
$to="";
$bool="";
$imres=$img;

if(isset($_POST['submit'])){
$im = imagecreatefrompng('text.png');
// $from=$_POST['oldhex'];

$to=$_POST['newhex'];
//conversion of hex to rgb values

// Get width and height
// $w = imagesx($im);
// $h = imagesy($im);
// $im= imagecreatetruecolor($w,$h);



![enter image description here][1]$r = hexdec(substr($to, 0, 2));
$g = hexdec(substr($to, 2, 2));
$b = hexdec(substr($to, 4, 2));
/* R, G, B, so 0, 255, 0 is green */

if($im && imagefilter($im, IMG_FILTER_COLORIZE, $r, $g, $b,30)&& imagealphablending($im, true)){// echo 'Image successfully shaded';


imagepng($im, '5.png');
$bool="true";
$imres="5.png";
}
else{
echo 'shading failed.';
}
}
?>


This is the input image

enter image description here

This is the image that I get...

failed image

This is the image that i want to haveenter image description here

Answer

I have had a try at this and got somethimng fairly similar to what you want - maybe it will give you some ideas for further experimentation. Basically I create another image the same size as yours and fill it with a colour wash and transparency and then blend them.

#!/usr/bin/php
<?php

    $im = imagecreatefrompng('text.png');

    $to='FFCC33';
    //conversion of hex to rgb values

    $r = hexdec(substr($to, 0, 2));
    $g = hexdec(substr($to, 2, 2));
    $b = hexdec(substr($to, 4, 2));

    $wash=imagecreatetruecolor(225,225);
    $col = imagecolorallocatealpha($wash,$r,$g,$b,70);
    imagefill($wash,0,0,$col);

    imagelayereffect($im,IMG_EFFECT_ALPHABLEND);
    imagecopy($im,$wash,0,0,0,0,225,225);
    imagepng($im,'5.png');
?>
Comments