Skip to content
Advertisement

PHP Resize Uploaded Image To Specific Dimensions Without Cropping

I currently have a script in which users can upload an image file of any dimensions. The uploaded image is sent via ajax to a PHP script where it should be resized and saved to the server. The resizing process should not crop or distort the image, but rather size it to a specific dimension by adding white to the sides, or top/bottom if it does not exactly match the dimensions. I have this process working great for a square image – but when trying to modify the process to work for rectangle dimensions it no longer functions correctly.

JavaScript

The outputted image ($moved) should have the final dimensions of 600 x 360. Instead, the final image is always distorted. If a tall image ratio is uploaded, the final product is stretch width wise. If a wider image ratio is uploaded then it gets compressed and mushed with extra top and bottom spacing. whiteBG.jpg is just a plain white jpeg with dimensions 600×360

Advertisement

Answer

Ended up correcting this. The issue was how I was calculating and compensating for ratios. This was the final code that corrected the issue, with variables to make the code more versatile for other situations

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement