/** * Builds the requested subfield image. * * Normalizing request & native image scales: * * When comparing the requested or "desired" image scale for the subfield * image to the native or "actual" image scale of the source image, it is * convenient to create a variable called "desiredToActual" which * represents the ratio of the desired scale to the actual scale. * * There are three possible cases which may occur: * * 1) desiredToActual = 1 * * In this case the subfield requested is at the natural image * scale. No resizing is necessary. * * 2) desiredToActual < 1 * * The subfield requested is at a lower image scale (HIGHER * quality) than the source JP2. * * 3) desiredToActual > 1 * * The subfield requested is at a higher image scale (LOWER * quality) than the source JP2. * * @TODO: Normalize quality scale. * @TODO: Create a cleanup array with names of files to be wiped after * processing is complete? * @TODO: Move generation of intermediate file to separate method * * @return void */ protected function build() { /* * Need to extend the time limit that writeImage() can use so it * doesn't throw fatal errors when movie frames are being made. * It seems that even if this particular instance of writeImage * doesn't take the full time frame, if several instances of it are * running PHP will complain. */ set_time_limit(600); try { // Choose extension to convert source image to if ($this->options['palettedJP2']) { $extension = '.bmp'; } else { $extension = '.pgm'; } $input = substr($this->outputFile, 0, -4) . rand() . $extension; // Extract region (PGM) $this->jp2->extractRegion($input, $this->imageSubRegion, $this->reduce); // Apply colormap if needed if (!$this->options['palettedJP2']) { // Convert to GD-readable format $grayscale = new IMagick($input); if (isset($this->options['verifyGrayscale']) && $this->options['verifyGrayscale'] && $grayscale->getImageType() != imagick::IMGTYPE_GRAYSCALE) { $this->colorTable = false; } $grayscale->setImageFormat('PNG'); $grayscale->setImageDepth(8); // Fastest PNG compression setting $grayscale->setImageCompressionQuality(10); // Assume that no color table is needed $coloredImage = $grayscale; // Apply color table if one exists if ($this->colorTable) { $clut = new IMagick($this->colorTable); $coloredImage->clutImage($clut); } } else { $coloredImage = new IMagick($input); } // Set alpha channel for images with transparent components $this->setAlphaChannel($coloredImage); // Apply compression and interlacing $this->compressImage($coloredImage); // Resize extracted image to correct size before padding. $rescaleBlurFactor = 0.6; $coloredImage->resizeImage(round($this->subfieldRelWidth), round($this->subfieldRelHeight), $this->imageOptions['rescale'], $rescaleBlurFactor); $coloredImage->setImageBackgroundColor('transparent'); // Places the current image on a larger field of black if the final // image is larger than this one $imagickVersion = $coloredImage->getVersion(); if ($imagickVersion['versionNumber'] > IMAGE_MAGICK_662_VERSION_NUM) { // ImageMagick 6.6.2-6 and higher // Problematic change occurred in revision 6.6.4-2 // See: http://www.imagemagick.org/script/changelog.php $coloredImage->extentImage($this->padding['width'], $this->padding['height'], $this->padding['offsetX'], $this->padding['offsetY']); } else { // Imagick 3.0 and lower $coloredImage->extentImage($this->padding['width'], $this->padding['height'], -$this->padding['offsetX'], -$this->padding['offsetY']); } $this->image = $coloredImage; // Check for PGM before deleting just in case another process // already removed it if (@file_exists($input)) { @unlink($input); } } catch (Exception $e) { // Clean-up intermediate files $this->_abort($this->outputFile); throw $e; } }