how to draw semi-transparent rectangle in php?
Asked Answered
A

2

8

Here is an example what I would like to do:

enter image description here

Here is the result:

function red_rectangle($img_src,$x1,$y1,$x2,$y2,$tr = 50)
{
    // Load image
    $img = imagecreatefromjpeg($img_src);
    // Transparent red
    $red = imagecolorallocatealpha($img, 255, 0, 0, $tr);
    // Draw a white rectangle
    imagefilledrectangle($img, $x1, $y1, $x2, $y2, $red);
    // Save the image (overwrite)
    imagejpeg($img, $img_src);
    imagedestroy($img);
}
Arlberg answered 9/12, 2011 at 9:48 Comment(7)
and what do you have until now?Mayo
this plan: what I'm expecting from the function.Arlberg
@2astalavista: Implement it then. I think you've misunderstood what this site is for.Feriga
@TomalakGeret'kal Why do you think so?Arlberg
@2astalavista: Because this is not a Stack Overflow question.Feriga
@TomalakGeret'kal This is not a Stack Overflow answer.Arlberg
@2astalavista: Correct. These are comments.Feriga
P
5

You need to use http://php.net/manual/en/function.imagefilledrectangle.php, passing a color created with http://www.php.net/manual/en/function.imagecolorallocatealpha.php.

As you can see, the example for http://php.net/manual/en/function.imagefilledrectangle.php is pratically what to you want to do.

Pathetic answered 9/12, 2011 at 9:55 Comment(1)
Note: If imagealphablending() is set to false, the alpha setting will have no effect. docs.Merari
S
4
function red_rectangle($img_src,$x1,$y1,$x2,$y2,$tr = 100)
{
// Load image
$img = imagecreatefromjpeg($img_src);

// Transparent red
$red = imagecolorallocatealpha($img, 255, 0, 0, $tr);

// Draw a white rectangle
imagefilledrectangle($img, $x1, $y1, $x2, $y2, $red);


// Don't forget to output a correct header
header('Content-Type: image/jpg');

// Save the image (overwrite)
imagejpeg($img);
imagedestroy($img);
}
$img_src = 'test.jpg';
$x1= 500;
$y1= 450;
$x2 = 370;
$y2=180;
red_rectangle($img_src,$x1,$y1,$x2,$y2);
Sfax answered 25/1, 2013 at 12:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.