How can I calculate the width when resizing an image that must be 3 X 4, knowing only the height?

Asked

Viewed 2,258 times

1

Use the PHP to be able to process images in a given system.

I need the photos to contain the 3x4 ratio.

That’s how the system works:

1 - User takes webcam photo.

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

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

I need to know how I can know which width I have to set for that image when I resize it, and I only have the height, because I don’t want to keep putting fixed numbers, I want to get the calculation ready, so that if I change the height, the width is dynamically adjusted.

Which calculation should I use in PHP to know what is 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');
  • 3

    You swear you’ll ask that question?

  • @Wallace, we have to do it differently, the way this will have horizontally compressed images so we lose the ratio, I suggest you change your question and make an image-cutting system, where the user can choose the rectangle in the 3x4 ratio of the part of the image he wants to save, more or less like Instagram does, but in your case in a rectangle, and not square.

  • @bigown just wanted to see if they would complicate or simplify.

  • @bigown already asked the question, kkkkk

2 answers

4


I think mathematically it is:

3x4:

Comprimento -> 3
Altura -> 4

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

(600 * 3) / 4 = comprimento

I’m sure you already know, but for your code:

....
(IMAGE_HEIGHT * 3) / 4 = $width
resize($width, IMAGE_HEIGHT)
  • That’s basic three rule! Easy, easy!

  • 1

    I think that was the focus of our colleague’s doubt

  • It’s true that it’s easy, I already know the answer. But I still think you did it the hard way

  • Exactly Miguel! Straight to the point!

  • Thank you, for this context your solution is even better @Wallace Maxters

  • It may be @Miguel, but as the mustache said it is bad to give a code ready and the person does not know what it does. I think my -1 was deserved

Show 1 more comment

1

Thanks for the answers, but I would like to leave a simple way. P

When I went to research this calculation, I saw accounts back and forth so I could do this, but it can be solved in a much simpler way. For good to say "with a line".

If you 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
  • 2

    This is not necessarily simpler. Of course who some simplification. But there are simplifications that are not desirable. It’s not wrong, and I know what this., but it’s not so obvious to everyone. Many programmers would choose to leave the two operations to clearly indicate that it is a proportion calculation. Some would even have used constants instead of 3 and 4 to avoid such "magic numbers" and give more semantics to what they are doing. Others would even use variables, better still, parameters and make the calculation generic for any ratio.

  • The programmer’s job is to simplify, but it’s also to abstract, generalize, mainly to write readable code. The question says nothing about writing the most simplified code possible. It seems Code Golf, only hidden, only those who asked the question know the answer.

  • Okay Mr @bigown. Thanks for your tips ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.