View Full Version : PHP imagecreate

08-28-2009, 05:15 PM
Hi, I'm looking to create a script which posts a random picture from an array.

Here's the coding:

header("Content-type: image/png");
$avatars[] = "sig/red.png";
$avatars[] = "sig/yellow.png";
$avatars[] = "sig/orange.png";
$avatars[] = "sig/green.png";
$avatars[] = "sig/blue.png";
$avatars[] = "sig/pink.png";

$avatar = $avatars[mt_rand(0, count($avatars)-1)];

$newavatar = imagecreatefrompng($avatar);


When I try it in a browser it doesn't work though. It says the image contains errors.

Any help would be appreciated, thanks!

08-28-2009, 05:27 PM
Found a script that actually works, nevermind :-)

08-28-2009, 07:19 PM
If its like you're OP, you don't need imagecreate at all.
Given that you have the file already, you can pass the necessary header for it ('image/png' in this example), and use a file_get_contents or fread and pump it to the browser as an image.
GD library has a lot of overhead, if you already have an image and don't need to perform any manipulations to it, keep it simple by just serving it.

08-28-2009, 08:31 PM
Thank you, I didn't know that :-)