Image resize

Need help with Photoshop, the GIMP, Illustrator, or others? Want to show off your work? Looking for advice on your newest Flash stuff?

Moderator: General Moderators

Post Reply
Cateyes
Forum Commoner
Posts: 63
Joined: Mon Jun 14, 2004 5:06 pm

Image resize

Post by Cateyes »

When an image is called from a DB is it possible to specify a height but have the width stay proportional to what height you specify. If not I will just specify both and hope it does't get distorted to much.
HappyTchoum
Forum Newbie
Posts: 8
Joined: Tue Dec 16, 2003 3:18 pm

Post by HappyTchoum »

Make a ratio in your php script. Keep a fix height or weight and then calculate the other parameter with the ratio of the image.
Cateyes
Forum Commoner
Posts: 63
Joined: Mon Jun 14, 2004 5:06 pm

Post by Cateyes »

Do you know where I could find an example of what your talking about I haven't read anything on ratio's yet.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'd think it's easiest to store the default width and height inside the database, then use [php_man]imagecreatefromstring[/php_man] to get the image into memory, divide width by height, then multiply this result by the new height you want. Rounding that result will give you the new width. Use [php_man]imagecreatetruecolor[/php_man] or whatever to create a new image based on the desired height and new width. Then use [php_man]imagecopyresampled[/php_man] to scale the original image to the new size.
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Post by litebearer »

I use the following script to resize images my family uploads to our family gallery. Perhaps you can glean some instight to resizing proportionately

Code: Select all

<?PHP

###########################################
#
# script to take all the images in a folder
# and resample them.
# ---------------------------
#
#  it will loop through all the images in a
#  folder and check to see if width is > 800
#  and/or height > 600 . If the image meets
# either of those criteria, the script will
# resample the image to fall within the 
# guidelines AND will remake the corresponding
# thumbnail.
#
############################################
if ($handle = opendir('pics'))
{
while (false !== ($file = readdir($handle)))
{ 
if ($file != "." && $file != ".." && $file != ".DS_Store")
{ 
$off_site = 'pics/' . $file;

$src = imagecreatefromjpeg($off_site); 
$width = imagesx($src); 
$height = imagesy($src); 

// check that width is 800 or less
if ($width > 800)
{
$ratio_w = 800/$width;
} else {
$ratio_w = 1;
}

// check if height is 600 or less
if ($height > 600)
{
$ratio_h = 600/$height;
} else {
$ratio_h = 1;
}

// determine which ratio creates the smaller image
if ($ratio_w <= $ratio_h)
{
$aspect_ratio = $ratio_w;
} else {
$aspect_ratio = $ratio_h;
}

// new resizing code
$new_w = abs($width * $aspect_ratio);
$new_h = abs($height * $aspect_ratio);
$size = $new_h;

$aspect_ratio = $height/$width; 

//start resizing 
if ($height <= $size) { 
$new_w = $width; 
$new_h = $height; 
} else { 
$new_h = $size; 
$new_w = abs($new_h / $aspect_ratio); 
} 


$img = imagecreatetruecolor ($new_w,$new_h); 

//save image 
imagecopyresampled ($img,$src,0,0,0,0,$new_w,$new_h,$width,$height); 

imagejpeg($img, $off_site, 90); 
imagedestroy($img); 

} 
}
closedir($handle); 
}

feyd | switched to

Code: Select all

tags[/color]
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

litebearer wrote:

Code: Select all

if ($file != "." && $file != ".." && $file != ".DS_Store")
~litebearer what does the string '.DS_Store' represent?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I seem to remember that being a Mac special folder..
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

yeah feyd it deals with macs.


yuck :roll:
Post Reply