Jump to content


Photo

random images using passthru


  • Please log in to reply
4 replies to this topic

#1 FunkyELF

FunkyELF
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 16 October 2006 - 09:17 PM

I thought of an exercize for me to put some of my newly learned PHP to use.
I post on a couple of different forums and some allow for offsite avatars.  I thought I could create a PHP script to serve up a random avatar.

I think I got it working although I haven't tried it on any forums yet.

I was hopeing someone could look at this and critisize it.

What is the best way to just serve a file from PHP using passthru.
All the examples I saw were with programs that were converting things or resizing them but none just giving the image as is.
I figured I could use the cat command and it works but is there a better way than this?...

passthru("cat " . $file );

Thanks,
~Eric
I know I'm not sanitizing the $_GET['size'] variable and its going right into a command string...what is the best function to sanitize something like this?

<?php

    $dirname = dirname($_SERVER['SCRIPT_FILENAME']);
    
    // get the files in the same dir as this script.
    $files = scandir($dirname);
    
    // filter for only .jpg files
    foreach($files as $file){
        if(substr($file,-4) == ".jpg")
            $pictures[] = $file;
    }
    
    // pick the random file
    $rfile = $pictures[mt_rand(0,count($pictures) - 1)];
    
    header("Content-type: image/jpeg");
    
    // resize if we need to
    if(isset($_GET['size']))
        passthru("convert -resize {$_GET['size']}x{$_GET['size']} $rfile jpg:-");
    else
        passthru("cat " . $rfile );
?>



#2 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 16 October 2006 - 09:33 PM

if get $_GET['size'] is a integer, then...

$size = intval ( $_GET['size'] );


Also glob(), would be much better than scandir() and that foreach() your using to build the jpg array!

$dirname = './images/';

$pictures = glob ( $dirname . '*.jpg' );


me!

#3 FunkyELF

FunkyELF
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 16 October 2006 - 10:03 PM

if get $_GET['size'] is a integer, then...

$size = intval ( $_GET['size'] );


Also glob(), would be much better than scandir() and that foreach() your using to build the jpg array!

$dirname = './images/';

$pictures = glob ( $dirname . '*.jpg' );


me!


Thanks, the fact that size should only be an int never occured to me.
As far as that glob goes, I don't think my book went into it.  Its things like that where I'll just wind up reinventing the wheel.

Anyway, how about that cat command?...is that the right way to go about things using passthru?

#4 FunkyELF

FunkyELF
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 18 October 2006 - 03:13 PM

Also glob(), would be much better than scandir() and that foreach() your using to build the jpg array!

$dirname = './images/';

$pictures = glob ( $dirname . '*.jpg' );


Does glob only work on the current directory?

#5 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 18 October 2006 - 03:23 PM

glob() works on the directory you give it, such as in my example './images/', so glob will create an array of documents found in that directory! if you don't give it a path './images/', then it will look in the current directory the script is running from!

// get the files that are of the type jpg in the directory images

$dirname = './images/';

$pictures = glob ( $dirname . '*.jpg' );

// get the files that are of the type jpg in the current directory, that the script was called from

$dirname = '';

$pictures = glob ( $dirname . '*.jpg' );

// or written like so

$pictures = glob ( '*.jpg' );


me!





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users