I want to take a file that I upload and resize it so that it is always 130 pixels wide and have the height adjust to the proper height so that the image stays the same aspect…and then save the image to my server… How would you write this??
@HazardTWJun 27.2007 — #oops... I can't seem to find my way back to the place that really helped me, maybe it was http://us.php.net/
I am pretty much a rookie at PHP but I have actually just completed something like what you are looking for.
I have a site where a user can upload multiple image files at one time, I am using the image uploader scripts from these forums, they work great.
I am allowing only jpg and gif to be uploaded so those are the only two handled in my script, which I just added to the upload handler.
I created this from bits and pieces I located at different sites which the main one I unfortunately can't find a link to now to pass along so I will just show you what I came up with that works for me.
In this script I do not worry about aspect ratio of original image, I just force them all to a set size, in the context of its use it is all I need, you may want or need to scale your images with respect to ratio.
This is just the portion I added to my upload handler, when the script gets to this point some of the $uploadFilename's could = "NOIMAGE" because I changed the upload handler to change it instead of failing all the files, it just fails upload on the one's that were not proper.
Note: I am pretty new at PHP and not a trained programmer, so if anyone that is feels like elaborating on or correcting my attempts to explain the script, please do. I'll take any constructive advice as well since I am sure there is a better way to do it ?
[code=php]//============ SCALE ALL PICTURES DOWN TO 480X360 ================== foreach($active_keys as $key) { // filter failed uploads if ($uploadFilename[$key] != "NOIMAGE") { // make a name for new scaled img from original minus '_org_' $scaledImg = str_replace("_org_","",$uploadFilename[$key]); // remove any spaces $scaledImg = str_replace(" ","",$scaledImg); // get dimensions and img type list($width, $height, $tmpType) = getimagesize($uploadFilename[$key]); // get img mime type $imgType = image_type_to_mime_type($tmpType); // width of new img $newwidth = 480; // height of new img $newheight = 360; // create a new img object to hold resized copy $newImg = imagecreatetruecolor($newwidth, $newheight);
if ($imgType == "image/jpeg") { // create image object from jpeg source $source = imagecreatefromjpeg($uploadFilename[$key]); }
if ($imgType == "image/gif") { // create image object from gif source $source = imagecreatefromgif($uploadFilename[$key]); }
// copy from source image object to new image object using scaled down size imagecopyresized($newImg, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); // save newly copied image object as a jpeg file at 80% quality to filname specified by $scaledImg imagejpeg($newImg,$scaledImg,80); // remove image from memory after saving imagedestroy($newImg); // delete the original uploaded file pointed to by $uploadFilename[$key] from server unlink($uploadFilename[$key]);
@bluestarsJun 28.2007 — #Why can't every browser be like IE? IE is the greatest!!![/QUOTE] Eww. ? Nice script, though. I know a little PHP, but I'm dangerous when I code, because I always forget to force escapes and such, so I try not to write too much nowdays.