imagesetpixel 


gd apg

DRAWS a pixel at the specified coordinate in an image file.





This function returns TRUE on success or FALSE on failure.



<?php

bool imagesetpixel 
( GdImage $image, 
                                          
int $x, 
                                          
int $y, 
                                          
int $color )


where,

$image = The image identifier

$x 
= The x-coodinate of the point in the image file

$y 
= The y-coodinate of the point in the image file

$color 
= color identifier created with imagecolorallocate

?>

  $image   


The image identifier.



  $x   


The x-coordinate of the point in the image file.



  $y   


The y-coordinate of the point in the image file.



  $color   


A color identifier created with imagecolorallocate.



  1 EXERCISE   

<?php

echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';

$base_img = "png/PNG 050 01-a.png";
$dest_img = "png/PNG 050 01-b.png";

$id_base = imagecreate(640, 420);
$yellow = imagecolorallocate($id_base, 250, 127, 0);
imagepng($id_base, $base_img); 

echo 
basename($base_img); ?><br><br>
<img src="<?php echo $base_img; ?>"
alt="<?php echo $base_img; ?>"
title="<?php echo $base_img; ?>" width="400"><br><br>

<?php

$green 
= imagecolorallocate($id_base, 0, 120, 0);
$blue = imagecolorallocate($id_base, 0, 0, 120);

$base_w = imagesx($id_base);
$base_h = imagesy($id_base);

/* - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
   Draws a line with green pixels.
   - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- */

for ( $yg_pos = 1; $yg_pos < ($base_h - 1); $yg_pos++ )
{
$xg_pos = ($base_h - 1);
imagesetpixel($id_base, $xg_pos, $yg_pos, $green);
}

/* - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
   Draws a line with blue pixels
   - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- */
for( $xb_pos = 1; $xb_pos < ($base_w - 1); $xb_pos++ )
{
$yb_pos = $xb_pos;
imagesetpixel($id_base, $xb_pos, $yb_pos, $blue);
}

imagepng($id_base, $dest_img);

echo 
basename($dest_img); ?><br><br>
<img src="<?php echo $dest_img; ?>"
alt="<?php echo $dest_img; ?>"
title="<?php echo $dest_img; ?>" width="400"><br><br>


 RESULT   

PNG 050 01-a.png

PNG 050 01-a.png apr

PNG 050 01-b.png

PNG 050 01-b.png apr

  2 EXERCISE   

<?php

echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';

$base_img = "png/PNG 051 02-a.png";
$dest_img = "png/PNG 051 02-b.png";

$id_base = imagecreatetruecolor(640, 420);
$yellow = imagecolorallocate($id_base, 255, 255, 0);
imagepng($id_base, $base_img); 

echo 
$base_img; ?><br><br>
<img src="<?php echo $base_img; ?>"
alt="<?php echo $base_img; ?>"
title="<?php echo $base_img; ?>" width="400"><br><br>

<?php

$green 
= imagecolorallocate($id_base, 0, 255, 0);
$white = imagecolorallocate($id_base, 255, 255, 255);

$base_w = imagesx($id_base);
$base_h = imagesy($id_base);

/* - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
   Draws points with green pixels.
   - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- */

for ( $yg_pos = 1; $yg_pos < ($base_h - 1); $yg_pos++ )
{
$xg_pos = mt_rand(0, $base_h - 1);
imagesetpixel($id_base, $xg_pos, $yg_pos, $green);
}

/* - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
   Draws points with white pixels
   - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- */
for( $xb_pos = 1; $xb_pos < ($base_w - 1); $xb_pos++ )
{
$yb_pos = mt_rand(0, $xb_pos);
imagesetpixel($id_base, $xb_pos, $yb_pos, $white);
}

imagepng($id_base, $dest_img);

echo 
$dest_img; ?><br><br>
<img src="<?php echo $dest_img; ?>"
alt="<?php echo $dest_img; ?>"
title="<?php echo $dest_img; ?>" width="400"><br><br>


 RESULT   

PNG 051 02-a.png

PNG 051 02-a.png apr


PNG 051 02-b.png

PNG 051 02-b.png apr

Pay attention to the result of this exercise.

This is a particular result.

For each new run a new result will be obtained.

So, try it yourself.



  3 EXERCISE   

<?php

echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';

$base_img = "gif/GIF 022 03.gif";
$dest_img = "gif/GIF 022 03-spx.gif";

echo 
$base_img; ?><br><br>
<img src="<?php echo $base_img; ?>"
alt="<?php echo $base_img; ?>"
title="<?php echo $base_img; ?>" width="400"><br><br>

<?php
$id_base 
= imagecreatefromgif($base_img);

$green = imagecolorallocate($id_base, 0, 255, 0);
$blue = imagecolorallocate($id_base, 0, 0, 255);

$base_w = imagesx($id_base);
$base_h = imagesy($id_base);

/* - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
   Draws points with green pixels. 
   - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- */ 

for ( $yg_pos = 1; $yg_pos < ($base_h - 1); $yg_pos++ )
{
$xg_pos = mt_rand(200, $base_h - 1);
imagesetpixel($id_base, $xg_pos, $yg_pos, $green);
}

/* - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
   Draws points with blue pixels 
   - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- */ 
for( $xb_pos = 1; $xb_pos < ($base_w - 1); $xb_pos++ )
{
$yb_pos = mt_rand(0, $xb_pos + 20);
imagesetpixel($id_base, $xb_pos, $yb_pos, $blue);
}

imagegif($id_base, $dest_img);

echo 
$dest_img; ?><br><br>
<img src="<?php echo $dest_img; ?>"
alt="<?php echo $dest_img; ?>"
title="<?php echo $dest_img; ?>" width="400"><br><br>

 RESULT   

GIF 022 03.gif

GIF 022 03.gif apr

GIF 022 03-spx.gif

GIF 022 03-spx.gif apr

Pay attention to the result of this exercise.

This is a particular result.

For each new run a new result will be obtained.

So, try it yourself.