Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Setting a max height in image resize script
Old 11-11-2007, 04:01 PM Setting a max height in image resize script
pealo86's Avatar
Super Spam Talker

Posts: 876
Name: Matt Pealing
Location: England, north west
Trades: 0
I have a script which uses the following code to resize images to a width of 350pixels, and keeps the height in proportion:

PHP Code:
list($width,$height)=getimagesize($uploadedfile);

$newwidth=350;
$newheight=($height/$width)*$newwidth
But also I need it to have a maximum height, say 300 pixels? But I cant really figure out the mathematical formula for it!

Im thinking something along the lines of:

PHP Code:
list($width,$height)=getimagesize($uploadedfile);

if(
height>300)
{
newwidth=($width/$height)*300;
}

else
{
$newwidth=350;
$newheight=($height/$width)*$newwidth;

__________________

Please login or register to view this content. Registration is FREE
pealo86 is offline
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
 
Register now for full access!
Old 11-11-2007, 08:50 PM Re: Setting a max height in image resize script
Defies a Status

Posts: 1,606
Trades: 0
All the ones I have played with use the larger dimesion (whether it be w or H) and resize the other on a % basis. That is the olny way to get an undistorted image.
__________________
Colbyt

Please login or register to view this content. Registration is FREE
colbyt is offline
Reply With Quote
View Public Profile
 
Old 11-12-2007, 04:51 AM Re: Setting a max height in image resize script
pealo86's Avatar
Super Spam Talker

Posts: 876
Name: Matt Pealing
Location: England, north west
Trades: 0
PHP Code:
$newheight=($height/$width)*$newwidth
is still a correct calculation to use though, it brings up the same results.
__________________

Please login or register to view this content. Registration is FREE
pealo86 is offline
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
Old 11-12-2007, 06:39 AM Re: Setting a max height in image resize script
shivaji's Avatar
Ultra Talker

Posts: 321
Trades: 0
I am sugest to you keep width and height in same dimension. Otherwise you will get disproportional resizing for higher images.

PHP Code:
$maxWidth_Height 350;
$sizes getimagesize($uploadedfile); 

// resize if is width bigger or same like height
if ($sizes[0] >= $sizes[1] && $sizes[0] > $maxWidth_Height) {
    
$resize $sizes[0]/$maxWidth_Height;
    
$width round($sizes[0]/$resize); 
    
$height round($sizes[1]/$resize); 

// resize if is height bigger then width
} elseif($sizes[0] < $sizes[1] && $sizes[1] > $maxWidth_Height) {
    
$resize $sizes[1]/$maxWidth_Height;              
    
$height round($sizes[1]/$resize); 
    
$width round($sizes[0]/$resize);

// don't resize if both dimension are smaller then $maxWidth_Height
} else {
    
$width $sizes[0];
    
$height $sizes[1]; 

I am using this script long time like part of my image gallery without any error.

Shivaji
__________________

Please login or register to view this content. Registration is FREE
- uncommon free scripts

Please login or register to view this content. Registration is FREE
- Städte, Sport, Party, Gourment, Apartments, Hotels
shivaji is offline
Reply With Quote
View Public Profile Visit shivaji's homepage!
 
Reply     « Reply to Setting a max height in image resize script
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.23461 seconds with 12 queries