how to draw semi-transparent rectangle in php?

user669677 picture user669677 · Dec 9, 2011 · Viewed 12.7k times · Source

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);
}

Answer

lorenzo-s picture lorenzo-s · Dec 9, 2011

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.