Skip to content

Commit fe50e53

Browse files
committed
changed mime type check call, added some additional debugging code
1 parent 2a2f884 commit fe50e53

3 files changed

Lines changed: 39 additions & 63 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ There's no perfect solution for serving up images for responsive websites. This
4040
- There is a baseline screen width for processing images without knowing the browser's clientWidth value. Currently the default it is set to 640px to minimise on file-size for low-bandwidth/narrow-screen devices but also to retain enough quality when images are embiggened on wider-screen devices (monitors, TVs, etc). This can be adjusted in the settings.
4141
- The script resizes images rounded up to the nearest 20 pixel value, so as to save on processing load. This can be adjusted in the settings.
4242
- It works with CSS background images. Yay! (Have to test CSS sprites still, but should work as well).
43+
- There's also a basic flush call to clear the original image from the resize server as well as all the resized images. Here is a sample URL: http://src.fejalish.com/{f}/http://src.yourdomain.com/images/filename.jpg
4344

4445
## Notes
4546

index.php

Lines changed: 37 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
$debug = false;
4+
35
//config file
46
require_once('config.php');
57

@@ -89,10 +91,14 @@
8991
fclose($file);
9092
}
9193

92-
//check image properties for mime type
93-
$props = getimagesize($local_input);
94-
//if mime type is image, process it
95-
if(strpos($props['mime'], 'image')!== false && strpos($props['mime'], 'image') >= 0){
94+
/*
95+
$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension
96+
finfo_file($finfo, $filename)
97+
finfo_close($finfo)
98+
*/
99+
100+
$mime = image_type_to_mime_type(exif_imagetype($local_input));
101+
if(strpos($mime, 'image')!== false && strpos($mime, 'image') >= 0){
96102

97103
//only need to calculate this if width is set
98104
if($w!="undefined"){
@@ -112,8 +118,8 @@
112118

113119
//compare screen width to values in ranges
114120
foreach ($ranges as &$range){
115-
//make sure have all the required values we need - min, max, and per or px
116-
//don't need pixel measurements here cause would need exact dimensions, not percentage and ranges...
121+
//make sure have all the required values we need - min, max, and per
122+
//don't actually need quality since it's not used in Imagick at all...
117123
if( ( isset($range["min"]) && is_numeric($range["min"]) ) && ( isset($range["max"]) && ( is_numeric($range["max"]) || $range["max"]=="*") ) && ( isset($range["per"]) && is_numeric($range["per"]) ) ){
118124

119125
//check in case max is set to "all"
@@ -146,58 +152,23 @@
146152
//check to see if resized image exists
147153
if (!file_exists($local_output)) {
148154

149-
/*
150-
//if quality is set in ranges
151-
if(isset($range["quality"])){
152-
//check if quality is below max
153-
if($range["quality"]<$max_quality){
154-
$quality = $range["quality"];
155-
} else {
156-
$quality = $max_quality;
157-
}
158-
}
159-
*/
160-
161155
//output resized image
162156
//FILTER_CATROM
163157
//FILTER_LANCZOS
164-
//$image->resizeImage(, ,Imagick::FILTER_CATROM,1);
165-
166-
//calc width, in case passing 0 is causing the errors
167-
$final_height = round( $image->getImageHeight() * ($final_width/$img_width) );
168-
169-
try {
170-
$image->scaleImage($final_width,$final_height,true);
171-
$image->writeImage($local_output);
172-
$image->clear();
173-
$image->destroy();
174-
} catch(Exception $e){
175-
echo 'Caught exception: ', $e->getMessage(), "\n";
176-
}
177-
178-
//output resized file
179-
//system("convert $local_input -strip -quality $quality -resize $final_width $local_output");
180-
181-
//error code to check if get error status message from convert call
182-
//uncomment if need to see results
183-
/*
184-
$retval = "";
185-
system("convert $local_input -strip -quality $quality -resize $final_width $local_output", $retval);
186-
187-
if($retval>0){
188-
$log = "[" . date('y m d H:i:s') . "]\n";
189-
$log .= $filename . "\n";
190-
$log .= "convert $local_input -strip -quality $quality -resize $final_width $local_output \n";
191-
//$log .= "output: ". print_r($output, true) . "\n";
192-
//$log .= "result: ". print_r($result, true) . "\n";
193-
//$log .= "system: ". $system . "\n";
194-
$log .= "retval: ". $retval . "\n";
195-
$log .= "----\n";
196-
$fp = fopen('error.log', 'at');
197-
fwrite($fp, $log);
198-
fclose($fp);
158+
//$image->resizeImage(, ,Imagick::FILTER_CATROM,1);
159+
160+
//calc width, in case passing 0 is causing the errors
161+
$final_height = round( $image->getImageHeight() * ($final_width/$img_width) );
162+
163+
try {
164+
$image->scaleImage($final_width,$final_height,true);
165+
$image->writeImage($local_output);
166+
$image->clear();
167+
$image->destroy();
168+
} catch(Exception $e){
169+
echo 'Caught exception: ', $e->getMessage(), "\n";
199170
}
200-
*/
171+
201172
}
202173

203174
//send new header for resized image
@@ -208,7 +179,7 @@
208179

209180
} else {
210181
//if missing any required paramters output a blank image
211-
showMt();
182+
showMt("missing required parameters");
212183
}
213184
} //end foreach
214185

@@ -221,7 +192,7 @@
221192
}
222193
} else {
223194
//end if, if mime type is not image
224-
showMt();
195+
showMt("incorrect mime type");
225196
}
226197

227198
//if domain is in whitelist, break out of foreach
@@ -248,22 +219,26 @@
248219

249220
//final check, if remote domain is not in whitelist
250221
if ($c>count($whitelist)) {
251-
showMt();
222+
showMt("domain not in whitelist");
252223
}
253224

254225
} else {
255226
//end if, if src is not in query
256227
//this will basically never happen currently cause htaccess does not pass through if not a proper image extension
257-
showMt();
228+
showMt("src not in query");
258229
}
259230

260231
} //end request uri check
261232

262-
function showMt(){
233+
function showMt($e){
263234
//output blank gif for cases where no there is no image
264-
header('Content-type: image/gif');
265-
readfile($GLOBALS["mt"]);
266-
exit;
235+
if($debug){
236+
header('Content-type: image/gif');
237+
readfile($GLOBALS["mt"]);
238+
exit;
239+
} else {
240+
echo $e."<br />";
241+
}
267242
}
268243

269244
?>

whitelist.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?php
22
//add domains here
3-
$whitelist = array("localhost","fejalish.com","headpoo.com");
3+
$whitelist = array("fejalish.com","headpoo.com");
44
?>

0 commit comments

Comments
 (0)