How to Resize Image Dynamically in PHP
- By Viral Patel on May 4, 2012
Today almost every website you visit show content in form of thumbnails. Thumbnails are nothing but images displayed next to the content. Be it News website or a blog, displaying images next to content is key to appeal user. Even our blog shows images as thumbnails on home page.
A prerequisites to show Thumbnail in a webpage is that thumbnail must be small enough. So that many thumbnails can be loaded as fast as possible. Hence almost every website resize the image to create small thumbnails.
So how to do this on the fly? How to resize an Image dynamically in PHP?
There is an extremely useful PHP library called timthumb which comes very handy. It’s just a simple PHP script that you need to download and put in some folder under your website. And then simply call it with appropriate arguments.
- Download timthumb.php and put under any folder.
- Upload this script
timthumb.php
though FTP to your web hosting. Put it under a directory/script
. - Just call timthumb.php with appropriate arguments, For example:
<
img
src
=
"/script/timthumb.php?src=/some/path/myimage.png&w=100&h=80"
alt
=
"resized image"
/>
And that’s all!!
One thing worth noting here is that this library will create a folder cache in the directory where timthumb.php script resides. This folder will cache the resized image for better performance.
You can refer following table for different parameters and its meaning.
Parameter | Values | Meaning | |
---|---|---|---|
src | source | url to image | Tells TimThumb which image to resize |
w | width | the width to resize to | Remove the width to scale proportionally (will then need the height) |
h | height | the height to resize to | Remove the height to scale proportionally (will then need the width) |
q | quality | 0 – 100 | Compression quality. The higher the number the nicer the image will look. I wouldn’t recommend going any higher than about 95 else the image will get too large |
a | alignment | c, t, l, r, b, tl, tr, bl, br | Crop alignment. c = center, t = top, b = bottom, r = right, l = left. The positions can be joined to create diagonal positions |
zc | zoom / crop | 0, 1, 2, 3 | Change the cropping and scaling settings |
f | filters | too many to mention | Let’s you apply image filters to change the resized picture. For instance you can change brightness/ contrast or even blur the image |
s | sharpen | Apply a sharpen filter to the image, makes scaled down images look a little crisper | |
cc | canvas colour | hexadecimal colour value (#ffffff) | Change background colour. Most used when changing the zoom and crop settings, which in turn can add borders to the image. |
ct | canvas transparency | true (1) | Use transparency and ignore background colour |
Hope this is useful for you
Related Posts
Get our Articles via Email. Enter your email address.
i am beginner of php,how i start to learn.
If u r a beginner then you can start leaning PHP from http://www.php.net/manual/en/index.php. This site will help you learning it step wise step… God bless.
If you are new, the PHP.net manual will give you nightmares. Everyone points newbies to it and I believe that is not the best place to start, as the manual is written for people who already understand PHP. For a newbie, it is an incredibly daunting thing, in my opinion.
Start with David Powers’ book PHP Solutions, you can order it from Amazon. I started with this book and used it to self-teach myself PHP. I am now getting to the point where I am learning more than the book can provide, but for the first several years, this book was a godsend, and I use it as a reference guide each time I program. I can not stress how good this book was for me.
Once you get your feet wet, sign up for an account with StackOverflow.com. It is not quite a forum, but it is absolutely the best place to get help.
Read the book I mentioned, take and keep notes, and read forums and StackOverflow posts to get an idea how to ask your questions the right way. Many experts will chide you for not asking questions the right way which sucks because being a newbie, how WOULD you know the right way to ask the question? Still, do some research on SO and you’ll get a good idea how to ask questions to get you the best results.
PHP Solutions by David Powers and StackOverflow will be your two BEST friends for learning PHP.
Cheers!
I thought that Timthumb had some nasty vulnerabilities and that is why it has been removed from WordPress.
dear sir,
I have a problem in my wordpress blog,when i post new things in my blog there are not showing that post in my home page. i have done setting option to show updated post.but there are not showing now, can you sir tell me how to do this. plz sir plz guide me. my blog is http://shrikrishnaprasadam.com/ you can see this blog sir..
thanks
thanks
thanks, great code for image resize.
thanks