Welcome Guest, Not a member yet? Register   Sign In
Thumbnailer - Flexible thumbnail generation in one line of code!
#1

[eluser]Unknown[/eluser]
This is my first contribution to the Code Igniter community. I'm still somewhat new here, so I doubt this is anything especially new or different, but I had fun making it and that's all that matters I guess. -shrugs-

Anyway, what I have for you is a really simple thumbnail generation helper. It supports multiple scale modes, can generate more than one size of thumbnail for the same image, and best of all; only uses one line of code!

Here's the code for thumbnailer_helper.php

Code:
<? if (!defined('BASEPATH')) exit('No direct script access allowed.');
/*****
  * Thumbnailer is a very flexible helper that generates thumbnails easily but only when
  * a thumnail with the same dimensions don't yet exist, making it light on server load.
  * @author     Stephen Belanger
  * @email      [email protected]
  * @filename   thumbnailer_healper.php
  * @title      Thumbnailer
  * @url        http://www.withstyledesign.com/
  * @version    1.0
  *****/

    function thumbnailer($filename, $path, $scalevalue="100", $scalemode = 'auto') {
        // Get current diimensions
        list($width, $height) = getimagesize($_SERVER['DOCUMENT_ROOT'].$path.$filename);

        // Set scaled dimensions
        switch ($scalemode) {
            case 'auto':
                if ($width > $height) {
                    $newwidth = $scalevalue;
                    $newheight = ($scalevalue / $width) * $height;
                } else {
                    $newwidth = ($scalevalue / $height) * $width;
                    $newheight = $scalevalue;
                }
                break;
            case 'x':
                $newwidth = $scalevalue;
                $newheight = ($scalevalue / $width) * $height;
                break;
            case 'y':
                $newwidth = ($scalevalue / $height) * $width;
                $newheight = $scalevalue;
                break;
        }

        // Load
        $source = imagecreatefromjpeg($_SERVER['DOCUMENT_ROOT'].$path.$filename);
        $thumb = imagecreatetruecolor($newwidth, $newheight);
        
        // Resize
        imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
        
        // Output Handling
        $thumbpath = $path."thumbs/".$scalemode."_".$scalevalue."_".$filename;
        if(!file_exists($thumbpath))
            imagejpeg($thumb, $thumbpath);

        return "/".$thumbpath;
    }
?>

To generate a thumbnail you just need to use this line;

Code:
<img src="&lt;?=thumbnailer("image.jpg", "path/to/file/", 100, 'auto')?&gt;" />

The last two values are the scale value and mode. Both are optional and set to 100 pixels for value and auto for mode by default. The mode can also be 'x' or 'y', which will limit scaling only on that particular dimension, while auto figures out which side is larger and sets it's maximum to the scale value .

That line of code will tell Thumbnailer to see if a thumbnail exists with those settings and, if not, it will generate one for you. It works quite handily if you place it in a loop. You may or may not want to make a few changes so it doesn't try and save thumbnails in a thumbs folder at the original image's path...it wouldn't work so great for thumbnailing images off a different server.
#2

[eluser]Jameson.[/eluser]
Stephen, thanks, this is a nice little helper, but wouldn't it be better to check if a thumbnail exists at the very beginning of the script, before image crunching procedures are done?
#3

[eluser]louis w[/eluser]
Doesn't look like this will work with GIF or PNG.

Also, why don't you use the Image Manipulation Class?
#4

[eluser]Unknown[/eluser]
I've actually rewritten it a few times since then--it's not really stable enough to post at the moment, but when it is I'll probably post it here.

I didn't use the Image Manipulation class because this was mostly all pre-existing code I used in other web design projects that I just did a quick port of for a recent project. At the time I didn't even know about that class, as this was my very first time using Code Igniter. There is certainly some room for improvement; I already moved the file checking to the start in the version I currently have and I'm working on changing it over to use the image manipulation class. I'm trying to figure out a good way to structure it so that I can support all the capabilities of it with a single line like how it is now--possibly passing a named array into it.

Sorry for being so slow on updating it, but I've had lots of contracts to get out of the way lately. Soon though, it'll definitely be ready soon.
#5

[eluser]Phil Sturgeon[/eluser]
For a simple jpeg, png, gif just explode the file extension off the end of the filename and use it to call which function needs to be used.

Or set an array.

Code:
$ext_to_func = array(
    'jpg' => 'jpeg',
    'jpeg' => 'jpeg',
    'gif' => 'gif',
    'png' => 'png',
    'bmp' => 'xbmp'
);

That converts extensions to the correct function name parts. So you can do this:

Code:
$source = {'imagecreatefrom'.$ext_to_func[$ext]}($_SERVER['DOCUMENT_ROOT'].$path.$filename);




Theme © iAndrew 2016 - Forum software by © MyBB