Resizing fixed-width image

0

I'm having trouble resizing an image to a specific width, I can only resize proportionally according to width or height.

I can only:

<?php

$original_width = x;
$original_height = y;

$redimensionar = 200;

// Novos valores
$width = 0;
$height = 0;

if ($original_width > $original_height) {
    $ratio = ($redimensionar / $original_width);
}
else {
    $ratio = ($redimensionar / $original_height);
}

$width = ($original_width * $ratio);
$height = ($original_height * $ratio);

The problem with this is that the image may be 200 wide or tall, what I have to do and I can not do is leave the width always with 200 and the proportional height.

[EDITED]

When I made a code to always resize with a width of 200, the ratio calculation was upside down, so I was working with just this one up.

    
asked by anonymous 19.01.2015 / 16:18

1 answer

2

If you want the image to always have width equal to 200 then its new width should be 200 and the new height should be proportional, ie equal to its ratio times the new width.

<?php

$original_width = x;
$original_height = y;

$redimensionar = 200;

// Novos valores
$width = 0;
$height = 0;

$ratio = ($original_height / $original_width);

$width = $redimensionar;
$height = ($width * $ratio);

This is because the altura/largura ratio must remain constant (for the scalar image proportionally) so if the new width is 200 the new height is obtained by doing largura * razão which in the case is 200 * razão .

    
19.01.2015 / 16:25