CakeFest 2025 Madrid: The Official CakePHP Conference

imagefilledrectangle

(PHP 4, PHP 5, PHP 7, PHP 8)

imagefilledrectangleDesenha um retângulo preenchido

Descrição

imagefilledrectangle(
    GdImage $image,
    int $x1,
    int $y1,
    int $x2,
    int $y2,
    int $color
): bool

Cria um retângulo preenchido com a cor informada no parâmetro color na imagem fornecida em image, iniciando no ponto 1 e terminando no ponto 2. 0, 0 é o canto superior esquerdo da imagem.

Parâmetros

image

Um objeto GdImage, retornado por uma das funções de criação de imagem, como imagecreatetruecolor().

x1

Coordenada x do ponto 1.

y1

Coordenada y do ponto 1.

x2

Coordenada x do ponto 2.

y2

Coordenada y do ponto 2.

color

A cor de preenchimento. Um identificador de cor criado com imagecolorallocate().

Valor Retornado

Retorna true em caso de sucesso ou false em caso de falha.

Registro de Alterações

Versão Descrição
8.0.0 O parâmetro image agora espera uma instância de GdImage; anteriormente, um resource gd válido era esperado.

Exemplos

Exemplo #1 Uso de imagefilledrectangle()

<?php
// Cria uma imagem 55x30
$im = imagecreatetruecolor(55, 30);
$white = imagecolorallocate($im, 255, 255, 255);

// Desenha um retângulo branco
imagefilledrectangle($im, 4, 4, 50, 25, $white);

// Grava a imagem
imagepng($im, './imagefilledrectangle.png');
?>

O exemplo acima produzirá algo semelhante a:

Saída do exemplo: imagefilledrectangle()

adicione uma nota

Notas Enviadas por Usuários (em inglês) 14 notes

up
8
michal dot kocarek at seznam dot cz
21 years ago
If you want to draw a rectangle with rounded corners, you can use this simple function...
Rectangle starts at x1y1 and ends at x2y2. $radius defines radius of circled corner.

<?

function ImageRectangleWithRoundedCorners(&$im, $x1, $y1, $x2, $y2, $radius, $color) {
// draw rectangle without corners
imagefilledrectangle($im, $x1+$radius, $y1, $x2-$radius, $y2, $color);
imagefilledrectangle($im, $x1, $y1+$radius, $x2, $y2-$radius, $color);
// draw circled corners
imagefilledellipse($im, $x1+$radius, $y1+$radius, $radius*2, $radius*2, $color);
imagefilledellipse($im, $x2-$radius, $y1+$radius, $radius*2, $radius*2, $color);
imagefilledellipse($im, $x1+$radius, $y2-$radius, $radius*2, $radius*2, $color);
imagefilledellipse($im, $x2-$radius, $y2-$radius, $radius*2, $radius*2, $color);
}

?>
up
4
olivier dot pons at google dot mail dot com
12 years ago
I wanted to clear an image, and set it to full transparent.
imagefilledrectangle() seems to ignore alpha channel and alpha blending.
Use imagefill() instead:

<?php
$w
= imagesx($final);
$h = imagesy($final);
$grande = imagecreatetruecolor($w, $h);

// Alpha blending on to use channel alpha
imagealphablending($grande, true);
// Allocate a transparent color and fill the new image with it.
// Without this the image will have a black background instead
// of being transparent.
$transparent = imagecolorallocatealpha($grande, 0, 0, 0, 127);
// transparent alpha will be _ignored_:
imagefilledrectangle($grande, 0, 0, $w, $h, $transparent);
// ok, transparent will be used and set whole alpha channel to transparent:
imagefill($grande, 0, 0, $transparent);
?>
up
3
administrador(ensaimada)sphoera(punt)com
19 years ago
I've made a function to make full color gradients:

<?php

// The image must be in truecolor mode!!
function gradient_region($img, $x, $y, $width, $height,$src_color, $dest_color=0){
$src_alpha = ($src_color) >> 24;
$src_red = ($src_color & 0xFF0000) >> 16;
$src_green = ($src_color & 0x00FF00) >> 8;
$src_blue = ($src_color & 0x0000FF);

$dest_alpha = ($dest_color) >> 24;
$dest_red = ($dest_color & 0xFF0000) >> 16;
$dest_green = ($dest_color & 0x00FF00) >> 8;
$dest_blue = ($dest_color & 0x0000FF);


$inc_alpha = ($dest_alpha - $src_alpha) / $width;
$inc_red = ($dest_red - $src_red)/$width;
$inc_green = ($dest_green - $src_green)/$width;
$inc_blue = ($dest_blue - $src_blue)/$width;

// If you need more performance, the step can be increased
for ($i=0;$i<$width;$i++){
$src_alpha += $inc_alpha;
$src_blue += $inc_blue;
$src_green += $inc_green;
$src_red += $inc_red;
imagefilledrectangle($img,
$x+$i,$y,
$x+$i,$y+$height,
imagecolorallocatealpha($img,
$src_red,$src_green,$src_blue,$src_alpha));
}
}
?>

More functions at http://www.sphoera.com
up
1
saramg at uclink dot berkeley dot edu
23 years ago
Important quirk to note:

While imagerectangle will allow you to use a different order of your coordinates (such as bottom-left to upper-right), imagefilledrectangle will only work correctly if you use top-left to bottom-right as indicated in the docs.
up
1
info at 555webdesign dot com
18 years ago
Thanks terereese. it took me over two hours to figure that one out.
it worked locally: imagefilledrectangle(imagresource, int x1, int x2, int y1, inty2, color)

BUT remote on my provider only this worked: imagefilledrectangle(imagresource, int x1, int y2, int x1, inty1, color)

Any ideas why and where?
up
0
Google
18 years ago
<?php
//index.php
//set your year, month, daym hour, minute, second you want to cuuntdown to.

//ONLY CHANGE FROM HERE
$year="2006";
$month="12";
$day="25";
$hour="00";
$minute="00";
$second="00";
$event="Christmas Day 2006";

$time=mktime($hour, $minute, $second, $month, $day, $year);

$timecurrent=date('U');
$cdtime=$time-$timecurrent;
$cdmonths=$cddays/30;
$cdyears=$cddays/365;

//Used this case only...
$cdminutes=round($cdtime/60);
//cdtime is seconds
$cdhours=round($cdtime/3600);
$cddays=round($cdhours/24);

//String the date
$currentdate = date('l, F j, Y');
// Set the content-type
header("Content-type: image/png");

// Create the image
$im = imagecreatetruecolor(701, 355);

//Temp BGCOLOR (center of c-finder)
$bg1 = 208;
$bg2 = 130;
$bg3 = 208;

$s1 = $bg1 - 20;
$s2 = $bg2 - 20;
$s3 = $bg3 - 20;

$t1 = $bg1 + 30;
$t2 = $bg2 + 30;
$t3 = $bg3 + 30;

$cArray=array();
$c1 = imagecolorallocate($im, $bg1, $bg2, $bg3); //Background
$c8 = imagecolorallocate($im, 255, $bg2, 255); //Background
$c2 = imagecolorallocate($im, $s1, $s2, $s3); //Shadow
$c3 = imagecolorallocate($im, $t1, $t2, $t3); //Text

imagefilledrectangle($im, 0, 0, 701, 50, $c1);

imagefilledrectangle($im, 0, 0, 701, 50, $c8);

// The text to draw
$text = $string;
// Replace path by your own font path
$fnum = rand(1, 9);
$font = "/f/font ($fnum)";

// Add some shadow to the text
imagettftext($im, 29, 1, 17, 42, $c2, $font, "Today is:");
imagettftext($im, 28, -1, 15, 40, $c3, $font, "Today is:");
imagettftext($im, 29, 1, 17, 92, $c2, $font, "...$currentdate...");
imagettftext($im, 28, -1, 15, 90, $c3, $font, "...$currentdate...");
imagettftext($im, 29, 1, 17, 142, $c2, $font, "So there are exactly:");
imagettftext($im, 28, -1, 15, 140, $c3, $font, "So there are exactly:");
imagettftext($im, 29, 1, 17, 192, $c2, $font, "$cddays with just...");
imagettftext($im, 28, -1, 15, 190, $c3, $font, "$cddays days with just...");
imagettftext($im, 29, 1, 17, 242, $c2, $font, "$cdminutes minutes and only...");
imagettftext($im, 28, -1, 15, 240, $c3, $font, "$cdminutes minutes and only...");
imagettftext($im, 29, 1, 17, 292, $c2, $font, "$cdseconds seconds until...");
imagettftext($im, 28, -1, 15, 290, $c3, $font, "$cdseconds seconds until...");
imagettftext($im, 29, 1