I'm trying to darken the image with PHP. Have tried this code, but it doesn't work. Have searched all over the Net and can't find any working solution. Please help.
$img = imagecreatefromjpeg($imgage_path);
$size=getimagesize($imgage_path);
$w=(int)$size[0];
$h=(int)$size[1];
$stamp = imagecreatetruecolor(100, 100);
$black = imagecolorallocate($stamp, 0, 0, 0);
imagefilledrectangle($stamp, 0, 0, $w, $h, $black);
imagecopymerge($img, $stamp, $w, $h, 0, 0, $w, $h, 50);
CodePudding user response:
Perhaps the following might help - there are 10 successive layers of the dark overlay applied ~ the opacity of the overlay remains constant but you will observe a definite darkening of the source image. Each layer and text is offset to show the effect clearer.
This is done by using imagecolorallocatealpha
for the overlay
<?php
$imgsrc='https://a.ltrbxd.com/resized/sm/upload/wi/cl/t4/l0/barbarella-1200-1200-675-675-crop-000000.jpg';
$text='It\'s getting darker...';
$img=imagecreatefromjpeg( $imgsrc );
list( $w, $h, $t, $a )=getimagesize( $imgsrc );
$opacity=99;
$fontsize=15;
$x=0;
$y=0;
$dx=50;
$dy=50;
$overlay = imagecolorallocatealpha( $img, 0, 0, 0, $opacity );
$colour = imagecolorallocate( $img, 255, 255, 255 );
$font = sprintf('%s/arial.ttf',__DIR__);
# add layers of overlays to show the darkening effect
for( $i=1; $i <= 10; $i ){
$x =$dx;
$y =$dy;
$text=$i > 2 ? 'and darker' : $text;
$text=$i==10 ? 'and noticably darker now!' : $text;
$msg=$i==1 ? 'Original' : $text;
imagefilledrectangle( $img, $x, $y, $w, $h, $overlay );
imagefttext( $img, $fontsize, 0, $x, $y - $fontsize, $colour, $font, sprintf( '[%d] %s', $i, $msg ) );
}
header('Content-Type: image/jpeg');
imagejpeg($img);
imagedestroy($img);
?>
CodePudding user response:
People at other site suggested me much simpler solution that works:
imagefilter($img, IMG_FILTER_BRIGHTNESS, -100);