WP_Image_Editor_Imagick::_save PHP Method

_save() protected method

protected _save ( Imagick $image, string $filename = null, string $mime_type = null ) : array | WP_Error
$image Imagick
$filename string
$mime_type string
return array | WP_Error
    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);
        }
        try {
            // Store initial Format
            $orig_format = $this->image->getImageFormat();
            $this->image->setImageFormat(strtoupper($this->get_extension($mime_type)));
            $this->make_image($filename, array($image, 'writeImage'), array($filename));
            // Reset original Format
            $this->image->setImageFormat($orig_format);
        } catch (Exception $e) {
            return new WP_Error('image_save_error', $e->getMessage(), $filename);
        }
        // Set correct file permissions
        $stat = stat(dirname($filename));
        $perms = $stat['mode'] & 0666;
        //same permissions as parent folder, strip off the executable bits
        @chmod($filename, $perms);
        /** This filter is documented in wp-includes/class-wp-image-editor-gd.php */
        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);
    }

Usage Example

 /**
  * 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);
 }