search for in the  
<imagesetbrushimagesetstyle>
Last updated: Thu, 19 May 2005

imagesetpixel

(PHP 3, PHP 4, PHP 5)

imagesetpixel -- Set a single pixel

Description

int imagesetpixel ( resource image, int x, int y, int color )

imagesetpixel() draws a pixel at x, y (top left is 0, 0) in image image of color color.

See also imagecreate() and imagecolorallocate().



User Contributed Notes
imagesetpixel
odin<spam inside(C)>dtdm.org
05-Oct-2004 07:36
Just a simple implementation of the Bresenham algorythm (educational purpose....)

You can find more about this and many other tutorials for gfx there: http://brand107.home.comcast.net/pc-gpe/

<?php

/****************************************************
     Bresenham Line Algorythm PHP/GD implementation 
****************************************************/
function line($im,$x1,$y1,$x2,$y2,$color){
  
  
$deltax=abs($x2-$x1);
  
$deltay=abs($y2-$y1);

   if (
$deltax>$deltay) {
    
$numpixels=$deltax+1;
    
$d=(2*$deltay)-$deltax;
    
$dinc1=$deltay << 1; $dinc2=($deltay-$deltax) << 1;
    
$xinc1=1; $xinc2=1;
    
$yinc1=0; $yinc2=1;
   } else {
    
$numpixels=$deltay+1;
    
$d=(2*$deltax)-$deltay;
    
$dinc1=$deltax << 1; $dinc2=($deltax-$deltay)<<1;
    
$xinc1=0; $xinc2=1;
    
$yinc1=1; $yinc2=1;
   }

   if (
$x1>$x2) {
    
$xinc1=-$xinc1;
    
$xinc2=-$xinc2;
   }

   if (
$y1>$y2) {
    
$yinc1=-$yinc1;
    
$yinc2=-$yinc2;
   }
  
$x=$x1;
  
$y=$y1;

   for (
$i=0;$i<$numpixels;$i++) {
    
imagesetpixel($im,$x,$y,$color);
     if (
$d<0) {
    
$d+=$dinc1;
    
$x+=$xinc1;
    
$y+=$yinc1;
     } else {
    
$d+=$dinc2;
    
$x+=$xinc2;
    
$y+=$yinc2;
     }
   }
return ;
}

?>
richard at mf2fm dot co dot uk
24-Aug-2004 04:06
Here is a function that takes an image ($im) and returns it with the contrast maximised...

<?php
function contrast($im) {
  
$brightness=0;
  
$maxb=0;
  
$minb=255;
  
$imagesize=getimagesize($im);
  
$w=$imagesize[0];
  
$h=$imagesize[1];
   for (
$x=0; $x<$w; $x++) {
       for (
$y=0; $y<$h; $y++) {
          
$rgb=imagecolorat($im, $x, $y);
          
$rgb=imagecolorsforindex($im, $rgb);
          
$grey=0.2125*$rgb['red']+
              
0.7154*$rgb['green']+
              
0.0721*$rgb['blue'];
          
$brightness+=$grey;
           if (
$grey>$maxb) $maxb=$grey;
           if (
$grey<$minb) $minb=$grey;
       }
   }
  
$brightness=$brightness/($w*$h);
  
$minb=$brightness/($brightness-$minb);
  
$maxb=(255-$brightness)/($maxb-$brightness);
  
$contrast=min($minb, $maxb);
   for (
$x=0; $x<$w; $x++) {
       for (
$y=0; $y<$h; $y++) {
          
$rgb=imagecolorat($im, $x, $y);
          
$rgb=imagecolorsforindex($im, $rgb);
          
imagesetpixel($im, $x, $y,
              
65536*floor(min($rgb['red']*$contrast, 255))+
              
256*floor(min($rgb['green']*$contrast, 255))+
              
floor(min($rgb['blue']*$contrast, 255)));
       }
   }
   return (
$im);
}
?>

An example of usage might be:
<?php
$imagefile
="/path/filename";
$image=imagecreatefromjpeg($imagefile);
$image=contrast($image);
imagejpeg($image, $imagefile);
?>
chris at drunkenpirates dot co dot uk
13-Sep-2003 07:50
<?php

/*
An example combining the use of ImageColorAllocate, Imagesetpixel, Imagecopyresized and some basic Trig

By chris@drunkenpirates.co.uk

*/

Header("Content-type: image/png");

$height = 128;
$width = 128;

$imA = ImageCreate($width, $height);
$imB = ImageCreate($width*4, $height*4);
$bckA = ImageColorAllocate($imA, 0,0,0);
$bckB = ImageColorAllocate($imB, 0,0,0);

//GENERATE GRAY SCALE PALLETE

for($c=0;$c<256;$c++){
              
ImageColorAllocate($imA, $c, $c, $c);
               }

//PRODUCE DATA

$m=rand(0,10);
for(
$c=0;$c<128;$c++){
              
$s=  (sin( deg2rad($c*360*$m/128) )+1)*127;
              
$col_arr[$c]=$s;
               }
for(
$y=0;$y<$height;$y++){
       for(
$x=0;$x<$width;$x++){
              
$imgA[$x][$y]=$col_arr[$x];
               }
       }
for(
$y=0;$y<$height;$y++){
       for(
$x=0;$x<$width;$x++){
              
$imgB[$x][$y]=$col_arr[$y];
               }
       }

//SET PIXELS

for($y=0;$y<$height;$y++){
       for(
$x=0;$x<$width;$x++){
              
$imgC[$x][$y]=$imgA[$x][$y]+$imgB[$x][$y];
              
$s=$imgC[$x][$y]/2;
              
Imagesetpixel($imA,$x,$y,$s);
               }
       }

//RESIZE IMAGE FOR DISPLAY

Imagecopyresized ($imB, $imA, 0, 0, 0, 0, $width*4, $height*4, $width, $width);
ImagePNG($imB);
?>
dino at nordmark dot dk
31-May-2002 06:28
The example above diden't work, because of some errors.
This should work and it's also faster because there is only one 512*512 loop. (but it is still very slow)

<?
$filename
="lena.raw";
$width=512;
$height=512;
$fp=fopen($filename, "r");
$contents=fread($fp,filesize($filename));
fclose($fp);

$image=imagecreate($width,$height);

// create greyscale palette because the image is limited to 256 colors
for ($i=0;$i<256;$i++){ ImageColorAllocate($image,$i,$i,$i);}

// This is slow, but probably the only way
for ($i=0;$i<512;$i++){
   for (
$j=0;$j<512;$j++){   
  
imagesetpixel ($image,$i,$j,ord($contents[$i+$j*512]));
   }
}

imagepng($image,"result.png");
imagedestroy($image);

echo
"<img src=result.png></img>";
?>

--

Dino Patti

<imagesetbrushimagesetstyle>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT