Jump to content

[SOLVED] if($ratio1>$ratio2, how do you know?


student101

Recommended Posts

I hope this is the right section to post this topic /question?

I have an image upload and resize function...

I would like to know the proper ratio information to help resize the image and keep the proportions or aspect ratio.

 

How does this

 work or How should it work? 
[code]$old_x=imagesx($src_img); //width
$old_y=imagesy($src_img); //height
$ratio1=$old_x/$new_h;
$ratio2=$old_y/$new_w;
if($ratio1>$ratio2){
$thumb_w=$new_w;
$thumb_h=$old_y/$ratio1;
}else{
$thumb_h=$new_h;
$thumb_w=$old_x/$ratio2;
}

Link to comment
https://forums.phpfreaks.com/topic/152518-solved-ifratio1ratio2-how-do-you-know/
Share on other sites

You'll need to know within what size you need to be.

 

$heightMax = 200;
$widthMax = 200;

$width = $widthOrig = imagex($src_img);
$height = $heightOrig = imagey($src_img);

if ($height > $heightMax) {
$width = round(($heightMax / $height) * $width);
$height = $heightMax;
}
if ($width > $widthMax) {
$height = round(($widthMax / $width) * $height);
$width = $widthMax;
}

echo "New size: {$width}x{$height}" . PHP_EOL;
echo "Old size: {$widthOrig}x{$heightOrig}" . PHP_EOL;
echo 'Ratio (before): ' . round($widthOrig/$heightOrig, 3) . PHP_EOL;
echo 'Ratio (after): ' . round($width/$height, 3) . PHP_EOL;

Yes I have that in, you also added it in now...

if ($height > $heightMax) { // WHY this
$width = round(($heightMax / $height) * $width); // WHY this 
$height = $heightMax; 
}
if ($width > $widthMax) { // WHY this
$height = round(($widthMax / $width) * $height); // WHY this
$width = $widthMax;
}

Ok, I see what you mean but don't fully understand it as such.

Why not just use;

$width = $width - 20%; 
$height = $height - 20%; 

Why the whole math system?

$height = round(($widthMax / $width) * $height);
$width = $widthMax;

Is there like a hidden manual that only a select few have access to?

Because you have to figure out what the growth rate is... I just picked 20% randomly, which would then have a growth rate of 0.8. The $widthMax / $width is that growth rate, and so you multiply it with the $height so the width and height will grow uniformly. Then you do the same again only with width and height switched.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.