Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.