Search code examples
phpimagerestimage-processingdimensions

Calculate nearest possible dimension value PHP (keeping ratio)


I am trying to implement API for image resizing. It is created not exactly for image processing, this is only one part/feature of API.

What I want to implement.

I have url for retrieving image from server it looks like

 mywebpage.com/api/product/42/image

This url will return URL to full image of product with id 42.
Everything is ok.
We can specify desired size with GET parameters like this

 mywebpage.com/api/product/42/image?width=200&height=300

It also looks fine

But my question if following.
As we can have different images on server with different dimension and aspect ratio, I need to keep this ratio while resizing.

For example I need image to fit 200x300 container but I have 1024x576 (16:9) image on the server. I need to resize this image but keep initial aspect ratio(16:9) but to fit desired container.

How can I efficiently calculate new image size to return depending on incoming desired dimension and current image aspect ratio.

I want to thank everyone in advance for any help or advises.


Solution

  • Here is a script I used to make similar thing. Quite old , so may be not up to date.

    <?php
    
        if( isset($_GET["width"]) && is_numeric($_GET["width"]))
            $target_width = intval($_GET["width"]);
        else
            $target_width= 200;//default value
    
        if( isset($_GET["height"]) && is_numeric($_GET["height"]))
            $target_height = intval($_GET["width"]);
        else
            $target_height= 300;//default value
    
        if( isset($_GET["id"]) && is_numeric($_GET["id"]))//prevent any unwanted filesystem access 
            $original_image_path = "img/products/$id.jpg";
        else
            $original_image_path = "placeholder.png"
    
        //http://php.net/manual/fr/function.getimagesize.php
        $image_size = getimagesize($original_image_path);
    
        //get the ratio of the original image
        $image_ratio=  $image_size[1]/ $image_size[0];
    
        $original_image = imagecreatefromjpeg($original_image_path);
        $new_image = imagecreatetruecolor($target_width, $image_ratio * $target_width);
    
        //paints the image in white
        //http://php.net/manual/en/function.imagefill.php
        //http://php.net/manual/en/function.imagecolorallocatealpha.php
        imagefill( $new_image, 0, 0, imagecolorallocatealpha($new_image, 255,255,255,127) ); 
        imagesavealpha($new_image, TRUE);
    
        /*
         Copies the original to the new, preserving the ratio. 
         The original image fills all the width of the new, 
         and is placed on the top of the new.
         http://php.net/manual/en/function.imagecopyresized.php
        */
        imagecopyresized(
                            $new_image,$original_image,
                            0,0,
                            0,0,
                            $target_width,$image_ratio * $target_width,
                            $image_size[0],$image_size[1]
                        ); 
    
        //image is returned in response to the request
        header ("Content-type: image/png");
        imagepng( $new_image ); 
    
    ?>