If you’ve been frustrated with having to resize images for the sake of continuity or load time and don’t want to develop an entire caching system than this post will be of great value to you.
Today I will discusses the theory and resolve of applying a system to autonomously resize images, with the GD library for PHP, before they get requested by the browser.
This practice presents many benefits to its use.
- Save loads of hard drive space
- Easy to resize div backgrounds
Calling the Application With HTML
Calling the php script is this easy. You can place this tag anywhere on any page and be able to display an image, with exact value constraints, and as many times as you need.
Developing the image.php Application
header() will make sure that the browser is aware how to handle the applicaton. Here it enables the page to be viewed as a jpeg source.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
Usually I break this larger portion of my post up into smaller pieces so I can reference all the code being used and what it does. But the fundamentals are so well noted and easy to navigate, that I figured even if I left it as so, that you should have no problem implementing this into your own work.
If source material is not working properly. Issue the following:
1 2 3
function_exists() will check the currently active Apache function repository, both internal and user-defined, to see if the listed function matches anything available.
If you’re receiving the error message than the server you’re running this code on does not support the GD library and you must either set it up manually or contact an administrator to do so for you.
Also note that only Jpeg formats are acceptable by this set up. Extending is not a difficult task. I only wanted you to understand the basics and be able to evolve with PHP from my writings.
Hopefully I’ve left more than enough room for you to adapt this into your own projects. You can add on to this with many of the GD library features documented by the PHP network.
If you develop an extension or even a class, from these tips, please feel free to share by commenting back and letting me know what you’ve done.