/    Sign up×
Community /Pin to ProfileBookmark

resizing (uploaded) images

Okay, I have my image script that’s finally working. Now I’d like to resize the uploaded image (permanatly). I’d assume this would be done during the uploading process.

Basically, I’d give preset porportions. The uploaded image would then be resized to the correct size I specified, then saved to the server.

How should I go about doing this?

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@bokehSep 26.2006 — Here's my [I]de facto[/I] resizing function. It covers most needs. Resizes most image types and maintains transparency and alpha if the image has them.[code=php]function resize($source, $destination = null, $w = 125, $h = 125, $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);
}
else
{
$slate = @imagecreate($w, $h) or die('Invalid thumbnail dimmensions');
if(false !== ($trans = @imagecolorsforindex($source, imagecolortransparent($source))))
{
$trans = ImageColorAllocate($slate, $trans['red'], $trans['green'], $trans['blue']);
imagefilledrectangle($slate, 0, 0, $w - 1, $h - 1, $trans);
imagecolortransparent($slate, $trans);
}
}
imagecopyresampled($slate, $source, 0, 0, 0, 0, $w, $h, $details[0], $details[1]);
$destination or header('Content-Type: '.$details['mime']);
eval('@image'.$type.'($slate'.(($type=='jpeg')?',$destination,$quality':($destination?',$destination ':'')).');');
imagedestroy($source);
imagedestroy($slate);
$destination or die;
}[/code]
If you have trouble integrating this you will need to post your code but before you do, check out the code in [URL=http://www.webdeveloper.com/forum/showthread.php?t=115351&page=1]this thread[/URL].
×

Success!

Help @Bleed 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.2,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...