-
Notifications
You must be signed in to change notification settings - Fork 1
/
imthumb-source-local.class.php
74 lines (61 loc) · 1.54 KB
/
imthumb-source-local.class.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
<?php
/**
* Default image loader, for local filesystem
*
* @package ImThumb
* @author Sam Pospischil <[email protected]>
* @since 2014-04-24
*/
class ImThumbSource_Local implements ImThumbSource
{
public function readMetadata($src, ImThumb $requestor)
{
$fallbackMeta = new ImThumbMeta();
$src = $this->getRealImagePath($src, $requestor->param('baseDir'));
$mtime = @filemtime($src);
if (false === $mtime) {
return $fallbackMeta->invalid();
}
$fileSize = @filesize($src);
$sData = @getimagesize($src); // ensure it's an image
if (!$sData) {
return $fallbackMeta->invalid();
}
$mimeType = strtolower($sData['mime']);
if(!preg_match('/^image\//i', $mimeType)) {
$mimeType = 'image/' . $mimeType;
}
if ($mimeType == 'image/jpg') {
$mimeType = 'image/jpeg';
}
return new ImThumbMeta(
$src,
$mtime,
$fileSize,
$mimeType
);
}
public function readResource(ImThumbMeta $meta, ImThumb $requestor)
{
if (!$meta->valid) {
return null;
}
$target = new Imagick();
$target->readImage($meta->src);
return $target;
}
//--------------------------------------------------------------------------
protected function getRealImagePath($src, $baseDir = null)
{
if ($baseDir) {
if (preg_match('@^' . preg_quote($baseDir, '@') . '@', $src)) {
return $src;
}
return realpath($baseDir . '/' . $src);
}
if (!class_exists('ImThumbRequestHandler')) {
require_once(IMTHUMB_BASE . '/imthumb-requesthandler.class.php');
}
return realpath(ImThumbRequestHandler::getDocRoot() . $src);
}
}