PHPMatters Help You Better Hosting Your PHP-based Sites
How to Resize Image Using PHP Scripts - Better Usage of Images

How to Resize Image Using PHP Scripts – Better Usage of Images

The graphical element can be regarded as a critical ingredient of any website and web posts, for it can pique readers’ eyes effectively and ease their eyestrain among loads of textual content to a large extent. So far, we haven’t found any websites that fail to have the images attached.

However, to maximize the function of images, you’d better upload them with the most appropriate sizes. It is good when you find some images featuring the right sizes, but if the images are large, you have to resize them without sacrificing the quality. In this case, we’d like to tell you how to do this dynamically by using PHP scripts.

Resize Images with PHP

resizeThere is powerful PHP script available called timthumb for cropping, zooming, and resizing images of any formats like JPG, PNG, and GIF. After downloading it at any folder under your website, let’s say the theme folder, you can upload the script of timthumb.php via the FTP client. Now, you are allowed to call timthumb.php with appropriate arguments to resize your images. You can decide the height and width of your pictures according to the overall design of your website.

For instance, for this website, the best size for right/left aligned pictures that will be placed next to the paragraphs is 200×200, and the best size for some centrally aligned ones is 650×400. Then, the coding stuff can be displayed as following.

resize iamges code

Note that the parameter of “w” refers to width, “h” refers to height and “src” refers to the URL of resized image.

Here, we have to mention that this is different from setting the width and height attributes on the image tag in your HTML like the example showed in below.

setting the width height attrabutes

Generally, the HTML attributes can display your images perfectly with the appropriate sizes. However, these images only be showcased in the limited size, but not be resized by browsers. This means the images still need to be downloaded at the full size, thus taking a long time in the case that the size is large.

Timthumb is totally different. This script will create a folder cache in the directory of timthumb.php, caching the resized images to guarantee a fast page loading speed.

In addition to the above mentioned PHP script, you can also make use of the Simple Image PHP class to resize images, and in the meanwhile, keep the aspect ratio. Note that this is a simple class that can resize images while maintaining the original proportions and quality. You can view the code in this page.

To better utilize this script, you also need to understand the following examples clearly.

Simply resize the images with the preferred width and height.

resize the images with preferred width and height code

Only resize the width but retain the dimensions ratio.

only resize width but retain dimensions ratio code

Scale the image to a specific percentage off the regular width and height.

scale the image to a specific percentage coding

Alternative Method

To be honest, the utilization of PHP might be complicated and difficult for some beginners. Therefore, in addition to resizing images using the coding staff, you can also make use of some image resizers. JPEGMini, for instance, is a powerful tool that can reduce the file size of your images up to 5 times and maintain the original quality. You can use it to compress the pictures at first, and then upload them into the website.