Project

General

Profile

1
<?php
2
/**
3
 * PhpThumb Library Definition File
4
 * 
5
 * This file contains the definitions for the PhpThumbFactory class.
6
 * It also includes the other required base class files.
7
 * 
8
 * If you've got some auto-loading magic going on elsewhere in your code, feel free to
9
 * remove the include_once statements at the beginning of this file... just make sure that
10
 * these files get included one way or another in your code.
11
 * 
12
 * PHP Version 5 with GD 2.0+
13
 * PhpThumb : PHP Thumb Library <http://phpthumb.gxdlabs.com>
14
 * Copyright (c) 2009, Ian Selby/Gen X Design
15
 * 
16
 * Author(s): Ian Selby <ian@gen-x-design.com>
17
 * 
18
 * Licensed under the MIT License
19
 * Redistributions of files must retain the above copyright notice.
20
 * 
21
 * @author Ian Selby <ian@gen-x-design.com>
22
 * @copyright Copyright (c) 2009 Gen X Design
23
 * @link http://phpthumb.gxdlabs.com
24
 * @license http://www.opensource.org/licenses/mit-license.php The MIT License
25
 * @version 3.0
26
 * @package PhpThumb
27
 * @filesource
28
 */
29

    
30
// define some useful constants
31
define('THUMBLIB_BASE_PATH', str_replace('\\','/',dirname(__FILE__)) );
32
define('THUMBLIB_PLUGIN_PATH', THUMBLIB_BASE_PATH . '/thumb_plugins/');
33
define('DEFAULT_THUMBLIB_IMPLEMENTATION', 'gd');
34

    
35
/**
36
 * Include the PhpThumb Class
37
 */
38
require_once THUMBLIB_BASE_PATH . '/PhpThumb.inc.php';
39
/**
40
 * Include the ThumbBase Class
41
 */
42
require_once THUMBLIB_BASE_PATH . '/ThumbBase.inc.php';
43
/**
44
 * Include the GdThumb Class
45
 */
46
require_once THUMBLIB_BASE_PATH . '/GdThumb.inc.php';
47

    
48
/**
49
 * PhpThumbFactory Object
50
 * 
51
 * This class is responsible for making sure everything is set up and initialized properly,
52
 * and returning the appropriate thumbnail class instance.  It is the only recommended way 
53
 * of using this library, and if you try and circumvent it, the sky will fall on your head :)
54
 * 
55
 * Basic use is easy enough.  First, make sure all the settings meet your needs and environment...
56
 * these are the static variables defined at the beginning of the class.
57
 * 
58
 * Once that's all set, usage is pretty easy.  You can simply do something like:
59
 * <code>$thumb = PhpThumbFactory::create('/path/to/file.png');</code>
60
 * 
61
 * Refer to the documentation for the create function for more information
62
 * 
63
 * @package PhpThumb
64
 * @subpackage Core
65
 */
66
class PhpThumbFactory
67
{
68
    /**
69
     * Which implemenation of the class should be used by default
70
     * 
71
     * Currently, valid options are:
72
     *  - imagick
73
     *  - gd
74
     *  
75
     * These are defined in the implementation map variable, inside the create function
76
     * 
77
     * @var string
78
     */
79
    public static $defaultImplemenation = DEFAULT_THUMBLIB_IMPLEMENTATION;
80
    /**
81
     * Where the plugins can be loaded from
82
     * 
83
     * Note, it's important that this path is properly defined.  It is very likely that you'll 
84
     * have to change this, as the assumption here is based on a relative path.
85
     * 
86
     * @var string
87
     */
88
    public static $pluginPath = THUMBLIB_PLUGIN_PATH;
89
    
90
    /**
91
     * Factory Function
92
     * 
93
     * This function returns the correct thumbnail object, augmented with any appropriate plugins.  
94
     * It does so by doing the following:
95
     *  - Getting an instance of PhpThumb
96
     *  - Loading plugins
97
     *  - Validating the default implemenation
98
     *  - Returning the desired default implementation if possible
99
     *  - Returning the GD implemenation if the default isn't available
100
     *  - Throwing an exception if no required libraries are present
101
     * 
102
     * @return GdThumb
103
     * @uses PhpThumb
104
     * @param string $filename The path and file to load [optional]
105
     */
106
    public static function create ($filename = null, $options = array(), $isDataStream = false)
107
    {
108
        // map our implementation to their class names
109
        $implementationMap = array
110
        (
111
            'imagick'    => 'ImagickThumb',
112
            'gd'         => 'GdThumb'
113
        );
114
        
115
        // grab an instance of PhpThumb
116
        $pt = PhpThumb::getInstance();
117
        // load the plugins
118
        $pt->loadPlugins(self::$pluginPath);
119
        
120
        $toReturn = null;
121
        $implementation = self::$defaultImplemenation;
122
        
123
        // attempt to load the default implementation
124
        if ($pt->isValidImplementation(self::$defaultImplemenation))
125
        {
126
            $imp = $implementationMap[self::$defaultImplemenation];
127
            $toReturn = new $imp($filename, $options, $isDataStream);
128
        }
129
        // load the gd implementation if default failed
130
        else if ($pt->isValidImplementation('gd'))
131
        {
132
            $imp = $implementationMap['gd'];
133
            $implementation = 'gd';
134
            $toReturn = new $imp($filename, $options, $isDataStream);
135
        }
136
        // throw an exception if we can't load
137
        else
138
        {
139
            throw new Exception('You must have either the GD or iMagick extension loaded to use this library');
140
        }
141
        
142
        $registry = $pt->getPluginRegistry($implementation);
143
        $toReturn->importPlugins($registry);
144
        return $toReturn;
145
    }
146
}
(5-5/5)