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

odindtdm.org

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){

In reply to weitheism at gmail.com:
You should probably have used ImageAlphaBlending($image, false); in your early attempts. This way any paint/fill operation replaces the alpha value of the destination.

weitheism

I was looking for a way to actually DELETE pixels or squares or parts of an image from an image resource, and at first I thought imagesetpixel would do the trick. Unfortunately, it merely paints over that one pixel, and as far as I knew, php didn't have any native way of deleting sections out of your image - so this little method should take care of deleting rectangular parts of your pictures!
function deleteRectangle(&$oldImage,$leftX,$leftY,$rightX,$rightY)
{
// Since php has no native way of delete parts of images
// We have to divide the image into four different parts and then copy them manually to a new
// image

// Copies each of the four imagesections into their respective old positions
for($i = 0;$i<count($imagesection);$i++)
imagecopyresampled($newImage,$oldImage, $imagesection[$i][0],$imagesection[$i][1], $imagesection[$i][0],$imagesection[$i][1], $imagesection[$i][2]-$imagesection[$i][0], $imagesection[$i][3]-$imagesection[$i][1], $imagesection[$i][2]-$imagesection[$i][0], $imagesection[$i][3]-$imagesection[$i][1]);

// Alternately you can cycle through each pixel in an image and see if that pixel is an the area
// but that could be more intensive
imagedestroy($oldImage);

// Sets the old image equal to the new, cleared image
$oldImage = $newImage;
}
It was made with a transparent background in mind, but you could easily change that by changeing imagecreatetruecolor to imagecreate and deleting the code that deals with transparency. Hope it helps!

brian vaughn php

I looked, but was unable to find any example code to watermark an image with a watermark that contained alpha transparency. So the following class does just that. As a parameter, it takes 2 image objects: the main image, and the watermark image (which can be a gif, png, whatever) - and optionally, an alpha setting (0-100% alpha for the watermark image). It then creates and returns a new image with the alpha-transparent watermark imposed, center-aligned, over the larger image.
<?php
class watermark{
function create_watermark( $dst_img, $watermark_img, $alpha = 100 ) {
$alpha /= 100; # convert 0-100% user-friendly alpha to decimal
# calculate our images dimensions
$dst_img_w = imagesx( $dst_img );
$dst_img_h = imagesy( $dst_img );
$watermark_img_w = imagesx( $watermark_img );
$watermark_img_h = imagesy( $watermark_img );

Here's my stab at the imagecreatefromtga function. I used code from send at mail dot 2aj dot net and others below as a basis, and added support for targa 16, targa 24 and targa 32. However, I only support uncompressed RBG data type as that's the only one I need. (I removed the return_array feature since you can simply use imagesx() and imagesy() to get the image size).
Please note that I have not tested this with a targa 16 since I don't have one handy at the moment.
<?php
function imagecreatefromtga( $filename )
{
$handle = fopen( $filename, 'rb' );
$data = fread( $handle, filesize( $filename ) );
fclose( $handle );