author | Dan |
Tue, 23 Sep 2008 23:31:51 -0400 | |
changeset 51 | 7009a9cbd46f |
parent 40 | bd3372a2afc1 |
child 73 | 1f55c324efcf |
permissions | -rw-r--r-- |
25 | 1 |
<?php |
2 |
||
3 |
/** |
|
4 |
* Greyhound - real web management for Amarok |
|
5 |
* Copyright (C) 2008 Dan Fuhry |
|
6 |
* |
|
7 |
* This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. |
|
9 |
* |
|
10 |
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
|
11 |
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. |
|
12 |
*/ |
|
13 |
||
14 |
/** |
|
15 |
* Determines an image's filetype based on its signature. |
|
16 |
* @param string Path to image file |
|
17 |
* @return string One of gif, png, or jpg, or false if none of these. |
|
18 |
*/ |
|
19 |
||
20 |
function get_image_filetype($filename) |
|
21 |
{ |
|
22 |
$filecontents = @file_get_contents($filename); |
|
23 |
if ( empty($filecontents) ) |
|
24 |
return false; |
|
25 |
||
26 |
if ( substr($filecontents, 0, 8) == "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a" ) |
|
27 |
return 'png'; |
|
28 |
||
29 |
if ( substr($filecontents, 0, 6) == 'GIF87a' || substr($filecontents, 0, 6) == 'GIF89a' ) |
|
30 |
return 'gif'; |
|
31 |
||
32 |
if ( substr($filecontents, 0, 2) == "\xFF\xD8" ) |
|
33 |
return 'jpg'; |
|
34 |
||
35 |
return false; |
|
36 |
} |
|
37 |
||
38 |
/** |
|
39 |
* Scales an image to the specified width and height, and writes the output to the specified |
|
40 |
* file. Will use ImageMagick if present, but if not will attempt to scale with GD. This will |
|
40
bd3372a2afc1
Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents:
25
diff
changeset
|
41 |
* always scale images proportionally, unless $preserve_ratio is set to false. |
25 | 42 |
* |
43 |
* Ported from Enano CMS (which is also my project) |
|
44 |
* |
|
45 |
* @param string Path to image file |
|
46 |
* @param string Path to output file |
|
47 |
* @param int Image width, in pixels |
|
48 |
* @param int Image height, in pixels |
|
49 |
* @param bool If true, the output file will be deleted if it exists before it is written |
|
40
bd3372a2afc1
Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents:
25
diff
changeset
|
50 |
* @param bool If true, preserves the image's aspect ratio (default) |
25 | 51 |
* @return bool True on success, false on failure |
52 |
*/ |
|
53 |
||
40
bd3372a2afc1
Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents:
25
diff
changeset
|
54 |
function scale_image($in_file, $out_file, $width = 225, $height = 225, $unlink = false, $preserve_ratio = true) |
25 | 55 |
{ |
56 |
global $db, $session, $paths, $template, $plugins; // Common objects |
|
57 |
||
58 |
if ( !is_int($width) || !is_int($height) ) |
|
59 |
return false; |
|
60 |
||
61 |
if ( !file_exists($in_file) ) |
|
62 |
return false; |
|
63 |
||
64 |
if ( preg_match('/["\'\/\\]/', $in_file) || preg_match('/["\'\/\\]/', $out_file) ) |
|
65 |
return false; |
|
66 |
||
67 |
if ( file_exists($out_file) && !$unlink ) |
|
68 |
return false; |
|
69 |
else if ( file_exists($out_file) && $unlink ) |
|
70 |
@unlink($out_file); |
|
71 |
if ( file_exists($out_file) ) |
|
72 |
// couldn't unlink (delete) the output file |
|
73 |
return false; |
|
74 |
||
75 |
$file_ext = substr($in_file, ( strrpos($in_file, '.') + 1)); |
|
76 |
switch($file_ext) |
|
77 |
{ |
|
78 |
case 'png': |
|
79 |
$func = 'imagecreatefrompng'; |
|
80 |
break; |
|
81 |
case 'jpg': |
|
82 |
case 'jpeg': |
|
83 |
$func = 'imagecreatefromjpeg'; |
|
84 |
break; |
|
85 |
case 'gif': |
|
86 |
$func = 'imagecreatefromgif'; |
|
87 |
break; |
|
88 |
case 'xpm': |
|
89 |
$func = 'imagecreatefromxpm'; |
|
90 |
break; |
|
91 |
default: |
|
92 |
return false; |
|
93 |
} |
|
94 |
||
95 |
// try to find convert in the PATH |
|
96 |
// FIXME: unix specific (won't work on windows) |
|
97 |
$path = ( isset($_ENV['PATH']) ) ? $_ENV['PATH'] : ( isset($_SERVER['PATH']) ? $_SERVER['PATH'] : '/usr/local/bin:/usr/bin:/bin' ); |
|
98 |
$path = explode(':', $path); |
|
99 |
foreach ( $path as $dir ) |
|
100 |
{ |
|
101 |
if ( file_exists("$dir/convert") && is_executable("$dir/convert") ) |
|
102 |
{ |
|
103 |
$magick_path = "$dir/convert"; |
|
104 |
} |
|
105 |
} |
|
106 |
||
107 |
$can_use_magick = isset($magick_path); |
|
108 |
$can_use_gd = ( |
|
109 |
function_exists('getimagesize') && |
|
110 |
function_exists('imagecreatetruecolor') && |
|
111 |
function_exists('imagecopyresampled') && |
|
112 |
function_exists($func) |
|
113 |
); |
|
114 |
if ( $can_use_magick ) |
|
115 |
{ |
|
116 |
if ( !preg_match('/^([\/A-z0-9_-]+)$/', $magick_path) ) |
|
117 |
{ |
|
118 |
// ImageMagick path seems screwy |
|
119 |
return false; |
|
120 |
} |
|
40
bd3372a2afc1
Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents:
25
diff
changeset
|
121 |
$op = ( $preserve_ratio ) ? '>' : '!'; |
bd3372a2afc1
Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents:
25
diff
changeset
|
122 |
$cmdline = "\"$magick_path\" \"$in_file\" -resize '{$width}x{$height}$op' \"$out_file\""; |
bd3372a2afc1
Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents:
25
diff
changeset
|
123 |
exec($cmdline, $output, $return); |
25 | 124 |
if ( !file_exists($out_file) ) |
125 |
return false; |
|
126 |
return true; |
|
127 |
} |
|
128 |
else if ( $can_use_gd ) |
|
129 |
{ |
|
130 |
@list($width_orig, $height_orig) = @getimagesize($in_file); |
|
131 |
if ( !$width_orig || !$height_orig ) |
|
132 |
return false; |
|
133 |
// calculate new width and height |
|
134 |
||
40
bd3372a2afc1
Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents:
25
diff
changeset
|
135 |
if ( $preserve_ratio ) |
25 | 136 |
{ |
40
bd3372a2afc1
Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents:
25
diff
changeset
|
137 |
$ratio = $width_orig / $height_orig; |
bd3372a2afc1
Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents:
25
diff
changeset
|
138 |
if ( $ratio > 1 ) |
bd3372a2afc1
Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents:
25
diff
changeset
|
139 |
{ |
bd3372a2afc1
Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents:
25
diff
changeset
|
140 |
// orig. width is greater that height |
bd3372a2afc1
Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents:
25
diff
changeset
|
141 |
$new_width = $width; |
bd3372a2afc1
Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents:
25
diff
changeset
|
142 |
$new_height = round( $width / $ratio ); |
bd3372a2afc1
Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents:
25
diff
changeset
|
143 |
} |
bd3372a2afc1
Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents:
25
diff
changeset
|
144 |
else if ( $ratio < 1 ) |
bd3372a2afc1
Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents:
25
diff
changeset
|
145 |
{ |
bd3372a2afc1
Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents:
25
diff
changeset
|
146 |
// orig. height is greater than width |
bd3372a2afc1
Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents:
25
diff
changeset
|
147 |
$new_width = round( $height / $ratio ); |
bd3372a2afc1
Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents:
25
diff
changeset
|
148 |
$new_height = $height; |
bd3372a2afc1
Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents:
25
diff
changeset
|
149 |
} |
bd3372a2afc1
Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents:
25
diff
changeset
|
150 |
else if ( $ratio == 1 ) |
bd3372a2afc1
Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents:
25
diff
changeset
|
151 |
{ |
bd3372a2afc1
Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents:
25
diff
changeset
|
152 |
$new_width = $width; |
bd3372a2afc1
Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents:
25
diff
changeset
|
153 |
$new_height = $width; |
bd3372a2afc1
Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents:
25
diff
changeset
|
154 |
} |
bd3372a2afc1
Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents:
25
diff
changeset
|
155 |
if ( $new_width > $width_orig || $new_height > $height_orig ) |
bd3372a2afc1
Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents:
25
diff
changeset
|
156 |
{ |
bd3372a2afc1
Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents:
25
diff
changeset
|
157 |
// Too big for our britches here; set it to only convert the file |
bd3372a2afc1
Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents:
25
diff
changeset
|
158 |
$new_width = $width_orig; |
bd3372a2afc1
Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents:
25
diff
changeset
|
159 |
$new_height = $height_orig; |
bd3372a2afc1
Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents:
25
diff
changeset
|
160 |
} |
25 | 161 |
} |
40
bd3372a2afc1
Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents:
25
diff
changeset
|
162 |
else |
25 | 163 |
{ |
164 |
$new_width = $width; |
|
40
bd3372a2afc1
Added artwork spriting support. Artwork is now displayed using a gigantic CSS sprite instead of hundreds of little images. GD required.
Dan
parents:
25
diff
changeset
|
165 |
$new_height = $height; |
25 | 166 |
} |
167 |
||
168 |
$newimage = @imagecreatetruecolor($new_width, $new_height); |
|
169 |
if ( !$newimage ) |
|
170 |
return false; |
|
171 |
$oldimage = @$func($in_file); |
|
172 |
if ( !$oldimage ) |
|
173 |
return false; |
|
174 |
||
175 |
// Perform scaling |
|
176 |
imagecopyresampled($newimage, $oldimage, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig); |
|
177 |
||
178 |
// Get output format |
|
179 |
$out_ext = substr($out_file, ( strrpos($out_file, '.') + 1)); |
|
180 |
switch($out_ext) |
|
181 |
{ |
|
182 |
case 'png': |
|
183 |
$outfunc = 'imagepng'; |
|
184 |
break; |
|
185 |
case 'jpg': |
|
186 |
case 'jpeg': |
|
187 |
$outfunc = 'imagejpeg'; |
|
188 |
break; |
|
189 |
case 'gif': |
|
190 |
$outfunc = 'imagegif'; |
|
191 |
break; |
|
192 |
case 'xpm': |
|
193 |
$outfunc = 'imagexpm'; |
|
194 |
break; |
|
195 |
default: |
|
196 |
imagedestroy($newimage); |
|
197 |
imagedestroy($oldimage); |
|
198 |
return false; |
|
199 |
} |
|
200 |
||
201 |
// Write output |
|
202 |
$outfunc($newimage, $out_file); |
|
203 |
||
204 |
// clean up |
|
205 |
imagedestroy($newimage); |
|
206 |
imagedestroy($oldimage); |
|
207 |
||
208 |
// done! |
|
209 |
return true; |
|
210 |
} |
|
211 |
if ( file_exists($out_file) ) |
|
212 |
return true; |
|
213 |
return false; |
|
214 |
} |
|
215 |