Click to See Complete Forum and Search --> : Resize an image and save to server
firman
06-25-2007, 07:16 PM
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??
firman
06-26-2007, 03:55 PM
Any and all help would be huge.. Thank you in advance.
bokeh
06-26-2007, 04:21 PM
This is a regular question. Try doing a search.
firman
06-26-2007, 05:28 PM
I have tried this below....
$source = '../../images/photo_gallery/'.$_POST["folder"].'/zzsmall/'.$file_fixed_name;
$destination = null;
$w = 130;
$h = 135;
$quality = 100;
$details = @getimagesize($source) or die("I cannot open $source");
$type = preg_replace('@^.+(?<=/)(.+)$@', '$1', $details['mime']);
eval('$source = imagecreatefrom'.$type.'($source);');
#if($details[0] < $details[1])
#{
# $w = round(($h / $details[1]) * $details[0]);
#}
#else
#{
$h = round(($w / $details[0]) * $details[1]);
#}
if(imageistruecolor($source))
{
$slate = @imagecreatetruecolor($w, $h) or die('Invalid thumbnail dimmensions');
imageAlphaBlending($slate, false);
imageSaveAlpha($slate, true);
}
This is not changing the size and of the image? Any help people?
HazardTW
06-26-2007, 07:22 PM
Might start here: http://www.sitepoint.com/article/image-resizing-php
firman
06-26-2007, 07:34 PM
I got the script from another post on this site. I am sure I am just missing one lil piece?
bokeh
06-27-2007, 03:32 AM
Might start here: http://www.sitepoint.com/article/image-resizing-phpYou've missed a big bit. It looks like that is a mod for another piece of code.
HazardTW
06-27-2007, 04:12 AM
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 :)
//============ 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]);
}
}
//===================================================================
bluestars
06-27-2007, 07:35 PM
Why can't every browser be like IE? IE is the greatest!!!
Eww. :D 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.
HazardTW
06-27-2007, 11:51 PM
That statement in my sig is sarcasm ;)