Project

General

Profile

1
<?php
2
/**
3
 *
4
 * @category        admin
5
 * @package         admintools
6
 * @author          WebsiteBaker Project
7
 * @copyright       Ryan Djurovich
8
 * @copyright       WebsiteBaker Org. e.V.
9
 * @link            http://websitebaker.org/
10
 * @license         http://www.gnu.org/licenses/gpl.html
11
 * @platform        WebsiteBaker 2.8.3
12
 * @requirements    PHP 5.3.6 and higher
13
 * @version         $Id: resize_img.php 2 2017-07-02 15:14:29Z Manuela $
14
 * @filesource      $HeadURL: svn://isteam.dynxs.de/wb/2.10.x/trunk/admin/media/resize_img.php $
15
 * @lastmodified    $Date: 2017-07-02 17:14:29 +0200 (Sun, 02 Jul 2017) $
16
 *
17
 */
18

    
19
    /**
20
     * Image Resizer.
21
     * @author : Harish Chauhan
22
     * @copyright : Freeware
23
     * About :This PHP script will resize the given image and can show on the fly or save as image file.
24
     *
25
     */
26

    
27

    
28
    define("HAR_AUTO_NAME",1);
29
    Class RESIZEIMAGE
30
    {
31
        var $imgFile="";
32
        var $imgWidth=0;
33
        var $imgHeight=0;
34
        var $imgType="";
35
        var $imgAttr="";
36
        var $type=NULL;
37
        var $_img=NULL;
38
        var $_error="";
39

    
40
        /**
41
         * Constructor
42
         *
43
         * @param [String $imgFile] Image File Name
44
         * @return RESIZEIMAGE (Class Object)
45
         */
46

    
47
        public function __construct($imgFile="")
48
        {
49
            if (!function_exists("imagecreate"))
50
            {
51
                $this->_error="Error: GD Library is not available.";
52
                return false;
53
            }
54

    
55
            $this->type=Array(1 => 'GIF', 2 => 'JPG', 3 => 'PNG', 4 => 'SWF', 5 => 'PSD', 6 => 'BMP', 7 => 'TIFF', 8 => 'TIFF', 9 => 'JPC', 10 => 'JP2', 11 => 'JPX', 12 => 'JB2', 13 => 'SWC', 14 => 'IFF', 15 => 'WBMP', 16 => 'XBM');
56
            if(!empty($imgFile))
57
                $this->setImage($imgFile);
58
        }
59
        /**
60
         * Error occured while resizing the image.
61
         *
62
         * @return String
63
         */
64
        function error()
65
        {
66
            return $this->_error;
67
        }
68

    
69
        /**
70
         * Set image file name
71
         *
72
         * @param String $imgFile
73
         * @return void
74
         */
75
        function setImage($imgFile)
76
        {
77
            $this->imgFile=$imgFile;
78
            return $this->_createImage();
79
        }
80
        /**
81
         *
82
         * @return void
83
         */
84
        function close()
85
        {
86
            return @imagedestroy($this->_img);
87
        }
88
        /**
89
         * Resize a image to given width and height and keep it's current width and height ratio
90
         *
91
         * @param Number $imgwidth
92
         * @param Numnber $imgheight
93
         * @param String $newfile
94
         */
95
        function resize_limitwh($imgwidth,$imgheight,$newfile=NULL)
96
        {
97
            $image_per = 100;
98
            list($width, $height, $type, $attr) = @getimagesize($this->imgFile);
99
            if($width > $imgwidth && $imgwidth > 0)
100
                $image_per = (double)(($imgwidth * 100) / $width);
101

    
102
            if(floor(($height * $image_per)/100)>$imgheight && $imgheight > 0)
103
                $image_per = (double)(($imgheight * 100) / $height);
104

    
105
            $this->resize_percentage($image_per,$newfile);
106

    
107
        }
108
        /**
109
         * Resize an image to given percentage.
110
         *
111
         * @param Number $percent
112
         * @param String $newfile
113
         * @return Boolean
114
         */
115
        function resize_percentage($percent=100,$newfile=NULL)
116
        {
117
            $newWidth=($this->imgWidth*$percent)/100;
118
            $newHeight=($this->imgHeight*$percent)/100;
119
            return $this->resize($newWidth,$newHeight,$newfile);
120
        }
121
        /**
122
         * Resize an image to given X and Y percentage.
123
         *
124
         * @param Number $xpercent
125
         * @param Number $ypercent
126
         * @param String $newfile
127
         * @return Boolean
128
         */
129
        function resize_xypercentage($xpercent=100,$ypercent=100,$newfile=NULL)
130
        {
131
            $newWidth=($this->imgWidth*$xpercent)/100;
132
            $newHeight=($this->imgHeight*$ypercent)/100;
133
            return $this->resize($newWidth,$newHeight,$newfile);
134
        }
135

    
136
        /**
137
         * Resize an image to given width and height
138
         *
139
         * @param Number $width
140
         * @param Number $height
141
         * @param String $newfile
142
         * @return Boolean
143
         */
144
        function resize($width,$height,$newfile=NULL)
145
        {
146
            if(empty($this->imgFile))
147
            {
148
                $this->_error="File name is not initialised.";
149
                return false;
150
            }
151
            if($this->imgWidth<=0 || $this->imgHeight<=0)
152
            {
153
                $this->_error="Could not resize given image";
154
                return false;
155
            }
156
            if($width<=0)
157
                $width=$this->imgWidth;
158
            if($height<=0)
159
                $height=$this->imgHeight;
160

    
161
            return $this->_resize($width,$height,$newfile);
162
        }
163

    
164
        /**
165
         * Get the image attributes
166
         * @access Private
167
         *
168
         */
169
        function _getImageInfo()
170
        {
171
            @list($this->imgWidth,$this->imgHeight,$type,$this->imgAttr)=@getimagesize($this->imgFile);
172
            $this->imgType=$this->type[$type];
173
        }
174

    
175
        /**
176
         * Create the image resource
177
         * @access Private
178
         * @return Boolean
179
         */
180
        function _createImage()
181
        {
182
            $this->_getImageInfo($this->imgFile);
183
            if($this->imgType=='GIF')
184
            {
185
                $this->_img=@imagecreatefromgif($this->imgFile);
186
            }
187
            elseif($this->imgType=='JPG')
188
            {
189
                $this->_img=@imagecreatefromjpeg($this->imgFile);
190
            }
191
            elseif($this->imgType=='PNG')
192
            {
193
                $this->_img=@imagecreatefrompng($this->imgFile);
194
            }
195
            if(!$this->_img || !@is_resource($this->_img))
196
            {
197
                $this->_error="Error loading ".$this->imgFile;
198
                return false;
199
            }
200
            return true;
201
        }
202

    
203
        /**
204
         * Function is used to resize the image
205
         *
206
         * @access Private
207
         * @param Number $width
208
         * @param Number $height
209
         * @param String $newfile
210
         * @return Boolean
211
         */
212
        function _resize($width,$height,$newfile=NULL)
213
        {
214
            if (!function_exists("imagecreate"))
215
            {
216
                $this->_error="Error: GD Library is not available.";
217
                return false;
218
            }
219

    
220
            $newimg=@imagecreatetruecolor($width,$height);
221
            //imagecolortransparent( $newimg, imagecolorat( $newimg, 0, 0 ) );
222

    
223
            if($this->imgType=='GIF' || $this->imgType=='PNG')
224
            {
225
                /** Code to keep transparency of image **/
226
                $colorcount = imagecolorstotal($this->_img);
227
                if ($colorcount == 0) $colorcount = 256;
228
                imagetruecolortopalette($newimg,true,$colorcount);
229
                imagepalettecopy($newimg,$this->_img);
230
                $transparentcolor = imagecolortransparent($this->_img);
231
                imagefill($newimg,0,0,$transparentcolor);
232
                imagecolortransparent($newimg,$transparentcolor);
233
            }
234

    
235
            @imagecopyresampled ( $newimg, $this->_img, 0,0,0,0, $width, $height, $this->imgWidth,$this->imgHeight);
236

    
237

    
238

    
239
            if($newfile===HAR_AUTO_NAME)
240
            {
241
                if(@preg_match("/\..*+$/",@basename($this->imgFile),$matches))
242
                       $newfile=@substr_replace($this->imgFile,"_har",-@strlen($matches[0]),0);
243
            }
244
            elseif(!empty($newfile))
245
            {
246
                if(!@preg_match("/\..*+$/",@basename($newfile)))
247
                {
248
                    if(@preg_match("/\..*+$/",@basename($this->imgFile),$matches))
249
                       $newfile=$newfile.$matches[0];
250
                }
251
            }
252

    
253
            if($this->imgType=='GIF')
254
            {
255
                if(!empty($newfile))
256
                    @imagegif($newimg,$newfile);
257
                else
258
                {
259
                    @header("Content-type: image/gif");
260
                    @imagegif($newimg);
261
                }
262
            }
263
            elseif($this->imgType=='JPG')
264
            {
265
                if(!empty($newfile))
266
                    @imagejpeg($newimg,$newfile,85);
267
                else
268
                {
269
                    @header("Content-type: image/jpeg");
270
                    @imagejpeg($newimg);
271
                }
272
            }
273
            elseif($this->imgType=='PNG')
274
            {
275
                if(!empty($newfile))
276
                    @imagepng($newimg,$newfile);
277
                else
278
                {
279
                    @header("Content-type: image/png");
280
                    @imagepng($newimg);
281
                }
282
            }
283
            @imagedestroy($newimg);
284
        }
285
    }
(13-13/16)