 |
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

December 16th, 2003, 08:23 AM
|
|
Registered User
|
|
Join Date: Dec 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|

December 16th, 2003, 12:38 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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/
|
|

December 16th, 2003, 05:14 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
|
|
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
:::::::::::::::::::::::::::::::::
|
|

December 16th, 2003, 05:18 PM
|
|
Registered User
|
|
Join Date: Dec 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|

December 16th, 2003, 05:21 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
|
|
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
:::::::::::::::::::::::::::::::::
|
|
 |