Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inage sizes on uploads

Status
Not open for further replies.

daybase

Technical User
Joined
Dec 13, 2002
Messages
115
Location
GB
Have successfully introduced an upload facility to a site but relying on clients to re-size images to something sensible looks like a mistake. Any ideas for checking size of image before upload or automatically re-sizing (and automatically creating thumbnails). Keep it simple with examples please - bit dim!
 
Thanks - will check it out in more detail but i thought this utility only really applied after upload
 
Using php you can check the size of an uploaded image before you process it. Then you can either reject it or continue working with it.

I have a script that checks allows users to upload a file. I check its size and if it is too big I resize it!

MrBelfry
 
Sounds just the thing - would it be cheeky to ask fora sneaky look?
 
Would be greatly appreciated - being self taught in everything I do I work so much better from examples. I never cease to be amazed by just how helpful people are. Thanks again.
 
You can output images directly to the browser but I prefer to store them on the server so that php doesn't have to do a load of extra work. Here is how you resize a jpg using GD

Code:
//first make a copy of the image you want to work on
//$file is the name of the image you have uploaded and want to resize; $img is the name of the new image
$img = imagecreatefromjpeg($file);

//then make some variables to tell php what size you want
$newwidth = 400;
$newheight =267 ;

//get the images height and width so that we can scale it properly
height = imagesy($img);
$width = imagesx($img);

//work out scale rate
$scale = min($newheight/$height, $newwidth/$width);

//if scale is less than 1 then the image needs to be reduced otherwise just move it. $destination is the filepath to where you want to store it on your server
if($scale>1){
   copy($file, $destination);
}else{
   //calculate new height and width using scale so that ratio is maintained
   $scaledheight = floor($scale*$height);
   $scaledwidth = floor($scale*$width);
		
   #Create a new temporary image of the new size
   $tmpimg = imagecreatetruecolor($scaledwidth, $scaledheight);

   #Copy and resize old image into new image
   imagecopyresampled($tmpimg, $img, 0, 0, 0, 0, $scaledwidth, $scaledheight, $width, $height);
   
   //free up some resources
   imagedestroy($img);
   
   //save jpeg. 95 refers to the compression level of the jpg but for some reason anything over 95 scrambles your image
   imagejpeg($tmpimg,$destination,95);
}

For maximum usability put that in a function!

You can also resize pngs by replacing all the function names that end in jpeg with png (ie imagejpeg becomes imagepng)

You cannot resize gifs because of the patent on the compression ratio.

Hope this is clear

MrBelfry
 
Absolutely brilliant - thank you so much.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top