How can I do the width calculation when resizing an image that should be 3 X 4, knowing only the height?

1

I use PHP to be able to render images in a certain system.

I need the photos to contain the ratio of 3x4.

The system works like this:

1 - The user takes the photo from the webcam.

2 - An ajax is sent to PHP to process this image.

3 - The image is resized to 3x4 , but the default height should be 478 .

I need to know how I can know the width that I have to define for this image when I resize it, and I only have the time, because I do not want to be putting fixed numbers, I want to leave the calculation ready, that if I change the height, the width is dynamically adjusted.

What is the calculation that I should use in PHP to know the width of a 3x4 photo, based only on height?

The code I currently have is this:

 class Solicitacao {
     const IMAGE_HEIGHT = 478;
 }

In creating the image, I do so:

   $imageString = base64_decode(Input::get('file'));

   Gregwar\Image\Image::fromData($imageString)
                      ->resize($largura_dinamica_aqui, Solicitacao::IMAGE_HEIGHT)
                     ->save('temp.png', 'png');
    
asked by anonymous 20.05.2016 / 13:59

2 answers

4

I believe that Mathematically it is:

3x4:

Comprimento -> 3
Altura -> 4

That is, suppose we have a height of 600px and we do not know the length:

(600 * 3) / 4 = comprimento

You should know, but to adhere to your code it will be:

....
(IMAGE_HEIGHT * 3) / 4 = $width
resize($width, IMAGE_HEIGHT)
    
20.05.2016 / 14:08
1

Thanks for the answers, but I'd like to leave it a simple way. P

When I went searching for this calculation, I saw accounts back and forth to be able to do this, but it can be solved in a much simpler way. To say "with a line".

If we think of logic:

 3/4 = 0.75

Just do a multiplication to find the result in PHP:

 $width = Solicitacao::IMAGE_HEIGHT * 0.75; // 358.5
    
20.05.2016 / 14:35