Page 1 of 1

Image resize

Posted: Fri Jul 02, 2004 9:57 am
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.

Posted: Fri Jul 02, 2004 10:14 am
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.

Posted: Fri Jul 02, 2004 6:11 pm
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.

Posted: Fri Jul 02, 2004 6:32 pm
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.

Posted: Tue Jul 13, 2004 3:52 am
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]

Posted: Tue Jul 13, 2004 3:33 pm
by hawleyjr
litebearer wrote:

Code: Select all

if ($file != "." && $file != ".." && $file != ".DS_Store")
~litebearer what does the string '.DS_Store' represent?

Posted: Tue Jul 13, 2004 3:59 pm
by feyd
I seem to remember that being a Mac special folder..

Posted: Tue Jul 13, 2004 4:53 pm
by tim
yeah feyd it deals with macs.


yuck :roll: