-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathfunction.resize.php
151 lines (128 loc) · 5.72 KB
/
function.resize.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
/**
* function by Wes Edling .. http://joedesigns.com
* feel free to use this in any project, i just ask for a credit in the source code.
* a link back to my site would be nice too.
*
*
* Changes:
* 2012/01/30 - David Goodwin - call escapeshellarg on parameters going into the shell
* 2012/07/12 - Whizzkid - Added support for encoded image urls and images on ssl secured servers [https://]
*/
/**
* SECURITY:
* It's a bad idea to allow user supplied data to become the path for the image you wish to retrieve, as this allows them
* to download nearly anything to your server. If you must do this, it's strongly advised that you put a .htaccess file
* in the cache directory containing something like the following :
* <code>php_flag engine off</code>
* to at least stop arbitrary code execution. You can deal with any copyright infringement issues yourself :)
*/
/**
* @param string $imagePath - either a local absolute/relative path, or a remote URL (e.g. http://...flickr.com/.../ ). See SECURITY note above.
* @param array $opts (w(pixels), h(pixels), crop(boolean), scale(boolean), thumbnail(boolean), maxOnly(boolean), canvas-color(#abcabc), output-filename(string), cache_http_minutes(int))
* @return new URL for resized image.
*/
function resize($imagePath,$opts=null){
$imagePath = urldecode($imagePath);
# start configuration
$cacheFolder = './cache/'; # path to your cache folder, must be writeable by web server
$remoteFolder = $cacheFolder.'remote/'; # path to the folder you wish to download remote images into
$defaults = array('crop' => false, 'scale' => 'false', 'thumbnail' => false, 'maxOnly' => false,
'canvas-color' => 'transparent', 'output-filename' => false,
'cacheFolder' => $cacheFolder, 'remoteFolder' => $remoteFolder, 'quality' => 90, 'cache_http_minutes' => 20);
$opts = array_merge($defaults, $opts);
$cacheFolder = $opts['cacheFolder'];
$remoteFolder = $opts['remoteFolder'];
$path_to_convert = 'convert'; # this could be something like /usr/bin/convert or /opt/local/share/bin/convert
## you shouldn't need to configure anything else beyond this point
$purl = parse_url($imagePath);
$finfo = pathinfo($imagePath);
$ext = $finfo['extension'];
# check for remote image..
if(isset($purl['scheme']) && ($purl['scheme'] == 'http' || $purl['scheme'] == 'https')):
# grab the image, and cache it so we have something to work with..
list($filename) = explode('?',$finfo['basename']);
$local_filepath = $remoteFolder.$filename;
$download_image = true;
if(file_exists($local_filepath)):
if(filemtime($local_filepath) < strtotime('+'.$opts['cache_http_minutes'].' minutes')):
$download_image = false;
endif;
endif;
if($download_image == true):
$img = file_get_contents($imagePath);
file_put_contents($local_filepath,$img);
endif;
$imagePath = $local_filepath;
endif;
if(file_exists($imagePath) == false):
$imagePath = $_SERVER['DOCUMENT_ROOT'].$imagePath;
if(file_exists($imagePath) == false):
return 'image not found';
endif;
endif;
if(isset($opts['w'])): $w = $opts['w']; endif;
if(isset($opts['h'])): $h = $opts['h']; endif;
$filename = md5_file($imagePath);
// If the user has requested an explicit output-filename, do not use the cache directory.
if(false !== $opts['output-filename']) :
$newPath = $opts['output-filename'];
else:
if(!empty($w) and !empty($h)):
$newPath = $cacheFolder.$filename.'_w'.$w.'_h'.$h.(isset($opts['crop']) && $opts['crop'] == true ? "_cp" : "").(isset($opts['scale']) && $opts['scale'] == true ? "_sc" : "").'.'.$ext;
elseif(!empty($w)):
$newPath = $cacheFolder.$filename.'_w'.$w.'.'.$ext;
elseif(!empty($h)):
$newPath = $cacheFolder.$filename.'_h'.$h.'.'.$ext;
else:
return false;
endif;
endif;
$create = true;
if(file_exists($newPath) == true):
$create = false;
$origFileTime = date("YmdHis",filemtime($imagePath));
$newFileTime = date("YmdHis",filemtime($newPath));
if($newFileTime < $origFileTime): # Not using $opts['expire-time'] ??
$create = true;
endif;
endif;
if($create == true):
if(!empty($w) and !empty($h)):
list($width,$height) = getimagesize($imagePath);
$resize = $w;
if($width > $height):
$resize = $w;
if(true === $opts['crop']):
$resize = "x".$h;
endif;
else:
$resize = "x".$h;
if(true === $opts['crop']):
$resize = $w;
endif;
endif;
if(true === $opts['scale']):
$cmd = $path_to_convert ." ". escapeshellarg($imagePath) ." -resize ". escapeshellarg($resize) .
" -quality ". escapeshellarg($opts['quality']) . " " . escapeshellarg($newPath);
else:
$cmd = $path_to_convert." ". escapeshellarg($imagePath) ." -resize ". escapeshellarg($resize) .
" -size ". escapeshellarg($w ."x". $h) .
" xc:". escapeshellarg($opts['canvas-color']) .
" +swap -gravity center -composite -quality ". escapeshellarg($opts['quality'])." ".escapeshellarg($newPath);
endif;
else:
$cmd = $path_to_convert." " . escapeshellarg($imagePath) .
" -thumbnail ". (!empty($h) ? 'x':'') . $w ."".
(isset($opts['maxOnly']) && $opts['maxOnly'] == true ? "\>" : "") .
" -quality ". escapeshellarg($opts['quality']) ." ". escapeshellarg($newPath);
endif;
$c = exec($cmd, $output, $return_code);
if($return_code != 0) {
error_log("Tried to execute : $cmd, return code: $return_code, output: " . print_r($output, true));
return false;
}
endif;
# return cache file path
return str_replace($_SERVER['DOCUMENT_ROOT'],'',$newPath);
}