function buildImage($opts) { $input = "input/2010_09_04__23_56_32_125__SDO_AIA_AIA_304.pgm"; $clut = "input/SDO_AIA_304.png"; $image = new IMagick($input); if ($opts['use_gd']) { // Format if ($opts['inter_format']) { $image->setImageFormat($opts['inter_format']); } // Bit-depth (intermediate image) if ($opts['depth']) { $image->setImageDepth($opts['depth']); } // Image type if ($opts['type']) { $image->setImageType(constant($opts['type'])); } // Compression quality if ($opts['input_quality'] || $opts['input_quality'] === 0) { $image->setImageCompressionQuality($opts['input_quality']); } $intermediate = $image->getimageblob(); if ($opts['base64']) { $intermediate = base64_encode($intermediate); } $image->destroy(); $coloredImage = setColorPalette($intermediate, $clut, $opts['base64']); if ($opts['base64']) { $coloredImage = base64_decode($coloredImage); } $image = new IMagick(); $image->readimageblob($coloredImage); } else { $image->clutImage(new Imagick($clut)); } if (strtolower($opts["output_format"]) == "png") { // Compression type if ($opts['compression']) { $image->setImageCompression(constant($opts['compression'])); } // Interlacing if ($opts['interlace']) { $image->setInterlaceScheme(constant($opts['interlace'])); } // Compression quality if ($opts['output_quality'] || $opts['output_quality'] === 0) { $image->setImageCompressionQuality($opts['output_quality']); } } // Bit-depth (final image) if ($opts['depth']) { $image->setImageDepth($opts['depth']); } $image->writeImage("final." . strtolower($opts["output_format"])); $image->destroy(); }
function upload() { // Default settings/restrictions $config['upload_path'] = $this->directory; $config['allowed_types'] = 'gif|jpg|png|jpeg'; $config['max_size'] = min(maxUploadSizeBytes() / 1024, self::MAX_FILE_SIZE_KB); $config['max_width'] = '0'; // no restriction (image will be resized anyway!) $config['max_height'] = '0'; $config['max_filename'] = self::MAX_FILE_NAME_LEN; $this->load->library('upload', $config); // Try and upload the file using the settings from __construct() if (!$this->upload->do_upload('new_file')) { // Abort upload and display errors, if any $data['error_message'] = $this->upload->display_errors(); $data['recent_uploads'] = $this->getRecentUploads(); $data['title'] = "Image Uploader"; $data['js_lib'] = array('core'); $this->load->view('uploader', $data); } else { // Create the compressed copy of the image based on a hash of the uploaded filename $upload_result = $this->upload->data(); $image = new Imagick($this->directory . $upload_result['file_name']); $image->setBackgroundColor(new ImagickPixel('white')); // Create the optimised image // "bestfit" param will ensure that the image is downscaled proportionally if needed // if ($image->getImageWidth() >= $image->getImageHeight() // && $image->getImageWidth() > self::IMAGE_LANDSCAPE_WIDTH || $image->getImageHeight() > self::IMAGE_LANDSCAPE_HEIGHT) // { // $image->resizeImage(self::IMAGE_LANDSCAPE_WIDTH,self::IMAGE_LANDSCAPE_HEIGHT, Imagick::FILTER_LANCZOS, 1, true); // } // else if ($image->getImageWidth() > self::IMAGE_PORTRAIT_WIDTH || $image->getImageHeight() > self::IMAGE_PORTRAIT_HEIGHT) // { // $image->resizeImage(self::IMAGE_PORTRAIT_WIDTH,self::IMAGE_PORTRAIT_HEIGHT, Imagick::FILTER_LANCZOS, 1, true); // } $flattened = new IMagick(); $flattened->newImage($image->getImageWidth(), $image->getImageHeight(), new ImagickPixel("white")); $flattened->compositeImage($image, imagick::COMPOSITE_OVER, 0, 0); $flattened->setImageFormat("jpg"); $flattened->setImageCompression(Imagick::COMPRESSION_JPEG); // $flattened->setCompression(Imagick::COMPRESSION_JPEG); // $flattened->setCompressionQuality(self::COMPRESSION_PERCENTAGE); $flattened->writeImage($this->directory . 'img_' . md5($upload_result['file_name']) . ".jpg"); $flattened->clear(); $flattened->destroy(); $image->clear(); $image->destroy(); $data['success_message'] = "File successfully uploaded!"; $data['recent_uploads'] = $this->getRecentUploads(); $data['title'] = "Image Uploader"; $data['js_lib'] = array('core'); $data['max_filesize'] = $config['max_size']; $this->load->view('uploader', $data); } }
<?php $width = intval($_GET['width']); $height = intval($_GET['height']); if (!(is_int($width) and is_int($height))) { exit($height); } $width = 1440; $height = 900; $image = new IMagick('media/images/menu.png'); $geo = $image->getImageGeometry(); $image->scaleimage(round($width * $geo['width'] / 1920), round($height * $geo['height'] / 1080)); $image->setImageFormat('png'); header('Content-Type: image/png'); exit($image);
/** * 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; } }