Subject: Reading images straight from datastream
Posted By: shinmai Post Date: 12/16/2003 7:23:10 AM
How could I read an image from the datastream send by an "upload" script (one that sends a file to the server).
What I want to do, is read the picture from the variable that the script sends ($pict) and pass it into GD, to check its dimensions, and if the picture is too large (>80x80) it will not be saved and an errormessage is sent to the user.

I would appreciate any help concerning this. I really would not like to save the file into a temporary file, load it into gd, check the dimensions, and then if it is too large, delete the tempfile, becouse I'm almost 100% certain, there is a way to do this the way I'd like to do it. The reason I belive this, is that GDs own functions (createimagefromjpeg) almost certainly work in the same fashion (make filehandle => load file into variable => process it into the gd format => copy into the userdefined variable), and it would be stupid of the developers not to enable users to skip the first two phases.

Anyways, thank you in advance.
Reply By: nikolai Reply Date: 12/16/2003 11:38:56 AM
When a user uploads a file, you can access the file details (name, type, etc..., tmpfile name, etc..) in the $_FILES array.  Try accessing the file directly from there without copying the file out of the tmp upload dir.

Use print_r() to get a list of all the available indexes and values:

echo "<pre>\n";
print_r($_FILES);
echo "</pre>\n";


Take care,

Nik
http://www.bigaction.org/
Reply By: richard.york Reply Date: 12/16/2003 4:14:37 PM
The following is one method which will calculate the image's dimensions and resize them in ratio to 80px x 80px.


/*
 * bool image_resize_to_ratio(string location[, int max width[, int max height]])
 * Gathers image dimensions and calculates resized dimensions to aspect ratio
 * exports resized deminsions to the $GLOBALS namespace as:
 * $GLOBALS["resize_width"] and
 * $GLOBALS["resize_height"] respectively
*/

function image_resize_to_ratio($image_location, $maximum_width = 80, $maximum_height = 80)
{
    $size = @getimagesize($image_location);

    if (empty($size))
    {                    
        echo"Error: image location provided did not yield a valid image.<br />";
        return false;
    }

    $actual_width     = $size[0];
    $actual_height    = $size[1];

    $x_ratio = $maximum_width / $actual_width;
    $y_ratio = $maximum_height / $actual_height;

    if (($actual_width <= $maximum_width) && ($actual_height <= $maximum_height))
    {            
        $resize_width    = $actual_width;
        $resize_height    = $actual_height;    
    }

    else if (($x_ratio * $actual_height) < $maximum_height)
    {                
        $resize_height = ceil($x_ratio * $actual_height);
        $resize_width  = $maximum_width;
    }

    else
    {                
        $resize_width  = ceil($y_ratio * $actual_width);
        $resize_height = $maximum_height;                            
    }

    $GLOBALS["resize_width"]  = $resize_width;
    $GLOBALS["resize_height"] = $resize_height;

    return true;
}

/*
 * This function will check that an image has the exact dimensions provided in the $width and $height arguments. 
 *
 * bool is_image_dimensions(string location[, int width[, int height]])
 *
*/

function is_image_dimensions($image_location, $width = 80, $height = 80)
{
    $size = @getimagesize($image_location);

    if (empty($size))
    {                    
        echo"Error: image location provided did not yield a valid image.<br />";
        return false;
    }

    $actual_width     = $size[0];
    $actual_height    = $size[1];

    if ($actual_width != $width && $actual_height != $height)    
    
        return false;

    else

        return true;
}


And of course you would provide the $image_location  as $_FILES["datafieldname"]["tmp_name"] if using this during the upload process.

hth,
: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::

Reply By: shinmai Reply Date: 12/16/2003 4:18:44 PM
thanks for the help, I'll have to modify the source a little though, as I wan't users to be able to use smaller images too..

Anyway, thank you both.
Reply By: richard.york Reply Date: 12/16/2003 4:21:42 PM
Also the file stored in the temporary directory will automatically get toasted at the end of a script's execution, you can simply design your script to return an error to the user and not move the file if errors are encountered.  You may also delete it explicitly during the script's execution with a call to bool unlink(string filename).

http://www.php.net/unlink

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::


Go to topic 7607

Return to index page 985
Return to index page 984
Return to index page 983
Return to index page 982
Return to index page 981
Return to index page 980
Return to index page 979
Return to index page 978
Return to index page 977
Return to index page 976