A Little bit of PHP help needed.

Sheepoholics

New Member
Messages
266
Reaction score
0
Points
0
move to web development please



Alright so as a little learning experimant of mine. I'm trying to make an image generator. Based upon Chuck Norris facts.

So basically this is what I did. I made one hundred Chuck Norris fact images.
like so:
Chuck%20noris%20Fact%201.gif

and put them in a folder called avatars.

and then I made a php script that looked like this.

PHP:
<?php
header("Content-type: image/gif");

$avatars[] = "avatars/Chuck noris Fact 1.gif"
$avatars[] = "avatars/Chuck noris Fact 2.gif"
$avatars[] = "avatars/Chuck noris Fact 3.gif"
$avatars[] = "avatars/Chuck noris Fact 4.gif"
$avatars[] = "avatars/Chuck noris Fact 5.gif"
$avatars[] = "avatars/Chuck noris Fact 6.gif"
$avatars[] = "avatars/Chuck noris Fact 7.gif"
$avatars[] = "avatars/Chuck noris Fact 8.gif"
$avatars[] = "avatars/Chuck noris Fact 9.gif"
$avatars[] = "avatars/Chuck noris Fact 10.gif"
$avatars[] = "avatars/Chuck noris Fact 11.gif"
$avatars[] = "avatars/Chuck noris Fact 12.gif"
$avatars[] = "avatars/Chuck noris Fact 13.gif"
$avatars[] = "avatars/Chuck noris Fact 14.gif"
$avatars[] = "avatars/Chuck noris Fact 15.gif"
$avatars[] = "avatars/Chuck noris Fact 16.gif"
$avatars[] = "avatars/Chuck noris Fact 17.gif"
$avatars[] = "avatars/Chuck noris Fact 18.gif"...

...$avatars[] = "avatars/Chuck noris Fact 95.gif"
$avatars[] = "avatars/Chuck noris Fact 96.gif"
$avatars[] = "avatars/Chuck noris Fact 97.gif"
$avatars[] = "avatars/Chuck noris Fact 98.gif"
$avatars[] = "avatars/Chuck noris Fact 99.gif"
$avatars[] = "avatars/Chuck noris Fact 100.gif";

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

$newavatar = imagecreatefromgif($avatar);
imagegif($newavatar);
imagedestroy($newavatar);

?>

and then I made this .htaccess file

Code:
RewriteEngine On
RewriteRule avatar.gif avatar.php

but my problem is that it dosent work.

Any help would be greatly appreciated.

I get this

Parse error: parse error, unexpected T_VARIABLE in /home/x10temp/public_html/images/avatar.php on line 5
 
Last edited:

Bryon

I Fix Things
Messages
8,149
Reaction score
101
Points
48
You need a ";" after each line/statement in a PHP script, to tell the PHP parser that the line/statment has ended.

I have a much more simple solution for you. :)

PHP:
<?php
header("Content-type: image/gif");

// Path to the directory. :-)
$path = './avatars';

$files = array();
if ($handle = opendir($path)) {
   while (false !== ($file = readdir($handle))) {
      if ($file != "." && $file != "..") {
         $files[] = $file;
      }
   }
   closedir($handle);
}

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

$newavatar = imagecreatefromgif($avatar);
imagegif($newavatar);
imagedestroy($newavatar);

?>


I don't have time to test it right now, but I looked it over to see if it would work. Basically I got rid of the 100 or so lines you would need to add each image individually to the array. I made a small loop to automatically take all images from the directory ($path) and place each into the array on it's own.
 
B

Brandon

Guest
Darn, NedreN replyed first. Also NedreN's script is much easier cause you don't have to type each one in. But do not place that in the avatars directory and have it look for avatars in the directory that the scripts in because it will loop forever.

Here is a different way
PHP:
<?php

	$folder = '.';  //folder where images are held

    $extList = array();        //You can add more image types
	$extList['gif'] = 'image/gif';
	$extList['jpg'] = 'image/jpeg';
	$extList['jpeg'] = 'image/jpeg';
	$extList['png'] = 'image/png';
	

$img = null;

if (substr($folder,-1) != '/') {
	$folder = $folder.'/';
}

if (isset($_GET['img'])) {
	$imageInfo = pathinfo($_GET['img']);
	if (
	    isset( $extList[ strtolower( $imageInfo['extension'] ) ] ) &&
        file_exists( $folder.$imageInfo['basename'] )
    ) {
		$img = $folder.$imageInfo['basename'];
	}
} else {
	$fileList = array();
	$handle = opendir($folder);
	while ( false !== ( $file = readdir($handle) ) ) {
		$file_info = pathinfo($file);
		if (
		    isset( $extList[ strtolower( $file_info['extension'] ) ] )
		) {
			$fileList[] = $file;
		}
	}
	closedir($handle);

	if (count($fileList) > 0) {
		$imageNumber = time() % count($fileList);
		$img = $folder.$fileList[$imageNumber];
	}
}

if ($img!=null) {
	$imageInfo = pathinfo($img);
	$contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ];
	header ($contentType);
	readfile($img);
} else {
	if ( function_exists('imagecreate') ) {
		header ("Content-type: image/png");
		$im = @imagecreate (100, 100)
		    or die ("Cannot initialize new GD image stream");
		$background_color = imagecolorallocate ($im, 255, 255, 255);
		$text_color = imagecolorallocate ($im, 0,0,0);
		imagestring ($im, 2, 5, 5,  "IMAGE ERROR", $text_color);
		imagepng ($im);
		imagedestroy($im);
	}
}

?>
 

Sheepoholics

New Member
Messages
266
Reaction score
0
Points
0
for some reason the file won't read the folder which is no big deal I just moved the php and .htaccess file to the folder with pictures and that solved it. :)

avatar.php

Refresh
 
Last edited:
B

Brandon

Guest
It is saved as a .php Not all forums support .php graphics.
 

Bryon

I Fix Things
Messages
8,149
Reaction score
101
Points
48
Umm, might not have worked because of the script's/directories permissions. If it's working though then, oh well. :)
 
Top