/    Sign up×
Community /Pin to ProfileBookmark

Resize an image and save to server

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??

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@firmanauthorJun 26.2007 — Any and all help would be huge.. Thank you in advance.
Copy linkTweet thisAlerts:
@bokehJun 26.2007 — This is a regular question. Try doing a search.
Copy linkTweet thisAlerts:
@firmanauthorJun 26.2007 — I have tried this below....

[code=php]
$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);
}

[/code]


This is not changing the size and of the image? Any help people?
Copy linkTweet thisAlerts:
@firmanauthorJun 27.2007 — I got the script from another post on this site. I am sure I am just missing one lil piece?
Copy linkTweet thisAlerts:
@bokehJun 27.2007 — Might start here: http://www.sitepoint.com/article/image-resizing-php[/QUOTE]You've missed a big bit. It looks like that is a mod for another piece of code.
Copy linkTweet thisAlerts:
@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]);

}
}
//===================================================================
[/code]
Copy linkTweet thisAlerts:
@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.
Copy linkTweet thisAlerts:
@HazardTWJun 28.2007 — That statement in my sig is sarcasm ?
×

Success!

Help @firman spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...