Ejemplo n.º 1
0
 public function processOperations(Image $input_image, Image $output_image = null)
 {
     $command_line = $this->getImageMagickConvert();
     $this->temp_input_filename = $this->getTempFilename('input_');
     file_put_contents($this->temp_input_filename, $input_image->getImageData());
     // Check for a SequenceNumber or Density operation.  They're a little special.
     foreach ($this->operations as $index => $operation) {
         if ($operation instanceof SequenceNumber) {
             $frame = (int) $operation->getValue();
             if (!$frame) {
                 throw new ImageMagickException('Frame was not an integer');
             }
             $this->temp_input_filename .= '[' . $frame . ']';
             unset($this->operations[$index]);
         }
         if ($operation instanceof Density) {
             $density = (int) $operation->getValue();
             if (!$density) {
                 throw new ImageMagickException('Density was not an integer');
             }
             $command_line .= ' -density ' . $density;
             unset($this->operations[$index]);
         }
     }
     $command_line .= ' ' . escapeshellarg($this->temp_input_filename) . ' ';
     foreach ($this->operations as $operation) {
         if ($operation instanceof InstantOperation) {
             return $operation->process($input_image, $command_line);
         }
         $command_line = $operation->process($input_image, $command_line)->getCommandLine() . ' ';
     }
     $temp_output_filename = '';
     if ($output_image) {
         $temp_output_filename = $this->getTempFilename('output_');
         $command_line .= $this->getOutputFileWithFormat($temp_output_filename);
     }
     // Remove extra whitespace between operations
     $command_line = preg_replace('/\\s+/', ' ', $command_line);
     // Check for a no-op.  Don't lose fidelity by converting it to itself
     // Also check to make sure it hasn't been forced.  We assume that the Convert operation wouldn't set the format if it didn't have to.
     if (!$this->output_format_is_forced && trim($command_line) == $this->getImageMagickConvert() . ' ' . escapeshellarg($this->temp_input_filename) . ' ' . $this->getOutputFileWithFormat($temp_output_filename)) {
         copy($this->temp_input_filename, $temp_output_filename);
         $output = array('No-op; Copied image');
         $status = 0;
     } else {
         exec($command_line, $output, $status);
     }
     if ($status) {
         throw new ImageMagickException('Error executing ImageMagick: ' . $status);
     }
     if ($output_image && $this->temp_input_filename) {
         $output_image->setImageData(file_get_contents($temp_output_filename));
     }
     return implode("\n", $output);
 }
Ejemplo n.º 2
0
 public function process(Image $image, $command_line = '')
 {
     $status = 0;
     $output = array();
     $temp_file = $this->processor->getTempFilename('info_');
     file_put_contents($temp_file, $image->getImageData());
     exec($this->processor->getImageMagickConvert() . ' -density 120 ' . escapeshellarg($temp_file) . ' -format "%w,%h,%z,%m,%Q" info:', $output, $status);
     $result = new Result();
     $result->setCommandLine($command_line);
     if (!$status) {
         $size_arr = explode(',', $output[0]);
         $result->setExtra(array('width' => $size_arr[0], 'height' => $size_arr[1], 'depth' => $size_arr[2], 'type' => $size_arr[3], 'quality' => $size_arr[4]));
     }
     return $result;
 }
Ejemplo n.º 3
0
 public function process(Image $image, $command_line = '')
 {
     if ($this->mode & CommonOptions::MODE_FILL_AREA_OR_FIT) {
         // Both...so if it's larger, shrink it to fill the area.
         // Otherwise, fit it.
         // What's my current width & height?
         $current_image_filename = $this->processor->getTempFilename('current_');
         $output_filename = $this->processor->getTempFilename('resize_temp_');
         file_put_contents($current_image_filename, $image->getImageData());
         // 0 => width, 1 => height
         $current_image_data = getimagesize($current_image_filename);
         exec($command_line . ' ' . escapeshellarg($output_filename));
         // 0 => width, 1 => height
         $image_data = getimagesize($output_filename);
         // Image is smaller in some dimension.  Follow the "Only shrink larger" directive, just fit instead.
         if ($image_data[0] < $current_image_data[0] || $image_data[1] < $current_image_data[1]) {
             $this->mode = 0;
         } elseif ($this->width == $image_data[0] && $this->height == $image_data[1]) {
             // The image is the exact same size.  Don't lose fidelity by resizing it.
             $result = new Result();
             $result->setCommandLine($command_line);
             return $result;
         } else {
             $this->mode = CommonOptions::MODE_FILL_AREA;
         }
     }
     if ($this->mode & CommonOptions::MODE_ONLY_SHRINK_LARGER) {
         $command_line .= ' -gravity ' . escapeshellarg($this->gravity);
         $command_line .= ' -thumbnail ' . escapeshellarg($this->width . 'x' . $this->height . '>');
     } elseif ($this->mode & CommonOptions::MODE_FILL_AREA) {
         $command_line .= ' -gravity ' . escapeshellarg($this->gravity);
         $command_line .= ' -thumbnail ' . escapeshellarg($this->width . 'x' . $this->height . '^');
     } elseif ($this->mode & CommonOptions::MODE_RESIZE_ABSOLUTE) {
         $command_line .= ' -gravity ' . escapeshellarg($this->gravity);
         $command_line .= ' -thumbnail ' . escapeshellarg($this->width . 'x' . $this->height . '!');
     } else {
         $command_line .= ' -gravity ' . escapeshellarg($this->gravity);
         $command_line .= ' -thumbnail ' . escapeshellarg($this->width . 'x' . $this->height);
     }
     $result = new Result();
     $result->setCommandLine($command_line);
     return $result;
 }
Ejemplo n.º 4
0
 public function process(Image $image, $command_line = '')
 {
     if (!$this->compare_to) {
         throw new ImageMagickException('No image to compare to!');
     }
     $output_file = $this->processor->getTempFilename('output_');
     $left_file = $this->processor->getTempFilename('left_');
     $right_file = $this->processor->getTempFilename('right_');
     if ($this->use_current) {
         $status = 0;
         $output = array();
         // Use the current queue, not the given file.
         exec($command_line . ' ' . $left_file, $output, $status);
         if ($status) {
             throw new ImageMagickException('Could not compare images: ' . implode("\n", $output));
         }
     } else {
         file_put_contents($left_file, $image->getImageData());
     }
     file_put_contents($right_file, $this->compare_to->getImageData());
     $status = 0;
     $output = array();
     exec($this->processor->getImageMagickPath() . DIRECTORY_SEPARATOR . 'compare -metric PSNR ' . escapeshellarg($left_file) . ' ' . escapeshellarg($right_file) . ' ' . ' -quality 100 ' . escapeshellarg($output_file) . ' 2>&1', $output, $status);
     if ($status == 1 && $this->processor->hasImageMagickQuirk(Processor::QUIRK_RESULT_IS_ONE_WHEN_COMPARE_FAILS)) {
         // $status > 1 means something else failed.  1 means the images weren't exactly the same.
         $status = 0;
     }
     if ($status == 0) {
         if (stripos($output[0], 'inf') !== false) {
             // IM sometimes returns 1.#INF instead of "inf".  Normalize it to what we expect.
             $output[0] = 'inf';
         }
     }
     if ($status) {
         throw new ImageMagickException('Could not compare images: (' . $status . ') ' . implode("\n", $output));
     }
     $result = new Result();
     $result->setCommandLine($command_line);
     if (!$status) {
         $result->setImage(new File($output_file))->setExtra(implode(PHP_EOL, $output));
     }
     return $result;
 }