author | Dan |
Fri, 12 Jun 2009 13:47:12 -0400 | |
changeset 75 | 2f39cb7f54c4 |
parent 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 |
|
73
1f55c324efcf
Image tools: Use which() instead of reimplementing (should also work on win32 now)
Dan
parents:
40
diff
changeset
|
96 |
$magick_path = which('convert'); |
25 | 97 |
|
73
1f55c324efcf
Image tools: Use which() instead of reimplementing (should also work on win32 now)
Dan
parents:
40
diff
changeset
|
98 |
$can_use_magick = !empty($magick_path); |
25 | 99 |
$can_use_gd = ( |
100 |
function_exists('getimagesize') && |
|
101 |
function_exists('imagecreatetruecolor') && |
|
102 |
function_exists('imagecopyresampled') && |
|
103 |
function_exists($func) |
|
104 |
); |
|
105 |
if ( $can_use_magick ) |
|
106 |
{ |
|
107 |
if ( !preg_match('/^([\/A-z0-9_-]+)$/', $magick_path) ) |
|
108 |
{ |
|
109 |
// ImageMagick path seems screwy |
|
110 |
return false; |
|
111 |
} |
|
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
|
112 |
$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
|
113 |
$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
|
114 |
exec($cmdline, $output, $return); |
25 | 115 |
if ( !file_exists($out_file) ) |
116 |
return false; |
|
117 |
return true; |
|
118 |
} |
|
119 |
else if ( $can_use_gd ) |
|
120 |
{ |
|
121 |
@list($width_orig, $height_orig) = @getimagesize($in_file); |
|
122 |
if ( !$width_orig || !$height_orig ) |
|
123 |
return false; |
|
124 |
// calculate new width and height |
|
125 |
||
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
|
126 |
if ( $preserve_ratio ) |
25 | 127 |
{ |
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
|
128 |
$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
|
129 |
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
|
130 |
{ |
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
|
131 |
// 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
|
132 |
$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
|
133 |
$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
|
134 |
} |
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 |
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
|
136 |
{ |
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 |
// 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
|
138 |
$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
|
139 |
$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
|
140 |
} |
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 |
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
|
142 |
{ |
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 |
$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
|
144 |
$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
|
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 |
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
|
147 |
{ |
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 |
// 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
|
149 |
$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
|
150 |
$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
|
151 |
} |
25 | 152 |
} |
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
|
153 |
else |
25 | 154 |
{ |
155 |
$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
|
156 |
$new_height = $height; |
25 | 157 |
} |
158 |
||
159 |
$newimage = @imagecreatetruecolor($new_width, $new_height); |
|
160 |
if ( !$newimage ) |
|
161 |
return false; |
|
162 |
$oldimage = @$func($in_file); |
|
163 |
if ( !$oldimage ) |
|
164 |
return false; |
|
165 |
||
166 |
// Perform scaling |
|
167 |
imagecopyresampled($newimage, $oldimage, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig); |
|
168 |
||
169 |
// Get output format |
|
170 |
$out_ext = substr($out_file, ( strrpos($out_file, '.') + 1)); |
|
171 |
switch($out_ext) |
|
172 |
{ |
|
173 |
case 'png': |
|
174 |
$outfunc = 'imagepng'; |
|
175 |
break; |
|
176 |
case 'jpg': |
|
177 |
case 'jpeg': |
|
178 |
$outfunc = 'imagejpeg'; |
|
179 |
break; |
|
180 |
case 'gif': |
|
181 |
$outfunc = 'imagegif'; |
|
182 |
break; |
|
183 |
case 'xpm': |
|
184 |
$outfunc = 'imagexpm'; |
|
185 |
break; |
|
186 |
default: |
|
187 |
imagedestroy($newimage); |
|
188 |
imagedestroy($oldimage); |
|
189 |
return false; |
|
190 |
} |
|
191 |
||
192 |
// Write output |
|
193 |
$outfunc($newimage, $out_file); |
|
194 |
||
195 |
// clean up |
|
196 |
imagedestroy($newimage); |
|
197 |
imagedestroy($oldimage); |
|
198 |
||
199 |
// done! |
|
200 |
return true; |
|
201 |
} |
|
202 |
if ( file_exists($out_file) ) |
|
203 |
return true; |
|
204 |
return false; |
|
205 |
} |
|
206 |