Since: 3.5.0
Inheritance: extends WP_Image_Editor
 /**
  * It's expected that we can't save image uses imagick built in, as
  * the undlaying system library can't write to the "s3://" filesystem.
  *
  */
 public function test_save_image_with_inbuilt_fails()
 {
     $upload_dir = wp_upload_dir();
     $path = $upload_dir['basedir'] . '/canola.jpg';
     copy($this->image_path, $path);
     $image_editor = new WP_Image_Editor_Imagick($path);
     $image_editor->load();
     $status = $image_editor->save($upload_dir['basedir'] . '/canola-100x100.jpg');
     $this->assertWPError($status);
 }
 /**
  * @return bool Whether WP_Image_Editor_Imagick can be used on this system.
  */
 public static function isImagickAvailable()
 {
     static $ret = null;
     if (is_null($ret)) {
         $ret = WP_Image_Editor_Imagick::test();
     }
     return $ret;
 }
 /**
  * Whether the current environment is configured with required methods.
  *
  * @since 1.0.0
  *
  * @param  array $args
  * @return bool
  */
 public static function test($args = array())
 {
     if (!parent::test($args)) {
         return false;
     }
     $required_methods = array('clutimage', 'newpseudoimage', 'transformimagecolorspace');
     if (array_diff($required_methods, get_class_methods('Imagick'))) {
         return false;
     }
     return true;
 }
 /**
  * Crops Image.
  *
  * @since 3.5.0
  * @access public
  *
  * @param string|int $src The source file or Attachment ID.
  * @param int $src_x The start x position to crop from.
  * @param int $src_y The start y position to crop from.
  * @param int $src_w The width to crop.
  * @param int $src_h The height to crop.
  * @param int $dst_w Optional. The destination width.
  * @param int $dst_h Optional. The destination height.
  * @param boolean $src_abs Optional. If the source crop points are absolute.
  * @return boolean|WP_Error
  */
 public function crop($src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false)
 {
     $ar = $src_w / $src_h;
     $dst_ar = $dst_w / $dst_h;
     if (isset($_GET['pte-fit-crop-color']) && abs($ar - $dst_ar) > 0.01) {
         PteLogger::debug(sprintf("IMAGICK - AR: '%f'\tOAR: '%f'", $ar, $dst_ar));
         // Crop the image to the correct aspect ratio...
         if ($dst_ar > $ar) {
             // constrain to the dst_h
             $tmp_dst_h = $dst_h;
             $tmp_dst_w = $dst_h * $ar;
             $tmp_dst_y = 0;
             $tmp_dst_x = $dst_w / 2 - $tmp_dst_w / 2;
         } else {
             $tmp_dst_w = $dst_w;
             $tmp_dst_h = $dst_w / $ar;
             $tmp_dst_x = 0;
             $tmp_dst_y = $dst_h / 2 - $tmp_dst_h / 2;
         }
         //$color = this::getImagickPixel( $_GET['pte-fit-crop-color'] );
         if (preg_match("/^#[a-fA-F0-9]{6}\$/", $_GET['pte-fit-crop-color'])) {
             $color = new ImagickPixel($_GET['pte-fit-crop-color']);
         }
         //else {
         //    PteLogger::debug( "setting transparent/white" );
         //    $color = new ImagickPixel( 'white' );
         //    //$color->setColorValue( Imagick::COLOR_ALPHA, 0 );
         //}
         try {
             // crop the original image
             $this->image->cropImage($src_w, $src_h, $src_x, $src_y);
             $this->image->scaleImage($tmp_dst_w, $tmp_dst_h);
             // Create a new image and then compose the old one onto it.
             $img = new Imagick();
             $img->newImage($dst_w, $dst_h, isset($color) ? $color : 'white');
             $img->setImageFormat($this->image->getImageFormat());
             if (!isset($color)) {
                 $img->setImageOpacity(0.0);
             }
             $img->compositeImage($this->image, Imagick::COMPOSITE_DEFAULT, $tmp_dst_x, $tmp_dst_y);
             $img->flattenImages();
             $this->image = $img;
         } catch (Exception $e) {
             return new WP_Error('image_crop_error', __('Image crop failed.'), $this->file);
         }
         return $this->update_size();
     }
     return parent::crop($src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs);
 }
 /**
  * Crops Image.
  *
  * @since 3.5.0
  * @access public
  *
  * @param string|int $src The source file or Attachment ID.
  * @param int $src_x The start x position to crop from.
  * @param int $src_y The start y position to crop from.
  * @param int $src_w The width to crop.
  * @param int $src_h The height to crop.
  * @param int $dst_w Optional. The destination width.
  * @param int $dst_h Optional. The destination height.
  * @param boolean $src_abs Optional. If the source crop points are absolute.
  * @return boolean|WP_Error
  */
 public function crop($src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false)
 {
     if (pte_is_crop_border_enabled($src_w, $src_h, $dst_w, $dst_h)) {
         // Crop the image to the correct aspect ratio...
         $ar = $src_w / $src_h;
         $dst_ar = $dst_w / $dst_h;
         if ($dst_ar > $ar) {
             // constrain to the dst_h
             $tmp_dst_h = $dst_h;
             $tmp_dst_w = $dst_h * $ar;
             $tmp_dst_y = 0;
             $tmp_dst_x = $dst_w / 2 - $tmp_dst_w / 2;
         } else {
             $tmp_dst_w = $dst_w;
             $tmp_dst_h = $dst_w / $ar;
             $tmp_dst_x = 0;
             $tmp_dst_y = $dst_h / 2 - $tmp_dst_h / 2;
         }
         if (pte_is_crop_border_opaque()) {
             $color = new ImagickPixel($_GET['pte-fit-crop-color']);
         }
         try {
             // crop the original image
             $this->image->cropImage($src_w, $src_h, $src_x, $src_y);
             $this->image->scaleImage($tmp_dst_w, $tmp_dst_h);
             // Create a new image and then compose the old one onto it.
             $img = new Imagick();
             $img->newImage($dst_w, $dst_h, isset($color) ? $color : 'white');
             $img->setImageFormat($this->image->getImageFormat());
             if (!isset($color)) {
                 $img->setImageOpacity(0.0);
             }
             $img->compositeImage($this->image, Imagick::COMPOSITE_DEFAULT, $tmp_dst_x, $tmp_dst_y);
             $img->flattenImages();
             $this->image = $img;
         } catch (Exception $e) {
             return new WP_Error('image_crop_error', __('Image crop failed.'), $this->file);
         }
         return $this->update_size();
     }
     return parent::crop($src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs);
 }
 /**
  * Loads the filepath into Imagick object.
  */
 public function load()
 {
     $ret = parent::load();
     // set correct page number
     if (!is_null($this->pg) && !is_wp_error($ret) && is_callable(array($this->image, 'setIteratorIndex'))) {
         $err = __('Failed to set Imagick page number', 'document-gallery');
         // setIteratorIndex() should return false on failure, but I've found
         // reports of it throwing an error so handling both cases.
         // NOTE: I've also seen it fail and return true, so we may not
         // log anything on failure...
         try {
             if (!$this->image->setIteratorIndex($this->pg)) {
                 DG_Logger::writeLog(DG_LogLevel::Error, $err . '.');
             }
         } catch (Exception $e) {
             DG_Logger::writeLog(DG_LogLevel::Error, $err . ': ' . $e->getMessage());
         }
     }
     return $ret;
 }
 /**
  * Imagick by default can't handle s3:// paths
  * for saving images. We have instead save it to a file file,
  * then copy it to the s3:// path as a workaround.
  */
 protected function _save($image, $filename = null, $mime_type = null)
 {
     list($filename, $extension, $mime_type) = $this->get_output_format($filename, $mime_type);
     if (!$filename) {
         $filename = $this->generate_filename(null, null, $extension);
     }
     $upload_dir = wp_upload_dir();
     if (strpos($filename, $upload_dir['basedir']) === 0) {
         $temp_filename = tempnam(get_temp_dir(), 's3-uploads');
     }
     $save = parent::_save($image, $temp_filename, $mime_type);
     if (is_wp_error($save)) {
         return $save;
     }
     $copy_result = copy($save['path'], $filename);
     unlink($save['path']);
     if (!$copy_result) {
         return new WP_Error('unable-to-copy-to-s3', 'Unable to copy the temp image to S3');
     }
     return array('path' => $filename, 'file' => wp_basename(apply_filters('image_make_intermediate_size', $filename)), 'width' => $this->size['width'], 'height' => $this->size['height'], 'mime-type' => $mime_type);
 }
 /**
  *
  * @ticket 30596
  */
 public function test_image_preserves_alpha_on_rotate()
 {
     $file = DIR_TESTDATA . '/images/transparent.png';
     $pre_rotate_editor = new Imagick($file);
     $pre_rotate_pixel = $pre_rotate_editor->getImagePixelColor(0, 0);
     $pre_rotate_alpha = $pre_rotate_pixel->getColorValue(imagick::COLOR_ALPHA);
     $save_to_file = tempnam(get_temp_dir(), '') . '.png';
     $pre_rotate_editor->writeImage($save_to_file);
     $pre_rotate_editor->destroy();
     $image_editor = new WP_Image_Editor_Imagick($save_to_file);
     $image_editor->load();
     $this->assertNotInstanceOf('WP_Error', $image_editor);
     $image_editor->rotate(180);
     $image_editor->save($save_to_file);
     $this->assertImageAlphaAtPointImagick($save_to_file, array(0, 0), $pre_rotate_alpha);
     unlink($save_to_file);
 }
	/**
	 * Test flipping an image
	 */
	public function test_flip() {

		$file = DIR_TESTDATA . '/images/gradient-square.jpg';

		$imagick_image_editor = new WP_Image_Editor_Imagick( $file );
		$imagick_image_editor->load();

		$property = new ReflectionProperty( $imagick_image_editor, 'image' );
		$property->setAccessible( true );

		$color_top_left = $property->getValue( $imagick_image_editor )->getImagePixelColor( 1, 1 )->getColor();

		$imagick_image_editor->flip( true, false );

		$this->assertEquals( $color_top_left, $property->getValue( $imagick_image_editor )->getImagePixelColor( 0, 99 )->getColor() );
	}
 public function update_size($width = null, $height = null)
 {
     return parent::update_size($width, $height);
 }
Example #11
0
 /**
  * @return bool Whether WP_Image_Editor_Imagick can be used on this system.
  */
 public static function isImagickAvailable()
 {
     static $ret = null;
     if (is_null($ret)) {
         include_once DG_WPINC_PATH . 'class-wp-image-editor.php';
         include_once DG_WPINC_PATH . 'class-wp-image-editor-imagick.php';
         $ret = WP_Image_Editor_Imagick::test();
     }
     return $ret;
 }
 /**
  * Test WP_Image_Editor_Imagick handles extension-less images
  * @ticket 39195
  */
 public function test_image_non_existent_extension()
 {
     $image_editor = new WP_Image_Editor_Imagick(DIR_TESTDATA . '/images/test-image-no-extension');
     $result = $image_editor->load();
     $this->assertTrue($result);
 }