| 1 | <?php |
| 2 | /** |
| 3 | * WordPress GhostScript Image Editor |
| 4 | * |
| 5 | * @package WordPress |
| 6 | * @subpackage Image_Editor |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * WordPress Image Editor Class for producing JPEG from PDF using GhostScript. |
| 11 | * |
| 12 | * @since 4.x |
| 13 | * @package WordPress |
| 14 | * @subpackage Image_Editor |
| 15 | * @uses WP_Image_Editor Extends class |
| 16 | */ |
| 17 | class WP_Image_Editor_GS extends WP_Image_Editor { |
| 18 | |
| 19 | /** |
| 20 | * Resolution of output JPEG. |
| 21 | * |
| 22 | * @access protected |
| 23 | * @var string |
| 24 | */ |
| 25 | protected $resolution = '128x128'; |
| 26 | |
| 27 | /** |
| 28 | * Checks to see if current environment supports GhostScript. |
| 29 | * |
| 30 | * @since 4.x |
| 31 | * |
| 32 | * @static |
| 33 | * @access public |
| 34 | * |
| 35 | * @staticvar array $have_gs |
| 36 | * |
| 37 | * @param array $args |
| 38 | * @return bool |
| 39 | */ |
| 40 | public static function test( $args = array() ) { |
| 41 | static $have_gs = null; |
| 42 | |
| 43 | if ( null === $have_gs ) { |
| 44 | $cmd = self::gs_cmd( '-dBATCH -dNOPAUSE -dNOPROMPT -dSAFER -v' ); |
| 45 | exec( $cmd, $output, $return_var ); |
| 46 | |
| 47 | if ( 0 !== $return_var ) { |
| 48 | $have_gs = false; |
| 49 | } elseif ( empty( $output[0] ) || false === stripos( $output[0], 'ghostscript' ) ) { |
| 50 | $have_gs = false; |
| 51 | } else { |
| 52 | $have_gs = true; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | if ( ! $have_gs ) { |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | if ( isset( $args['methods'] ) ) { |
| 61 | $unsupported_methods = array( 'resize', 'multi_resize', 'crop', 'rotate', 'flip', 'stream' ); |
| 62 | if ( array_intersect( $unsupported_methods, $args['methods'] ) ) { |
| 63 | return false; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // Loading from URL not currently supported. |
| 68 | if ( isset( $args['path'] ) && preg_match( '|^https?://|', $args['path'] ) ) { |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Checks to see if editor supports the mime-type specified. |
| 77 | * |
| 78 | * @since 4.x |
| 79 | * |
| 80 | * @static |
| 81 | * @access public |
| 82 | * |
| 83 | * @param string $mime_type |
| 84 | * @return bool |
| 85 | */ |
| 86 | public static function supports_mime_type( $mime_type ) { |
| 87 | return 'PDF' === strtoupper( self::get_extension( $mime_type ) ); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Checks existence and sets mime type and calls `set_quality`. |
| 92 | * |
| 93 | * @since 4.x |
| 94 | * @access protected |
| 95 | * |
| 96 | * @return true|WP_Error True if loaded; WP_Error on failure. |
| 97 | */ |
| 98 | public function load() { |
| 99 | if ( ! is_file( $this->file ) ) { |
| 100 | return new WP_Error( 'error_loading_image', __( 'File doesn’t exist?' ), $this->file ); |
| 101 | } |
| 102 | |
| 103 | list( $filename, $extension, $mime_type ) = $this->get_output_format( $this->file ); |
| 104 | $this->mime_type = $mime_type; |
| 105 | |
| 106 | return $this->set_quality(); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Creates JPEG from PDF. |
| 111 | * |
| 112 | * @since 4.x |
| 113 | * @access public |
| 114 | * |
| 115 | * @param string $destfilename |
| 116 | * @param string $mime_type |
| 117 | * @return array|WP_Error {'path'=>string, 'file'=>string, 'width'=>int, 'height'=>int, 'mime-type'=>string} |
| 118 | */ |
| 119 | public function save( $destfilename = null, $mime_type = null ) { |
| 120 | list( $filename, $extension, $mime_type ) = $this->get_output_format( $destfilename, $mime_type ); |
| 121 | |
| 122 | if ( 'image/jpeg' !== $mime_type ) { |
| 123 | return new WP_Error( 'image_save_error', __( 'Unsupported mime type.' ) ); |
| 124 | } |
| 125 | |
| 126 | if ( ! $filename ) { |
| 127 | $filename = $this->generate_filename( null, null, $extension ); |
| 128 | } |
| 129 | $pdf_filename = $this->file; |
| 130 | |
| 131 | // TODO: Probably some escaping and Windows/IIS issues. |
| 132 | $cmd = self::gs_cmd( $this->get_gs_args( $filename ) . ' ' . escapeshellarg( $pdf_filename ) ); |
| 133 | exec( $cmd, $output, $return_val ); |
| 134 | |
| 135 | if ( 0 !== $return_val ) { |
| 136 | return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed.' ) ); |
| 137 | } |
| 138 | |
| 139 | $size = @ getimagesize( $filename ); |
| 140 | if ( ! $size ) { |
| 141 | return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed.' ) ); |
| 142 | } |
| 143 | |
| 144 | // Transmogrify into the JPEG file. |
| 145 | $this->file = $filename; |
| 146 | $this->mime_type = $mime_type; |
| 147 | $this->update_size( $size[0], $size[1] ); |
| 148 | |
| 149 | // Set correct file permissions |
| 150 | $stat = stat( dirname( $filename ) ); |
| 151 | $perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits |
| 152 | @ chmod( $filename, $perms ); |
| 153 | |
| 154 | /** This filter is documented in wp-includes/class-wp-image-editor-gd.php */ |
| 155 | return array( |
| 156 | 'path' => $filename, |
| 157 | 'file' => wp_basename( apply_filters( 'image_make_intermediate_size', $filename ) ), |
| 158 | 'width' => $this->size['width'], |
| 159 | 'height' => $this->size['height'], |
| 160 | 'mime-type' => $mime_type, |
| 161 | ); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Process shell command to lessen noise. |
| 166 | * |
| 167 | * @since 4.x |
| 168 | * @access protected |
| 169 | * |
| 170 | * @param string $cmd Command. |
| 171 | * @return string |
| 172 | */ |
| 173 | protected static function gs_cmd( $args ) { |
| 174 | global $is_IIS; |
| 175 | if ( $is_IIS ) { |
| 176 | $args .= ' -sstdout=NUL'; |
| 177 | } else { |
| 178 | $args .= ' -sstdout=/dev/null'; |
| 179 | } |
| 180 | // Redirection gets escaped in safe mode. |
| 181 | if ( ! ini_get( 'safe_mode' ) ) { |
| 182 | $args .= ' 2>1'; |
| 183 | } |
| 184 | return 'gs ' . $args; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Get the arguments for main GhostScript invocation. |
| 189 | * |
| 190 | * @since 4.x |
| 191 | * @access protected |
| 192 | * |
| 193 | * @param string $filename File name of output JPEG. |
| 194 | * @return string |
| 195 | */ |
| 196 | protected function get_gs_args( $filename ) { |
| 197 | $ret = '-dBATCH -dFirstPage=1 -dLastPage=1 -dNOPAUSE -dNOPROMPT -dQUIET -q -sDEVICE=jpeg'; |
| 198 | |
| 199 | // TODO: Probably some escaping and Windows/IIS issues. |
| 200 | if ( $quality = $this->get_quality() ) { |
| 201 | $ret .= ' ' . escapeshellarg( sprintf( '-dJPEGQ=%d', $quality ) ); |
| 202 | } |
| 203 | if ( $this->resolution ) { |
| 204 | $ret .= ' ' . escapeshellarg( '-r' . $this->resolution ); |
| 205 | } |
| 206 | if ( $filename ) { |
| 207 | $ret .= ' ' . escapeshellarg( '-sOutputFile=' . $filename ); |
| 208 | } |
| 209 | |
| 210 | return $ret; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Resizes current image. Unsupported. |
| 215 | * |
| 216 | * At minimum, either a height or width must be provided. |
| 217 | * If one of the two is set to null, the resize will |
| 218 | * maintain aspect ratio according to the provided dimension. |
| 219 | * |
| 220 | * @since 4.x |
| 221 | * @access public |
| 222 | * |
| 223 | * @param int|null $max_w Image width. |
| 224 | * @param int|null $max_h Image height. |
| 225 | * @param bool $crop |
| 226 | * @return WP_Error |
| 227 | */ |
| 228 | public function resize( $max_w, $max_h, $crop = false ) { |
| 229 | return new WP_Error( 'image_resize_error', __( 'Unsupported operation.' ) ); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Resize multiple images from a single source. Unsupported. |
| 234 | * |
| 235 | * @since 4.x |
| 236 | * @access public |
| 237 | * |
| 238 | * @param array $sizes { |
| 239 | * An array of image size arrays. Default sizes are 'small', 'medium', 'large'. |
| 240 | * |
| 241 | * @type array $size { |
| 242 | * @type int $width Image width. |
| 243 | * @type int $height Image height. |
| 244 | * @type bool $crop Optional. Whether to crop the image. Default false. |
| 245 | * } |
| 246 | * } |
| 247 | * @return WP_Error |
| 248 | */ |
| 249 | public function multi_resize( $sizes ) { |
| 250 | return new WP_Error( 'image_multi_resize_error', __( 'Unsupported operation.' ) ); |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Crops Image. Unsupported. |
| 255 | * |
| 256 | * @since 4.x |
| 257 | * @access public |
| 258 | * |
| 259 | * @param int $src_x The start x position to crop from. |
| 260 | * @param int $src_y The start y position to crop from. |
| 261 | * @param int $src_w The width to crop. |
| 262 | * @param int $src_h The height to crop. |
| 263 | * @param int $dst_w Optional. The destination width. |
| 264 | * @param int $dst_h Optional. The destination height. |
| 265 | * @param bool $src_abs Optional. If the source crop points are absolute. |
| 266 | * @return WP_Error |
| 267 | */ |
| 268 | public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ) { |
| 269 | return new WP_Error( 'image_crop_error', __( 'Unsupported operation.' ) ); |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Rotates current image counter-clockwise by $angle. Unsupported. |
| 274 | * |
| 275 | * @since 4.x |
| 276 | * @access public |
| 277 | * |
| 278 | * @param float $angle |
| 279 | * @return WP_Error |
| 280 | */ |
| 281 | public function rotate( $angle ) { |
| 282 | return new WP_Error( 'image_rotate_error', __( 'Unsupported operation.' ) ); |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Flips current image. Unsupported. |
| 287 | * |
| 288 | * @since 4.x |
| 289 | * @access public |
| 290 | * |
| 291 | * @param bool $horz Flip along Horizontal Axis |
| 292 | * @param bool $vert Flip along Vertical Axis |
| 293 | * @return WP_Error |
| 294 | */ |
| 295 | public function flip( $horz, $vert ) { |
| 296 | return new WP_Error( 'image_flip_error', __( 'Unsupported operation.' ) ); |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Streams current image to browser. Unsupported. |
| 301 | * |
| 302 | * @since 4.x |
| 303 | * @access public |
| 304 | * |
| 305 | * @param string $mime_type |
| 306 | * @return WP_Error |
| 307 | */ |
| 308 | public function stream( $mime_type = null ) { |
| 309 | return new WP_Error( 'image_stream_error', __( 'Unsupported operation.' ) ); |
| 310 | } |
| 311 | } |