Image resize
Moderator: General Moderators
Image resize
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.
-
- Forum Newbie
- Posts: 8
- Joined: Tue Dec 16, 2003 3:18 pm
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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.
-
- Forum Contributor
- Posts: 194
- Joined: Sat Mar 27, 2004 5:54 am
I use the following script to resize images my family uploads to our family gallery. Perhaps you can glean some instight to resizing proportionately
feyd | switched to
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]
~litebearer what does the string '.DS_Store' represent?litebearer wrote:Code: Select all
if ($file != "." && $file != ".." && $file != ".DS_Store")