Jump to content

Archived

This topic is now archived and is closed to further replies.

FunkyELF

random images using passthru

Recommended Posts

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?...

[code]passthru("cat " . $file );[/code]

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?

[code]<?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 );
?>

[/code]

Share this post


Link to post
Share on other sites
if get $_GET['size'] is a [b]integer[/b], then...

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


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

[code]$dirname = './images/';

$pictures = glob ( $dirname . '*.jpg' );[/code]


me!

Share this post


Link to post
Share on other sites
[quote author=printf link=topic=111702.msg452829#msg452829 date=1161034405]
if get $_GET['size'] is a [b]integer[/b], then...

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


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

[code]$dirname = './images/';

$pictures = glob ( $dirname . '*.jpg' );[/code]


me!
[/quote]

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?

Share this post


Link to post
Share on other sites
[quote author=printf link=topic=111702.msg452829#msg452829 date=1161034405]
Also [b]glob()[/b], would be much better than scandir() and that foreach() your using to build the jpg array!

[code]$dirname = './images/';

$pictures = glob ( $dirname . '*.jpg' );[/code]
[/quote]

Does glob only work on the current directory?

Share this post


Link to post
Share on other sites
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 [b]jpg[/b] in the directory [b]images[/b]

[code]$dirname = './images/';

$pictures = glob ( $dirname . '*.jpg' );[/code]

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

[code]$dirname = '';

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

// or written like so

$pictures = glob ( '*.jpg' );[/code]


me!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.