+ Reply to Thread
Results 1 to 6 of 6

Thread: A Little bit of PHP help needed.

  1. #1
    Sheepoholics's Avatar
    Sheepoholics is offline x10 Sophmore Sheepoholics is an unknown quantity at this point
    Join Date
    Dec 2005
    Location
    Beaumont AB
    Posts
    133

    A Little bit of PHP help needed.

    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:
    and put them in a folder called avatars.

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

    PHP Code:
    <?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(0count($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 by Sheepoholics; 03-07-2006 at 10:01 PM.

  2. #2
    Bryon is offline Administrator Bryon has disabled reputation
    Join Date
    Apr 2005
    Location
    Northfield, NH
    Posts
    7,608

    Re: A Little bit of PHP help needed.

    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 Code:

    <?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(0count($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.

  3. #3
    Brandon Guest

    Re: A Little bit of PHP help needed.

    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 Code:
    <?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( 
    $extListstrtolower$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( 
    $extListstrtolower$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 (100100)
                or die (
    "Cannot initialize new GD image stream");
            
    $background_color imagecolorallocate ($im255255255);
            
    $text_color imagecolorallocate ($im0,0,0);
            
    imagestring ($im255,  "IMAGE ERROR"$text_color);
            
    imagepng ($im);
            
    imagedestroy($im);
        }
    }

    ?>

  4. #4
    Sheepoholics's Avatar
    Sheepoholics is offline x10 Sophmore Sheepoholics is an unknown quantity at this point
    Join Date
    Dec 2005
    Location
    Beaumont AB
    Posts
    133

    Re: A Little bit of PHP help needed.

    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.


    Refresh
    Last edited by Sheepoholics; 03-08-2006 at 10:23 PM.

  5. #5
    Brandon Guest

    Re: A Little bit of PHP help needed.

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

  6. #6
    Bryon is offline Administrator Bryon has disabled reputation
    Join Date
    Apr 2005
    Location
    Northfield, NH
    Posts
    7,608

    Re: A Little bit of PHP help needed.

    Umm, might not have worked because of the script's/directories permissions. If it's working though then, oh well. :-)

+ Reply to Thread

Similar Threads

  1. [PHP] Variables in PHP
    By Bryon in forum Tutorials
    Replies: 15
    Last Post: 01-29-2009, 09:46 AM
  2. tons of PHP Resources
    By Chris S in forum Scripts & 3rd Party Apps
    Replies: 10
    Last Post: 01-16-2009, 10:07 AM
  3. [PHP] PHP For Starters
    By Complex in forum Tutorials
    Replies: 24
    Last Post: 06-14-2008, 11:40 PM
  4. Unstand PHP?
    By o0slowpaul0o in forum Tutorials
    Replies: 8
    Last Post: 01-07-2008, 09:16 PM
  5. Help with PHP is needed...
    By dragoneye_xp in forum Scripts & 3rd Party Apps
    Replies: 3
    Last Post: 06-17-2006, 10:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers