PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Thu Apr 19, 2018 10:21 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Image resize
PostPosted: Fri Jul 02, 2004 9:57 am 
Offline
Forum Commoner

Joined: Mon Jun 14, 2004 5:06 pm
Posts: 63
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.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 02, 2004 10:14 am 
Offline
Forum Newbie

Joined: Tue Dec 16, 2003 4:18 pm
Posts: 8
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.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 02, 2004 6:11 pm 
Offline
Forum Commoner

Joined: Mon Jun 14, 2004 5:06 pm
Posts: 63
Do you know where I could find an example of what your talking about I haven't read anything on ratio's yet.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 02, 2004 6:32 pm 
Offline
Neighborhood Spidermoddy
User avatar

Joined: Mon Mar 29, 2004 4:24 pm
Posts: 31559
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.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 13, 2004 3:52 am 
Offline
Forum Contributor

Joined: Sat Mar 27, 2004 6:54 am
Posts: 194
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>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 13, 2004 3:33 pm 
Offline
BeerMod
User avatar

Joined: Tue Jan 13, 2004 5:58 pm
Posts: 2170
Location: Jax FL & Spokane WA USA
litebearer wrote:
Syntax: [ Download ] [ Hide ]
if ($file != "." &amp;&amp; $file != ".." &amp;&amp; $file != ".DS_Store")



~litebearer what does the string '.DS_Store' represent?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 13, 2004 3:59 pm 
Offline
Neighborhood Spidermoddy
User avatar

Joined: Mon Mar 29, 2004 4:24 pm
Posts: 31559
Location: Bothell, Washington, USA
I seem to remember that being a Mac special folder..


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 13, 2004 4:53 pm 
Offline
DevNet Resident
User avatar

Joined: Thu Feb 12, 2004 8:19 pm
Posts: 1165
Location: ohio
yeah feyd it deals with macs.


yuck :roll:


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group