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.

                 $sourceImage = imagecreatefromjpeg("../img/whiteBG.jpg");
                 $dimensions = getimagesize($files["tmp_name"][$i]);
                 $ratio = $dimensions[0] / $dimensions[1]; // width/height

                 $dst_y = 0;
                 $dst_x = 0;

                 //final image should be 600x360
                 if ($ratio > 1) {
                        $width = 600;
                        $height = 360 / $ratio;
                        $dst_y = (360 - $height) / 2;
                 } else {
                        $width = 600 * $ratio;
                        $height = 360;
                        $dst_x = (600 - $width) / 2;
                 }

                 $src = imagecreatefromstring(file_get_contents($files["tmp_name"][$i]));
                 $dst = imagecreatetruecolor($width, $height);
                 imagecopyresampled($dst, $src, 0, 0, 0, 0, $width, $height, $dimensions[0], $dimensions[1]);

                 imagecopymerge($sourceImage, $dst, $dst_x, $dst_y, 0, 0, imagesx($dst), imagesy($dst), 100);
                 $moved = imagepng($sourceImage, $dir . $filename);

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

             $targetWidth = 600;
             $targetHeight = 360;
             $dstY = 0;
             $dstX = 0;
             $ratioFactor = 0;

             $bgImage = imagecreatefromjpeg("../img/bgWhite.jpg");
             $dimensions = getimagesize($files["tmp_name"][$i]);
             $sourceWidth = $dimensions[0];
             $sourceHeight = $dimensions[1];
             $ratio = $sourceWidth / $sourceHeight; // width/height

             //final image should be 600x360
             if ($ratio > 1) {
                    $ratioFactor = $sourceWidth / $targetWidth;
                    $targetHeight = $sourceHeight / $ratioFactor;
                    $dstY = (360 - $targetHeight) / 2;
             } else {
                    $ratioFactor = $sourceHeight / $targetHeight;
                    $targetWidth = $sourceWidth / $ratioFactor;
                    $dstX = (600 - $targetWidth) / 2;
             }

             $source = imagecreatefromstring(file_get_contents($files["tmp_name"][$i]));
             $target = imagecreatetruecolor($targetWidth, $targetHeight);
             imagecopyresampled($target, $source, 0, 0, 0, 0, $targetWidth, $targetHeight, $sourceWidth, $sourceHeight);

             imagecopymerge($bgImage, $target, $dstX, $dstY, 0, 0, imagesx($target), imagesy($target), 100);
             imagepng($bgImage, $dir . $filename);
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement