srchub

srchub Git Source Tree


Root/pluf/src/Pluf/Image/Thumbnail.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# ***** END LICENSE BLOCK ***** */
 
/**
 * Generate the thumbnail of an image.
 *
 * Sample usage.
 *
 * $thumbnail = new Pluf_Image_Thumbnail($thumbnail_folder, $source_image);
 * if (!$thumbnail->exists()) {
 *     $thumbnail_filename = $thumbnail->generate();
 * }
 *
 */
class Pluf_Image_Thumbnail
{
    protected $dir = ''; /**< Path to the thumbnail folder. */
    protected $filename = ''; /**< Filename of the last created thumbnail. */
    public $source = ''; /**< Full path to the source file. */
    public $size = array(120, 120); /** Max width and heigth of the thumb. */
 
    /**
     * Init the thumbnail class.
     */
    public function __construct($dir, $source='')
    {
        $this->dir = $dir;
        $this->source = $source;
    }
 
    /**
     * Get the name of a thumbnail from the image name, the image
     * name can include a path this is just a md5() operation.
     * If the height and the width are given, the info is used to
     * generate the name, else not.
     *
     * @param array Optional size.
     * @return string Filename of the thumbnail.
     */
    public function getName($size=null)
    {
        $name = md5($this->source);
        if ($size === null) {
            $size = $this->size;
        }
        return $name.'-'.$size[0].'-'.$size[1].'.png';
    }
  
    /**
     * Get the full path to the thumbnail.
     */
    function getPath()
    {
        return $this->dir.'/'.$this->getName();
    }
 
    /**
     * Check if the thumbnail exists.
     */
    function exists()
    {
        return file_exists($this->getPath());
    }
 
    /**
     * Get the size of the last created thumbnail
     * return the same results as the builtin getimagesize PHP function.
     */
    function getSize()
    {
        if (file_exists($this->getPath())) {
            return getimagesize($this->getPath());
        } else {
            return false;
        }
    }
 
    /**
     * Create thumbnail of an image, proportions are kept.
     *
     * @return mixed Filename or false.
     */
    function generate()
    {
        if (!file_exists($this->source)) {
            return false;
        }
     
        if (($size = @getimagesize($this->source)) === false) {
            return false;
        }
        list($w, $h) = $this->size;
        $type = $size[2];
        $H = $size[1];
        $W = $size[0];
     
        if ($type == '1') {
            $function = 'imagecreatefromgif';
        } elseif ($type == '2') {
            $function = 'imagecreatefromjpeg';
        } elseif ($type == '3') {
            $function = 'imagecreatefrompng';
        } else {
            return false;
        }
        if (!function_exists($function)) {
            return false;
        }
        if (($img = @$function($this->source)) == false) {
            return false;
        }
        // get the zoom factors and the thumbnail height and width
        $rB = $H/$W;
        $rS = $h/$w;
        if (($H > $h) && ($W > $w)) {
            if ($rB > $rS) {
                $height = $h;
                $width  = $height/$rB;
            } else {
                $width = $w;
                $height = $width*$rB;
            }
        } elseif ($H > $h) {
            $height = $h;
            $width  = $height/$rB;
        } elseif ($W > $w) {
            $width = $w;
            $height = $width*$rB;
        } else {
            $height = $H;
            $width  = $W;
        }
        $zx = $W/$width;
        $zy = $H/$height;
 
        if (Pluf_Image_Thumbnail::gd_version() >= 2) {
            if (($img2=imagecreatetruecolor(round($width),round($height)))===false) {
                return false;
            }
        } else {
            if (($img2=ImageCreate(round($width),round($height)))===false) {
                return false;
            }  
        }
        $this->resampleBicubic($img2,$img,0,0,0,0,$width,$height,$zx,$zy);
        if (@imagepng($img2,$this->getPath(),9) === false) {
            return false;
        }
        imagedestroy($img2);
        if (@file_exists($this->getPath())) {
            return $this->getPath();
        } else {
            return false;
        }
    }
     
    /**
     * Get the current GD version. Need the output buffering functions.
     */
    public static function gd_version()
    {
        static $gd_version_number = null;
        if ($gd_version_number === null) {
            // Use output buffering to get results from phpinfo()
            // without disturbing the page we're in.  Output
            // buffering is "stackable" so we don't even have to
            // worry about previous or encompassing buffering.
            ob_start();
            phpinfo(8);
            $module_info = ob_get_contents();
            ob_end_clean();
            if (preg_match("/\bgd\s+version\b[^\d\n\r]+?([\d\.]+)/i",
                           $module_info,$matches)) {
                $gd_version_number = $matches[1];
            } else {
                $gd_version_number = '0';
            }
        }
        return $gd_version_number;
 
    }
 
    /* ========================================================================
     *   Private functions, should not be called from outside of the class.
     * ========================================================================
     */
 
    /**
     * Resample the image
     */
    protected function resampleBicubic(&$dst, &$src, $dstx, $dsty, $srcx,
                                       $srcy, $w, $h, $zoomX, $zoomY='')
    {
        if (!$zoomY) {
            $zoomY = $zoomX;
        }
        $palsize = ImageColorsTotal($src);
        for ($i = 0; $i<$palsize; $i++) {
            $colors = ImageColorsForIndex($src, $i);
            ImageColorAllocate($dst, $colors['red'], $colors['green'],
                               $colors['blue']);
        }
        $zoomX2 = (int)($zoomX/2);
        $zoomY2 = (int)($zoomY/2);
        $dstX = imagesx($dst);
        $dstY = imagesy($dst);
        $srcX = imagesx($src);
        $srcY = imagesy($src);
     
        for ($j=0; $j<($h-$dsty); $j++) {
            $sY = (int)($j*$zoomY) + $srcy;
            $y13 = $sY+$zoomY2;
            $dY = $j+$dsty;
         
            if (($sY >= $srcY) or ($dY >= $dstY) or ($y13 >= $srcY)) {
                break 1;
            }
            for ($i=0; $i<($w-$dstx); $i++) {
                $sX = (int)($i*$zoomX)+$srcx;
                $x34 = $sX+$zoomX2;
                $dX = $i+$dstx;
                if (($sX >= $srcX) or ($dX >= $dstX) or ($x34 >= $srcX)) {
                    break 1;
                }
                $c1 = ImageColorsForIndex($src, ImageColorAt($src, $sX, $y13));
                $c2 = ImageColorsForIndex($src, ImageColorAt($src, $sX, $sY));
                $c3 = ImageColorsForIndex($src, ImageColorAt($src, $x34, $y13));
                $c4 = ImageColorsForIndex($src, ImageColorAt($src, $x34, $sY));
         
                $r = ($c1['red']+$c2['red']+$c3['red']+$c4['red'])/4;
                $g = ($c1['green']+$c2['green']+$c3['green']+$c4['green'])/4;
                $b = ($c1['blue']+$c2['blue']+$c3['blue']+$c4['blue'])/4;
         
                ImageSetPixel($dst, $dX, $dY, ImageColorClosest($dst, $r,
                                                                $g, $b));
            }
        }
    }
}

Archive Download this file

Branches

Number of commits:
Page rendered in 0.12915s using 11 queries.