WP_Image_Editor_Imagick::load PHP Method

load() public method

Loads image from $this->file into new Imagick Object.
Since: 3.5.0
public load ( ) : true | WP_Error
return true | WP_Error True if loaded; WP_Error on failure.
    public function load()
    {
        if ($this->image instanceof Imagick) {
            return true;
        }
        if (!is_file($this->file) && !preg_match('|^https?://|', $this->file)) {
            return new WP_Error('error_loading_image', __('File doesn’t exist?'), $this->file);
        }
        /*
         * Even though Imagick uses less PHP memory than GD, set higher limit
         * for users that have low PHP.ini limits.
         */
        wp_raise_memory_limit('image');
        try {
            $this->image = new Imagick();
            $file_parts = pathinfo($this->file);
            $filename = $this->file;
            if ('pdf' == strtolower($file_parts['extension'])) {
                $filename = $this->pdf_setup();
            }
            // Reading image after Imagick instantiation because `setResolution`
            // only applies correctly before the image is read.
            $this->image->readImage($filename);
            if (!$this->image->valid()) {
                return new WP_Error('invalid_image', __('File is not an image.'), $this->file);
            }
            // Select the first frame to handle animated images properly
            if (is_callable(array($this->image, 'setIteratorIndex'))) {
                $this->image->setIteratorIndex(0);
            }
            $this->mime_type = $this->get_mime_type($this->image->getImageFormat());
        } catch (Exception $e) {
            return new WP_Error('invalid_image', $e->getMessage(), $this->file);
        }
        $updated_size = $this->update_size();
        if (is_wp_error($updated_size)) {
            return $updated_size;
        }
        return $this->set_quality();
    }

Usage Example

 /**
  * 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);
 }
All Usage Examples Of WP_Image_Editor_Imagick::load