-
Notifications
You must be signed in to change notification settings - Fork 0
/
iiif-simple.php
executable file
·124 lines (116 loc) · 4.37 KB
/
iiif-simple.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
<?php
/** @prefix : <http://purl.org/net/ns/doas#> . <> a :PHPScript;
:title "A Simple IIIF Service";
:created "2017-01-19";
:creator <http://purl.org/net/who/kanzaki#masahide> ;
:release [:revision "0.41"; :created "2017-01-25"];
:dependencies "requires PHP GD extension" ;
:description """A minimum IIIF service for JPEG pictures without predefined tiles. Because all tiles are generated on the fly, the performance will not be efficient as a large scale public service. Maybe useful for local testing purpose.
- define IMG_ROOT (relative path to JPEG image directory)
- prepare manifest file where service@id is set like
"@id": "http://example.org/{path to this script}/{image path from IMG_ROOT}",
- the above manifest can be generated by the acompanying manifest-generator.php
""".
*/
// give the relative path to JPEG image directory, e.g. "../../2016/pub/images/"
define("IMG_ROOT", "images/");
$path = $_SERVER["PATH_INFO"];
if(preg_match("!^/(.*?\.jpg)(/.*)?$!", $path, $m)){
if(!($imgwh = @getimagesize(IMG_ROOT . $m[1]))) report_error("404");
if(!$m[2]){
gen_image($m[1], $imgwh, array(0, 0, $imgwh[0], $imgwh[1]), $imgwh);
}elseif($m[2] == "/info.json"){
gen_infojson($imgwh);
}elseif(preg_match("!^/info.json(/.*jpg)$!", $m[2], $mm)){
gen_clipimg($m[1], $mm[1], $imgwh);
}else{
gen_clipimg($m[1], $m[2], $imgwh);
}
}else{
if(array_pop(preg_split("![/:\\\\]!", __FILE__)) == array_pop(explode("/", $_SERVER["SCRIPT_URI"]))){
report_error("404");
}else{
//manifest-generator.php;
}
}
//generates clipped and resized image as IIIF service
function gen_clipimg($imgfile, $param, $imgwh){
$pm = explode("/", $param);
$clip = calc_region($pm[1], $imgwh);
$size = calc_size($pm[2], $clip, $imgwh);
//ignores rotate and quality params
gen_image($imgfile, $imgwh, $clip, $size);
}
//calc the clip region of the original image
function calc_region($regionp, $imgwh){
if($regionp == "full"){
$clip = array(0, 0, $imgwh[0], $imgwh[1]);
}elseif(preg_match("/^(\d+),(\d+),(\d+),(\d+)$/", $regionp, $m)){
$clip = array_slice($m, 1);
}else{
report_error("400", "wrong region params", $regionp);
}
return $clip;
}
//calc the output image size
function calc_size($sizep, $clip, $imgwh){
if($sizep == "full" or $sizep == "max"){
$newsize = array($clip[2], $clip[3]);
}elseif(preg_match("/^pct:([\d\.]+)$/", $sizep, $m)){
$newsize = array(round($clip[2] * $m[1] / 100), round($clip[3] * $m[1] / 100));
}elseif(preg_match("/^(\d+)?,(\d+)?$/", $sizep, $m)){
$newsize = array($m[1], $m[2]);
if(!$newsize[0]){
$newsize[0] = round($clip[2] * $newsize[1] / $clip[3]);
}elseif(!$newsize[1]){
$newsize[1] = round($clip[3] * $newsize[0] / $clip[2]);
}
}else{
report_error("400", "wrong size params", $sizep);
}
return $newsize;
}
//generates the output image and returns with header
function gen_image($imgfile, $imgwh, $clip, $size){
if(!($srcimg = imagecreatefromjpeg(IMG_ROOT . $imgfile))) report_error("500", "image source creation error");
if(!($newimg = imagecreatetruecolor($size[0], $size[1]))) report_error("500", "image creation error", "($size[0], $size[1])");
if(!(imagecopyresampled($newimg, $srcimg,
0, 0, //destination x, y
$clip[0], $clip[1], //source x, y
$size[0], $size[1], //destination w, h
$clip[2], $clip[3] //source w, h
))) report_error("500", "image clip error", print_r($clip, true));
header("content-type: image/jpeg");
imagejpeg($newimg);
}
//generates IIIF service info.json and returns with appropriate headers
function gen_infojson($imgwh){
$service = $_SERVER["SCRIPT_URI"];
header("Access-Control-Allow-Origin: *");
header("content-type: application/json;charset=utf-8");
print <<<EOF
{
"@context" : "http://iiif.io/api/image/2/context.json",
"@id" : "$service",
"protocol" : "http://iiif.io/api/image",
"width" : $imgwh[0],
"height" : $imgwh[1],
"tiles": [{"width" : $imgwh[0], "scaleFactors" : [1]}],
"profile" : ["http://iiif.io/api/image/2/level1.json"]
}
EOF;
}
//error responses
function report_error($code, $msg="", $input=""){
$errmsg = array(
"400"=>"Bad request - invalid parameter",
"404"=>"Not found",
"500"=>"Internal server error",
);
header("HTTP/1.0 $code");
print $errmsg[$code].": ".(
$msg ? $msg.($input ? " ".htmlspecialchars($input) : "") :
$_SERVER["REQUEST_URI"]
) ;
exit;
}