Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中绘制半透明矩形?_Php_Php Gd - Fatal编程技术网

如何在php中绘制半透明矩形?

如何在php中绘制半透明矩形?,php,php-gd,Php,Php Gd,下面是我想做的一个例子: 结果如下: 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

下面是我想做的一个例子:

结果如下:

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);
}
您需要使用,传递用创建的颜色


如您所见,的示例实际上是您想做什么。

到目前为止您有什么?此计划:我对函数的期望。@2astalavista:然后实现它。我想你误解了这个网站的用途。@TomalakGeret'kal你为什么这么认为?@2astalavista:对。这些是注释。注意:如果
imagealphablending()
设置为
false
,则alpha设置将无效。
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);