Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 16th, 2003, 08:23 AM
Registered User
 
Join Date: Dec 2003
Location: Helsinki, , Finland.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Reading images straight from datastream

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 With Quote
  #2 (permalink)  
Old December 16th, 2003, 12:38 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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 With Quote
  #3 (permalink)  
Old December 16th, 2003, 05:14 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

The following is one method which will calculate the image's dimensions and resize them in ratio to 80px x 80px.

Code:
/*
 * 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 With Quote
  #4 (permalink)  
Old December 16th, 2003, 05:18 PM
Registered User
 
Join Date: Dec 2003
Location: Helsinki, , Finland.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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 With Quote
  #5 (permalink)  
Old December 16th, 2003, 05:21 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

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
:::::::::::::::::::::::::::::::::
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help before thye put me in straight jacket keithc BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 5 March 5th, 2006 08:10 AM
Reading images from Database shankhan Classic ASP Databases 1 March 21st, 2005 01:36 AM
Reading XML datastream realgone_ Classic ASP XML 5 September 22nd, 2004 01:17 PM
Random images straight from folder dsunmedia PHP How-To 1 July 21st, 2004 01:43 PM
Reading XML datastream returned from vendor marabunta Classic ASP XML 1 September 8th, 2003 09:23 AM



All times are GMT -4. The time now is 11:26 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.