imagecolortransparent
GETS or
SETS a transparent color in the given image.
TRANSPARENCY
is a global property of an image.
If a color is set to transparent, all regions of the image of the same color will have this property.
The identifier of the new, (or current, if none is specified), transparent color is returned.
If $color is not specified, and the image has no transparent color, the returned identifier will be -1.
<?php
int imagecolortransparent( GdImage $image, ?int $color = null )
where,
$image = The image identifier
$color = The color identifier
?>
$image
The image identifier.
$color
The color identifier created with imagecolorallocate.
EXERCISE
<?php
// RUN this code several times
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$img_z01 = "png/PNG 045 01.png";
// Plaza Zocovoder - Toledo - ESP
$xis_z01 = "png/PNG 045 01 TT.png";
$fin_z01 = "png/PNG 045 01 WTT.png";
$id_01 = imagecreatefrompng($img_z01);
$img_z01_w = imagesx($id_01);
$img_z01_h = imagesy($id_01);
$w_img_h = '( ' . $img_z01_w . 'px X ' . $img_z01_h . 'px )';
?>
<?php echo $img_z01; ?><br><br>
<?php echo $w_img_h; ?><br><br>
<img src="<?php echo $img_z01; ?>"
alt="<?php echo $img_z01; ?>"
title="<?php echo $img_z01; ?>" width="400">
<br><br><br>
<?php
$id_xis = imagecreatefrompng($xis_z01);
$xis_z01_w = imagesx($id_xis);
$xis_z01_h = imagesy($id_xis);
$w_xis_h = '( ' . $xis_z01_w . 'px X ' . $xis_z01_h . 'px )';
?>
<?php echo $xis_z01; ?><br><br>
<?php echo $w_xis_h; ?><br><br>
<img src="<?php echo $xis_z01; ?>"
alt="<?php echo $xis_z01; ?>"
title="<?php echo $xis_z01; ?>" width="400">
<br><br><br>
<?php
$b_color = imagecolorallocate($id_xis, 2, 2, 2);
$set_tr_color = imagecolortransparent($id_xis, $b_color);
$img_z01_x = $img_z01_w - $xis_z01_w;
$img_z01_y = $img_z01_h - $xis_z01_h;
$prc01 = mt_rand(70, 100);
imagecopymerge($id_01, $id_xis,
$img_z01_x, $img_z01_y,
0, 0,
$img_z01_w, $img_z01_h,
$prc01);
imagepng($id_01, $fin_z01);
echo $fin_z01 . '<br>Transparent: ' . $prc01 . '%'; ?><br><br>
<img src="<?php echo $fin_z01; ?>"
alt="<?php echo $fin_z01; ?>"
title="<?php echo $fin_z01; ?>" width="400">
RESULT PNG 045 01.png
( 600px X 397px )
PNG 045 01 TT.png ( 600px X 120px ) PNG 045 01 WTT.pngTransparent: 98%This is a particular result.
For each new run a new result is shown.
EXERCISE
<?php
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$img_imgt = "png/PNG 046 02 gdlogo.png";
$img_bl_rd = "png/PNG 046 02 gdlogo-s.png";
$finimgt = "png/PNG 046 02 gdlogo-c.png";
$id_imgt = imagecreatefrompng($img_imgt);
$img_imgt_w = imagesx($id_imgt);
$img_imgt_h = imagesy($id_imgt);
$w_img_h = '( ' . $img_imgt_w . 'px X ' . $img_imgt_h . 'px )';
echo $img_imgt; ?><br><br>
<?php echo $w_img_h; ?><br><br>
<img src="<?php echo $img_imgt; ?>"
alt="<?php echo $img_imgt; ?>"
title="<?php echo $img_imgt; ?>">
<br><br><br>
<?php
$id_azul = imagecreatefrompng($img_bl_rd);
$img_bl_rd_w = imagesx($id_azul);
$img_bl_rd_h = imagesy($id_azul);
$w_azul_h = '( ' . $img_bl_rd_w . 'px X ' . $img_bl_rd_h . 'px )';
echo $img_bl_rd; ?><br><br>
<?php echo $w_azul_h; ?><br><br>
<img src="<?php echo $img_bl_rd; ?>"
alt="<?php echo $img_bl_rd; ?>"
title="<?php echo $img_bl_rd; ?>">
<br><br><br>
<?php
$b_color = imagecolorallocate($id_azul, 255, 0, 0);
$set_tr_color = imagecolortransparent($id_azul, $b_color);
$img_imgt_x = $img_imgt_w - $img_bl_rd_w;
$img_imgt_y = $img_imgt_h - $img_bl_rd_h;
$prc = 100;
imagecopymerge($id_imgt, $id_azul,
$img_imgt_x, $img_imgt_y,
0, 0,
$img_imgt_w, $img_imgt_h,
$prc);
imagepng($id_imgt, $finimgt);
echo $finimgt . '<br><br>Transparent: ' . $prc . '%'; ?><br><br>
<img src="<?php echo $finimgt; ?>"
alt="<?php echo $finimgt; ?>"
title="<?php echo $finimgt; ?>">
RESULT PNG 046 02 gdlogo.png ( 256px X 256px )PNG 046 02 gdlogo-s.png( 256px X 256px )PNG 046 02 gdlogo-c.pngTransparent: 100%