1.9.x compatibility
[imagecompressor.git] / lib / class.imagecompressor_utils.php
1 <?php
2 #-------------------------------------------------------------------------
3 #
4 # Jonathan Schmid, <hi@jonathanschmid.de>
5 # Web: www.jonathanschmid.de
6 #
7 #-------------------------------------------------------------------------
8 #
9 # ImageCompressor is a CMS Made Simple module that can compress user-uploaded images.
10 #
11 #-------------------------------------------------------------------------
12 #
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 2 of the License, or
16 # (at your option) any later version.
17 #
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 # GNU General Public License for more details.
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 # Or read it online: http://www.gnu.org/licenses/licenses.html#GPL
26 #
27 #-------------------------------------------------------------------------
28
29 class imagecompressor_utils {
30
31         const SMUSHIT_API_URL = 'http://www.smushit.com/ysmush.it/ws.php?img=%s';
32         const SMUSHIT_UA = 'CMS made simple Image Compressor/0.10.2';
33
34         private function __construct() {}
35
36         static final public function format_bytes($size)
37         {
38                 $units = array(' B', ' KB', ' MB', ' GB', ' TB');
39                 for ($i = 0; $size >= 1024 && $i < 4; $i++)
40                 {
41                         $size /= 1024;
42                 }
43                 return round($size, 1).$units[$i];
44         }
45
46         static final protected function smushit_fopen($url)
47         {
48                 $f = @fopen($url, 'r');
49                 if($f === false)
50                 {
51                         return false;
52                 }
53                 $response = fgets($f);
54                 fclose($f);
55                 if($response == null)
56                 {
57                         return false;
58                 }
59                 if(function_exists('json_decode'))
60                 {
61                         return json_decode($response);
62                 }
63                 else
64                 {
65                         $json = new Services_JSON();
66                         return $json->decode($response);
67                 }
68         }
69
70         static final protected function smushit_curl($url)
71         {
72                 $smushit = curl_init();
73                 curl_setopt($smushit, CURLOPT_CONNECTTIMEOUT, 10);
74                 curl_setopt($smushit, CURLOPT_RETURNTRANSFER, true);
75                 curl_setopt($smushit, CURLOPT_URL, $url);
76                 curl_setopt($smushit, CURLOPT_USERAGENT, self::SMUSHIT_UA);
77                 $response = curl_exec($smushit);
78                 curl_close($smushit);
79                 if(function_exists('json_decode'))
80                 {
81                         return json_decode($response);
82                 }
83                 else
84                 {
85                         $json = new Services_JSON();
86                         return $json->decode($response);
87                 }
88         }
89
90         static final public function smushit_post($file_url)
91         {
92                 $url = sprintf(self::SMUSHIT_API_URL, urlencode($file_url));
93                 if(function_exists('curl_init'))
94                 {
95                         return self::smushit_curl($url);
96                 }
97                 else
98                 {
99                         return self::smushit_fopen($url);
100                 }
101         }
102
103         static final public function is_image($path)
104         {
105                 $info = @exif_imagetype($path);
106                 if($info & (IMAGETYPE_GIF | IMAGETYPE_JPEG | IMAGETYPE_PNG))
107                 {
108                         return true;
109                 }
110                 else
111                 {
112                         return false;
113                 }
114         }
115
116         static final public function sort_files_by_date($a, $b)
117         {
118                 if($a->filetime == $b->filetime)
119                 {
120                         return 0;
121                 }
122
123                 return $a->filetime > $b->filetime ? -1 : 1;
124         }
125
126         static final public function get_url_path()
127         {
128                 $config = cmsms()->GetConfig();
129
130                 $url = substr(realpath(dirname(__FILE__).'/../'), strlen($config['root_path']));
131                 $url = str_replace(DIRECTORY_SEPARATOR, '/', $url);
132                 $url = $config['root_url'] . $url;
133
134                 return $url;
135         }
136
137         static final public function url_fopen_enabled()
138         {
139                 if (!ini_get('allow_url_fopen'))
140                 {
141                         return false;
142                 }
143                 return true;
144         }
145 }