-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.php
161 lines (131 loc) · 4.82 KB
/
run.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
152
153
154
155
156
157
158
159
160
161
<?php
require 'vendor/autoload.php';
require './extend/extendZoomify.php';
use ZoomImage\ExtendZoomify;
// Set your configuration options
$globalConfig = [
// Add your config options here
'tileLayout' => 'zoomify',
'tileSize' => 512,
'tileOverlap' => 0,
'tileQuality' => 100,
'destinationRemove' => true,
'processor' => 'ExtendVips'
];
function getFolderSizeAndFileCount($dir) {
$size = 0;
$count = 0;
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS)) as $file) {
$size += $file->getSize();
$count++;
}
return ['size' => $size, 'count' => $count];
}
function resizeImage($source, $destination, $newWidth) {
// Get the original image dimensions
list($width, $height) = getimagesize($source);
// Calculate the new height based on the aspect ratio
$newHeight = intval(($newWidth / $width) * $height);
// Create a new image with the new dimensions
$newImage = imagecreatetruecolor($newWidth, $newHeight);
// Get the image extension
$imageExtension = strtolower(pathinfo($source, PATHINFO_EXTENSION));
// Load the source image based on the extension
switch ($imageExtension) {
case 'jpeg':
case 'jpg':
$image = imagecreatefromjpeg($source);
break;
case 'png':
$image = imagecreatefrompng($source);
break;
case 'gif':
$image = imagecreatefromgif($source);
break;
default:
throw new Exception('Unsupported image format');
}
// Resize the image
imagecopyresampled($newImage, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
// Save the resized image
imagejpeg($newImage, $destination, 100);
// Free memory
imagedestroy($newImage);
imagedestroy($image);
return array(
'fullWidth' => $newWidth,
'fullHeight' => $newHeight,
'originWidth' => $width,
'originHeight' => $height,
'fullImage' => "full.jpg",
);
}
$output = array();
// Define an $images array containing the image configurations.
$images = [
[
'name' => 'pexels-max-rahubovskiy-5997992',
'source' => 'input/pexels-max-rahubovskiy-5997992.jpg',
'destination' => 'output/'.'pexels-max-rahubovskiy-5997992',
'config' => [ /* image specific configs */
"tileSize" => 512,
],
'enable' => true,
],
[
'name' => 'pexels-max-rahubovskiy-5997992',
'source' => 'input/pexels-max-rahubovskiy-5997992.jpg',
'destination' => 'output/'.'pexels-max-rahubovskiy-5997992',
'config' => [ /* image specific configs */
'tileLayout' => 'deepzoom',
],
'enable' => true,
]
];
// Set the source image path and the destination folder for the tiles
$source = __DIR__ . '/input/pexels-max-rahubovskiy-5997992.jpg';
$destination = 'output';
// Set your configuration options
$config = [
// Add your config options here
"destinationRemove" => true,
];
// Loop through the $images array and process each image
foreach ($images as $image) {
if ($image['enable']) {
// Merge global config with image specific config
$config = array_merge($globalConfig, $image['config']);
$source = $image['source'];
$destination = $image['destination'].'--'.$config['tileLayout'];
// Setup the Zoomify library.
$zoomify = new ExtendZoomify($config);
// Process the source file and save tiles in the destination folder
$result = $zoomify->process($source, $destination);
// ... rest of the code
$folderInfo = getFolderSizeAndFileCount($destination);
$folderSize = $folderInfo['size'] / 1048576; // Convert to megabytes
$fileCount = $folderInfo['count'];
// Grab the result of all images into JSON file
$fileOutput = array(
"image" => $image["name"].'--'.$config['tileLayout'],
"status" => $result,
"fileCount" => $fileCount - 1,
"folderSize" => $folderSize,
"tileSize" => $config['tileSize'],
"tileOverlap" => $config['tileOverlap'],
"format" => $config['tileLayout']
);
// Create a resized version of the image
$resizedDestination = $destination . "/full.jpg";
$imageInfo = resizeImage($source, $resizedDestination, 2500);
// Concat the imageInfo with output[]
$fileOutput = array_merge($fileOutput, $imageInfo);
$output[] = $fileOutput;
}
}
// Convert the array to a JSON string with pretty formatting
$json = json_encode($output, JSON_PRETTY_PRINT);
// Define the path to the output JSON file
$outputFile = 'output/output.json';
// Write the JSON string to the output file
file_put_contents($outputFile, $json);