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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
|
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Image_Composite_HelioviewerCompositeImage class definition
*
* TODO: Instead of writing intermediate layers as files, store as IMagick objects.
*
* PHP version 5
*
* @category Image
* @package Helioviewer
* @author Keith Hughitt <keith.hughitt@nasa.gov>
* @author Jaclyn Beck <jaclyn.r.beck@gmail.com>
* @license http://www.mozilla.org/MPL/MPL-1.1.html Mozilla Public License 1.1
* @link http://launchpad.net/helioviewer.org
*/
require_once 'src/Image/JPEG2000/JP2Image.php';
require_once 'src/Database/ImgIndex.php';
/**
* Image_Composite_HelioviewerCompositeImage class definition
*
* PHP version 5
*
* @category Image
* @package Helioviewer
* @author Keith Hughitt <keith.hughitt@nasa.gov>
* @author Jaclyn Beck <jaclyn.r.beck@gmail.com>
* @license http://www.mozilla.org/MPL/MPL-1.1.html Mozilla Public License 1.1
* @link http://launchpad.net/helioviewer.org
*/
class Image_Composite_HelioviewerCompositeImage
{
private $_composite;
private $_dir;
private $_imageLayers;
private $_filepath;
private $_filename;
protected $compress;
protected $date;
protected $db;
protected $height;
protected $interlace;
protected $layers;
protected $roi;
protected $scale;
protected $watermark;
protected $width;
/**
* Creates a new HelioviewerCompositeImage instance
*
* @param object $layers A Helper_HelioviewerLayers object representing the requested image layers
* @param string $obsDate The date for which the composite image should be created
* @param object $roi The rectangular region of interest defining the composite image's boundaries
* @param array $options A list of optional parameters to use when creating the composite image
*
* @return void
*/
public function __construct($layers, $obsDate, $roi, $options)
{
set_time_limit(90); // Extend time limit to avoid timeouts
// Default image settings (optimized for small filesize)
$defaults = array(
'database' => false,
'watermark' => true,
'compress' => true,
'interlace' => true
);
$options = array_replace($defaults, $options);
$this->width = $roi->getPixelWidth();
$this->height = $roi->getPixelHeight();
$this->scale = $roi->imageScale();
$this->db = $options['database'] ? $options['database'] : new Database_ImgIndex();
$this->layers = $layers;
$this->date = $obsDate;
$this->roi = $roi;
$this->compress = $options['compress'];
$this->interlace = $options['interlace'];
$this->watermark = $options['watermark'];
}
/**
* Builds the composite image.
*
* TODO: Instead of writing out individual layers as files and then reading them back in simply use the IMagick
* objects directly.
*
* @return void
*/
private function _buildCompositeImageLayers()
{
$imageLayers = array();
// Find the closest image for each layer, add the layer information string to it
foreach ($this->layers->toArray() as $layer) {
$image = $this->_buildImageLayer($layer);
array_push($imageLayers, $image);
}
// Check to see if layers were created
if (empty($imageLayers)) {
throw new Exception("Unable to create layers needed for composite image");
}
return $imageLayers;
}
/**
* Builds a single layer image
*
* @param array $layer Associative array containing the layer properties
*
* @return object A HelioviewerImage instance (e.g. AIAImage or LASCOImage)
*/
private function _buildImageLayer($layer)
{
$image = $this->db->getClosestImage($this->date, $layer['sourceId']);
// Instantiate a JP2Image
$jp2Filepath = HV_JP2_DIR . $image['filepath'] . "/" . $image['filename'];
$jp2 = new Image_JPEG2000_JP2Image($jp2Filepath, $image['width'], $image['height'], $image['scale']);
$offsetX = $image['sunCenterX'] - ($image['width'] / 2);
$offsetY = ($image['height'] / 2) - $image['sunCenterY'];
// Options for individual layers
$options = array(
"date" => $image['date'],
"layeringOrder" => $layer['layeringOrder'],
"opacity" => $layer['opacity'],
"compress" => false
);
// For layers with transparent regions use PNG
$ext = $layer['layeringOrder'] > 1 ? "png" : "bmp";
// Choose a temporary filename (should never be used)
$tmpFile = $this->_dir . "/" . rand() . ".$ext";
// Choose type of image to create
if ($layer['instrument'] == "SECCHI") {
if (substr($layer['detector'], 0, 3) == "COR") {
$type = "CORImage";
} else {
$type = strtoupper($layer['detector']) . "Image";
}
} else {
$type = strtoupper($layer['instrument']) . "Image";
}
include_once "src/Image/ImageType/$type.php";
$classname = "Image_ImageType_" . $type;
return new $classname(
$jp2, $tmpFile, $this->roi, $layer['observatory'], $layer['instrument'], $layer['detector'], $layer['measurement'],
$offsetX, $offsetY, $options
);
}
/**
* Builds each image separately and then composites them together if necessary.
*
* @return string Composite image filepath
*/
private function _buildCompositeImage()
{
// Composite images on top of one another if there are multiple layers.
if (sizeOf($this->_imageLayers) > 1) {
$sortedImages = $this->_sortByLayeringOrder($this->_imageLayers);
$image = null;
foreach ($sortedImages as $layer) {
$previous = $image;
$image = $layer->getIMagickImage();
// If $previous exists, then the images need to be composited. For memory purposes,
// destroy $previous when done with it.
if ($previous) {
$image->compositeImage($previous, IMagick::COMPOSITE_DSTOVER, 0, 0);
$previous->destroy();
}
}
} else {
// For single layer images the composite image is simply the first image layer
//$imagickImage = new IMagick($this->_imageLayers[0]->getFilepath());
$image = $this->_imageLayers[0]->getIMagickImage();
}
if ($this->watermark) {
$this->_addWatermark($image);
}
$this->_finalizeImage($image, $this->_filepath);
// Store the IMagick composite image
$this->_composite = $image;
}
/**
* Finalizes image and writes it to a file
*
* @param Object $imagickImage An Imagick object
* @param String $output The filepath where the image will be written to
*
* @return void
*/
private function _finalizeImage($imagickImage, $output)
{
//set_time_limit(60); // Need to up the time limit that imagick is allowed to use to execute commands.
// Compress image
$this->_compressImage($imagickImage);
// Add comment
$comment = sprintf(
"Image created by http://helioviewer.org using data from %s near %s",
$this->layers->toHumanReadableString(),
$this->date
);
$imagickImage->commentImage($comment);
// Flatten image and write to disk
$imagickImage->setImageAlphaChannel(IMagick::ALPHACHANNEL_OPAQUE);
$imagickImage->writeImage($output);
}
/**
* Sets compression and interlacing settings for the composite image
*
* @param object $imagickImage An initialized Imagick object
*
* @return void
*/
private function _compressImage($imagickImage)
{
// Apply compression based on image type for those formats that support it
if ($this->_format === "png") {
// Set filetype
$imagickImage->setImageFormat("PNG");
// Compression type
$imagickImage->setImageCompression(IMagick::COMPRESSION_LZW);
// Compression quality
$quality = $this->compress ? PNG_HIGH_COMPRESSION : PNG_LOW_COMPRESSION;
$imagickImage->setImageCompressionQuality($quality);
// Interlacing
if ($this->interlace) {
$imagickImage->setInterlaceScheme(IMagick::INTERLACE_PLANE);
}
// Quantization
if ($this->compress) {
// Reduce the number of colors used for the image (256/layer + 512 for watermark)
$imagickImage->quantizeImage($this->layers->length() * 256 + 512,
IMagick::COLORSPACE_RGB, 0, FALSE, FALSE);
}
} elseif ($this->_format === "jpg") {
// Set filetype
$imagickImage->setImageFormat("JPG");
// Compression type
$imagickImage->setImageCompression(IMagick::COMPRESSION_JPEG);
// Compression quality
$quality = $this->compress ? JPG_HIGH_COMPRESSION : JPG_LOW_COMPRESSION;
$imagickImage->setImageCompressionQuality($quality);
// Interlacing
if ($this->interlace) {
$imagickImage->setInterlaceScheme(IMagick::INTERLACE_LINE);
}
}
$imagickImage->setImageDepth(8);
}
/**
* Composites a watermark (the date strings of the image) onto the lower left corner and the HV logo in the
* lower right corner.
*
* Layer names are added togeter as one string, and date strings are added as a separate string,
* to line them up nicely. An example string would be:
*
* -annotate +20+0 'EIT 304\nLASCO C2\n'
* and:
* -annotate +100+0 '2003-01-01 12:00\n2003-01-01 11:30\n'
*
* These two strings are then layered on top of each other and put in the southwest corner of the image.
*
* @param object $imagickImage An Imagick object
*
* @return void
*/
private function _addWatermark($imagickImage)
{
if ($this->width < 200 || $this->height < 200) {
return;
}
$watermark = new IMagick(HV_ROOT_DIR . "/api/resources/images/watermark_small_black_border.png");
// If the image is too small, use only the circle, not the url, and scale it so it fits the image.
if ($this->width / 300 < 2) {
$watermark->readImage(HV_ROOT_DIR . "/api/resources/images/watermark_circle_small_black_border.png");
$scale = ($this->width / 2) / 300;
$width = $watermark->getImageWidth();
$watermark->scaleImage($width * $scale, $width * $scale);
}
// For whatever reason, compositeImage() doesn't carry over gravity settings so the offsets must
// be relative to the top left corner of the image rather than the desired gravity.
$x = $this->width - $watermark->getImageWidth() - 10;
$y = $this->height - $watermark->getImageHeight() - 10;
$imagickImage->compositeImage($watermark, IMagick::COMPOSITE_DISSOLVE, $x, $y);
// If the image is too small, text won't fit. Don't put a date string on it.
if ($this->width > 285) {
$this->_addTimestampWatermark($imagickImage);
}
// Cleanup
$watermark->destroy();
}
/**
* Builds an imagemagick command to composite watermark text onto the image
*
* @param object $imagickImage An initialized IMagick object
*
* @return void
*/
private function _addTimestampWatermark($imagickImage)
{
$nameCmd = "";
$timeCmd = "";
$height = $imagickImage->getImageHeight();
$lowerPad = $height - 8;
// Put the names on first, then put the times on as a separate layer so the times are nicely aligned.
foreach ($this->_imageLayers as $layer) {
$lowerPad -= 12;
$nameCmd .= $layer->getWaterMarkName();
$timeCmd .= $layer->getWaterMarkDateString();
}
$black = new IMagickPixel("#000C");
$white = new IMagickPixel("white");
// Outline words in black
$underText = new IMagickDraw();
$underText->setStrokeColor($black);
$underText->setStrokeWidth(2);
$imagickImage->annotateImage($underText, 20, $lowerPad, 0, $nameCmd);
$imagickImage->annotateImage($underText, 125, $lowerPad, 0, $timeCmd);
// Write words in white over outline
$text = new IMagickDraw();
$text->setFillColor($white);
$text->setStrokeWidth(0);
$imagickImage->annotateImage($text, 20, $lowerPad, 0, $nameCmd);
$imagickImage->annotateImage($text, 125, $lowerPad, 0, $timeCmd);
// Cleanup
$black->destroy();
$white->destroy();
$underText->destroy();
$text->destroy();
}
/**
* Sorts the layers by their associated layering order
*
* Layering orders that are supported currently are 3 (C3 images), 2 (C2 images), 1 (EIT/MDI images).
* The array is sorted by increasing layeringOrder.
*
* @param array &$images Array of Composite image layers
*
* @return array Array containing the sorted image layers
*/
private function _sortByLayeringOrder(&$images)
{
$sortedImages = array();
// Array to hold any images with layering order 2 or 3.
// These images must go in the sortedImages array last because of how compositing works.
$groups = array("2" => array(), "3" => array());
// Push all layering order 1 images into the sortedImages array,
// push layering order 2 and higher into separate array.
foreach ($images as $image) {
$order = $image->getLayeringOrder();
if ($order > 1) {
array_push($groups[$order], $image);
} else {
array_push($sortedImages, $image);
}
}
// Push the group 2's and group 3's into the sortedImages array now.
foreach ($groups as $group) {
foreach ($group as $image) {
array_push($sortedImages, $image);
}
}
// return the sorted array in order of smallest layering order to largest.
return $sortedImages;
}
/**
* Builds the screenshot and saves it to the path specified
*
* @param string filepath Filepath to save the screenshot to
*
* @return void
*/
public function build($filepath)
{
$this->_filepath = $filepath;
$path_parts = pathinfo($filepath);
$this->_dir = $path_parts['dirname'];
$this->_filename = $path_parts['basename'];
$this->_format = $path_parts['extension'];
if (!file_exists($this->_dir)) {
if (!mkdir($this->_dir, 0777, true)) {
throw new Exception("Unable to create directory: ". $this->_dir);
}
}
// Build individual layers
$this->_imageLayers = $this->_buildCompositeImageLayers();
// Composite layers and create the final image
$this->_buildCompositeImage();
// Check to see if composite image was successfully created
if (!file_exists($this->_filepath)) {
throw new Exception('The requested image is either unavailable or does not exist.');
}
}
/**
* Displays the composite image
*
* @return void
*/
public function display()
{
$fileinfo = new finfo(FILEINFO_MIME);
$mimetype = $fileinfo->file($this->_filepath);
header("Content-Disposition: inline; filename=\"" . $this->_filename . "\"");
header("Content-type: " . $mimetype);
echo $this->_composite;
}
/**
* Returns the IMagick object associated with the composite image
*
* @return object IMagick object
*/
public function getIMagickImage()
{
return $this->_composite;
}
/**
* Destructor
*
* @return void
*/
public function __destruct()
{
// Destroy IMagick object
if (isset($this->_composite)) {
$this->_composite->destroy();
}
}
}
?>
|