PHP Developers Network
http://forums.devnetwork.net/

Image resize
http://forums.devnetwork.net/viewtopic.php?f=32&t=23385
Page 1 of 1

Author:  Cateyes [ Fri Jul 02, 2004 9:57 am ]
Post subject:  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.

Author:  HappyTchoum [ Fri Jul 02, 2004 10:14 am ]
Post subject: 


Author:  Cateyes [ Fri Jul 02, 2004 6:11 pm ]
Post subject: 

Do you know where I could find an example of what your talking about I haven't read anything on ratio's yet.

Author:  feyd [ Fri Jul 02, 2004 6:32 pm ]
Post subject: 

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.

Author:  litebearer [ Tue Jul 13, 2004 3:52 am ]
Post subject: 

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

Syntax: [ Download ] [ Hide ]
<?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
Syntax: [ Download ] [ Hide ]
tags</span>

Author:  hawleyjr [ Tue Jul 13, 2004 3:33 pm ]
Post subject: 


Author:  feyd [ Tue Jul 13, 2004 3:59 pm ]
Post subject: 

I seem to remember that being a Mac special folder..

Author:  tim [ Tue Jul 13, 2004 4:53 pm ]
Post subject: 

yeah feyd it deals with macs.


yuck :roll:

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/