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.


Messages In This Thread
Thumbnailer - Flexible thumbnail generation in one line of code! - by El Forum - 03-25-2008, 11:17 PM



Theme © iAndrew 2016 - Forum software by © MyBB