Zabs
Zabs

Reputation: 14142

Adding a single pixel to an image using PHP

I am using Codeigniter 2.1.3 and would like to add a single pixel to an image - I know GD or ImageMagick should be able to do this however I find them quite 'bloated' libraries. Does anyone know of relatively simple method with Codeigniter or even another PHP OOP library that would be easily to integrate?

Upvotes: 0

Views: 1217

Answers (1)

d.danailov
d.danailov

Reputation: 9800

http://php.net/manual/en/function.imagesetpixel.php

imagesetpixel()

<?php

$x = 200;
$y = 200;

$gd = imagecreatetruecolor($x, $y);

$corners[0] = array('x' => 100, 'y' =>  10);
$corners[1] = array('x' =>   0, 'y' => 190);
$corners[2] = array('x' => 200, 'y' => 190);

$red = imagecolorallocate($gd, 255, 0, 0); 

for ($i = 0; $i < 100000; $i++) {
  imagesetpixel($gd, round($x),round($y), $red);
  $a = rand(0, 2);
  $x = ($x + $corners[$a]['x']) / 2;
  $y = ($y + $corners[$a]['y']) / 2;
}

header('Content-Type: image/png');
imagepng($gd);

?>

Upvotes: 2

Related Questions