|
Subject:
|
Image Resize after Upload
|
|
Posted By:
|
echovue
|
Post Date:
|
7/10/2006 1:36:54 AM
|
Hi everyone,
Here is the challenge I am facing. I have a page that uploads two graphic files to the server. Currently I can get the dimensions of the images, and warn the user is they exceed the size limits (350x350). I think that instead of a warning, I could just resize the images to the correct size, and the user will never know.
I am sure I am close, but would someone be able to direct as to the best way to do this?
I figure I can either resize after the upload process, or once the upload is complete and the images have been removed to the specific folders.
Thanks in advance,
Mike
Mike EchoVue.com
|
|
Reply By:
|
panacea
|
Reply Date:
|
7/12/2006 6:19:03 AM
|
I use the GD library to resample my digital camera pictures for thumbnails, medium view, and large view JPGs for hosting on my website. It takes a bit of work to get set up, but once you do, it works great. You can specify the JPEG quality level and even do graphic manipulation.
You can get the installation instructions and learn more here: http://www.php.net/manual/en/ref.image.php
Jon Emerson http://www.jonemerson.net/
|
|
Reply By:
|
echovue
|
Reply Date:
|
7/13/2006 12:16:39 AM
|
I actually figured this one out. The trick was getting the image and then getting it saved back over itself once it was resaved.
I got this code from a comment posted on the PHP.net site, and tweaked it to get the desired functionality.
function ResizeImage($file,$maxwdt,$maxhgt, $dest) {
list($owdt,$ohgt,$otype)=@getimagesize($file);
switch($otype) {
case 1: $newimg=imagecreatefromgif($file); break;
case 2: $newimg=imagecreatefromjpeg($file); break;
case 3: $newimg=imagecreatefrompng($file); break;
default: echo "Unkown filetype (file $file, typ $otype)"; return;
}
if($newimg) {
if($owdt>1500 || $ohgt>1200)
list($owdt, $ohgt) = Resample($newimg, $owdt, $ohgt, 1024,768,0);
Resample($newimg, $owdt, $ohgt, $maxwdt, $maxhgt);
if(!$dest) return $newimg;
if(!is_dir(dirname($dest)))
mkdir(dirname($dest));
switch($otype) {
case 1: imagegif($newimg,dest); break;
case 2: imagejpeg($newimg,$dest,90); break;
case 3: imagepng($newimg,$dest); break;
}
imagedestroy($newimg);
chmod($dest,0644);
}
}
function Resample(&$img, $owdt, $ohgt, $maxwdt, $maxhgt, $quality=1) {
if(!$maxwdt) $divwdt=0;
else $divwdt=Max(1,$owdt/$maxwdt);
if(!$maxhgt) $divhgt=0;
else $divhgt=Max(1,$ohgt/$maxhgt);
if($divwdt>=$divhgt) {
$newwdt=$maxwdt;
$newhgt=round($ohgt/$divwdt);
} else {
$newhgt=$maxhgt;
$newwdt=round($owdt/$divhgt);
}
$tn=imagecreatetruecolor($newwdt,$newhgt);
if($quality)
imagecopyresampled($tn,$img,0,0,0,0,$newwdt,$newhgt,$owdt,$ohgt);
else
imagecopyresized($tn,$img,0,0,0,0,$newwdt,$newhgt,$owdt,$ohgt);
imagedestroy($img);
$img = $tn;
return array($newwdt, $newhgt);
}
To use it, I just use the following line...
ResizeImage($imageName,350,350, $imageName);
Which would resize and resample so that the longest side is set at 350 pixels.
Mike EchoVue.com
|