Real-time image manipulation and cache using class.upload.php |
[eluser]helmutbjorg[/eluser]
Hi Guys, Thought i would share something as I guess this is how the whole open source thing works and I have already taken so much from this forum. I know this has been done before. I'm using the class.upload.php (if you don't know what it can do check out their samples page) because the CI image_lib doesn't handle PNG very well and I think it really is an unfinished and un-supported part of the library to be honest. And class.upload.php is amazing and thorough. This is not recommended for BEGINNERS. You must understand how the concept works before you can really get good use out of this technique. I wanted a way to let my clients upload an image (the biggest one they had) and then instead of manipulating that image on upload and creating various sizes to use throughout the system, I want to be able to use a script to generate an image (using a set format template) and save it to cache so that subsequent requests get the modified image. So essentially the resizing, cropping, conversion, etc only happens on the first request for the image. For example... A typical process (the old way) 1. A user uploads default.jpg which is 800x600 2. The system creates a thumbnail which is 100x75 and saves it as default_thumb.jpg 3. The system creates a small version which is 300x200 and saves it as default_small.jpg 4. Now the system can use those images like so... Code: <?php echo img('uploads/default_small.jpg'); ?> My system 1. A user uploads default.jpg which is 800x600 2. Now the system can use it like so Code: <?php echo img_format('uploads/default.jpg', 'small'); ?> The first time that tag is called the default.jpg image is modified according to the config image format set for 'small' and saved to cache. The second time the image is loaded from cache requiring no processing time. The benefits: 1. You don't have to lock yourself in to any particular formats for images so that if requirements change you can deliver any size image or type. 2. Make a change to the format template and delete the cache and the whole site is updated.
[eluser]helmutbjorg[/eluser]
How to implement 1. Download class.upload.php and save to system/application/libraries/class.upload.php 2. Create system/application/libraries/Imaging.php Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 3. Add this function to system/application/helpers/MY_html_helper.php (create it if you have to) Code: /** 4. Create system/application/config/my_image_formats.php Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 5. Create a folder outside of the above system folder called /cache/ (note NOT /system/cache/ NOT system/application/cache BUT /cache/. If you have htaccess to remove index.php don't forget to add an allowance for the cache folder.
[eluser]louis w[/eluser]
Very interesting, thanks for the post. I am going to check this out. Have been very disappointed with the built in image resizing.
[eluser]Colin Williams[/eluser]
I've used something very similar since CI 1.3. It does use the CI image_lib class, and has a different system for caching (something more natural, utilizing mod_rewrite rules). I have a rough draft of a tutorial started for it. Will have it out one of these days...
[eluser]helmutbjorg[/eluser]
@Colin - would you care to elaborate on the mod_rewrite bit? @Louis - No problems. Even if you do not use the real-time cache bit the class.upload.php is great for general use anyway.
[eluser]Colin Williams[/eluser]
There's no quick explanation of it ![]()
[eluser]longlostcousin[/eluser]
hi. thanks for this library! i have set it up on a development site and i am looping through an array of images in a view and it turns them all into the same image. it seems to only access the image library once. not sure what is going on. any ideas? thanks! mark
[eluser]helmutbjorg[/eluser]
Problem solved. I have updated the original post... Update the Imaging.php library and also the html helper function. The problem was that codeigniter was only running the class.upload.php constructor once (as is codeigniters excellent way!) but that class requires the constructor to be hit for each file. All I needed to change in the helper was Code: // From Cheers
[eluser]helmutbjorg[/eluser]
Don't forget to empty the cache directory before you try again.
[eluser]roj[/eluser]
I know the topic is kind of closed — given that the problem is solved — but I still don't quite understand the problem. Surely this just adds to server loads when caches expire and adds irritation to the end user who tries to load the file the first time and has to wait that little bit longer? Perhaps I just don't get it but the benefits listed don't make much sense to me: 1. if you have the original image you're not really tied to anything as they can all be resized. 2. how is this any different to changing the format template and then a having a simple to script to resize the various templates to the new size in a single batch? As I said I just don't get the benefits, but I'm willing to learn.... |
Welcome Guest, Not a member yet? Register Sign In |