How to Resize Image Dynamically in PHP

resize-image-dynamically-php

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.

  1. Download timthumb.php and put under any folder.
  2. Upload this script timthumb.php though FTP to your web hosting. Put it under a directory /script.
  3. 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" />
Code language: HTML, XML (xml)

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.

 ParameterValuesMeaning
srcsourceurl to imageTells TimThumb which image to resize
wwidththe width to resize toRemove the width to scale proportionally (will then need the height)
hheightthe height to resize toRemove the height to scale proportionally (will then need the width)
qquality0 – 100Compression 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
aalignmentc, t, l, r, b, tl, tr, bl, brCrop alignment. c = center, t = top, b = bottom, r = right, l = left. The positions can be joined to create diagonal positions
zczoom / crop0, 1, 2, 3Change the cropping and scaling settings
ffilterstoo many to mentionLet’s you apply image filters to change the resized picture. For instance you can change brightness/ contrast or even blur the image
ssharpen Apply a sharpen filter to the image, makes scaled down images look a little crisper
cccanvas colourhexadecimal colour value (#ffffff)Change background colour. Most used when changing the zoom and crop settings, which in turn can add borders to the image.
ctcanvas transparencytrue (1)Use transparency and ignore background colour

Hope this is useful for you :)

Get our Articles via Email. Enter your email address.

You may also like...

44 Comments

  1. DEV says:

    i am beginner of php,how i start to learn.

    • Deepika Rawat says:

      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.

      • Dev says:

        Thanks Dear

        • is it working on ftp server !!!!!!!!!!!!!!

          please replay

          how to url path set………..

    • NoName says:

      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!

  2. CS says:

    I thought that Timthumb had some nasty vulnerabilities and that is why it has been removed from WordPress.

  3. 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

    • Abhi says:

      thanks

  4. huynhhieu says:

    thanks, great code for image resize.

  5. Abhi says:

    thanks

  6. myfishtory says:

    Thumbnails of large image created is being oriented 90 deg couter-clockwise ; this issue doesnt affect smaller images. How to overcome this?

  7. Ashish says:

    I want to code one PHP script which allows me to generate thumbs of given images with specified dimensions and names as per Next-Gen Gallery Plugin of WordPress.

    I have website for cover pages: http://facebook4free.com/

    Thumb Gallery Page: http://facebook4free.com/category/facebook-cover-pages/abstract-facebook-cover-pages-facebook-cover-pages/

    Main Image Page: http://facebook4free.com/abstract-cover-pages-for-facebook-profile-2/facebook-images/image/1-cover-photo-82140/

    Issue is timeout when I upload images in wordpress plugin. It uploads images in create their thumbs but when I upload more than 10 images it fails. So I want to separately create PHP script which create thumbnails of my FB cover pages with given dimensions and specified name (a pattern).

    Any help would be appreciated.

  8. Create thumbnail image by php
    When we upload large size images on server. Uploaded large images take more time to load on webpage, so we need to show small size images on our webpage. Image thumbnail is a solution to generate uploaded image’s thumbnail to show required size images on our website.
    http://codelibrary.googleplus.co.in/create-thumbnail-image-by-php/

  9. yasin says:

    i have some knowledge in php and i want to develop my site in codeigniter i am new to codeigniter then how to start my new site and please guide me how to learn codeigniter. please guide me.

  10. yessie says:

    ‘The resource from this URL is not text:’ why it always appear when my code using timthumb?

  11. Geoman says:

    This tutorial saved a brother. thanks

    again i need a tutorial to convert a php output to pdf once the submit button is clicked.

    here is what i mean

    1. a user fills my form
    2. he clicks on submit button
    3. the next screen he gets is a summary of what he had filled as a pdf file.

  12. nice one.. timthumb really works very well :)

  13. Jon says:

    Great info. I have been wondering about this – does anyone have any feel for the time for user to d/l larger image (and their browser resizes) vs. the time a server takes to resize and serve the smaller image. I suppose their are a number of parameters but are we talking orders of magnitude for, say, a 1000 x 1000 image being displayed as 100 x 100 px.

  14. Anna says:

    Awesome script ..It really worked for me.Thanks Sir

  15. chris says:

    Im having an issue I don’t know if it is related can someone please advise to a fix

    using php 5.5.3 when I use 5.3.27 all is fine I cant use 5.4 cause it asks all to download a file.

    here is the error:
    Warning: Illegal string offset ‘file’ in /home/name/public_html/wp-content/themes/swift/lib/plugins/dynamic-thumbnails.php on line 92

    any help would be appreciated

  16. mahesh says:

    if i place all script,image in folder. It is not working. Did you have solution for that.

  17. I will suggest use style property for image tag. and add max-width=100% and max-height=100%. And set parent container to fixed width & height. Image will be shown in correct ratio. If you do not wish to call that file everytime you print image tag.

  18. darya says:

    hi
    thank yor sir
    Excuse me
    for GIF ,transparency dosen’t work
    please help me
    I use CT=1 but it isn’t useful

  19. Dnyanesh says:

    Thanks for the post. Its important to resize images as it reduces the data payload between client and server.

  20. himanshu says:

    Resize Image While Downloading with PHP..

  21. Keshav Patidar says:

    Nice Library.

  22. Nikhil Lad says:

    Its really cool library. good work

  23. Darshan says:

    It is really Good library. nice one

  24. naveen says:

    I have used the script and its works fine. But the code fails if the image size is very large. I am trying to resize an image with 12MB and more. Is it a bug or do I need to make any changes in the library. Please help.

  25. amirali says:

    niiiice

  26. Wot says:

    Eeerrrr, did anybody actually take a look through the script?

    Prefer the built in imagecopyresized function
    https://secure.php.net/manual/en/function.imagecopyresized.php

  27. sandy says:

    Not working.

  28. baby hazel says:

    I have used the script and its works fine.

  29. komal says:

    nice

  30. Fresh Lagu says:

    Please help me for import image with http:// Thank you

  31. Ngemusik says:

    Thanks

  32. Jason says:

    Nice article, I cannot find the script as the file download link is not available and it is on end.

  33. jason says:

    Very nice tutorial. wiil try this

  34. Fresh-Lagu.Co says:

    where I could learn php program easily and quickly

  35. Cakra Khan says:

    Nice info guys

  36. Uswatul says:

    The tutorial so nice.

  37. Atul says:

    Nice post

  38. Planetlagu says:

    Nice post, thanks your article

  39. Laguku says:

    Thanks for information code

Leave a Reply

Your email address will not be published. Required fields are marked *