Search code examples
phpresize-image

PHP - Resize image in server before browser downloads it


There is a file in stored in the server, which for instance has dimension of 400 X 600 pixels. But in different part of the websites, we need the same picture of different dimension. For thumbnail, we usually need 50 X 50 pixels and if we choose to download the same picture with original dimension, it would take longer and the page load would slow down. If that picture is resized its size would go much more down (like, 500 KB to 50 KB). So, what I want is, before the browser downloads the picture, server should resize the picture to the dimensions as required in PHP script.


Solution

  • You need to put the code in thumbnail code in a new file lets say image.php and then use this script as the src of the img tag with parameters of image file name, width and height.

    <img src="http://path-to-directory/image.php?img=abc.jpg&width=50&height=50" />
    

    You need change the "DSC01088.jpg" to echo $_GET['img']; after proper validation.