]> scripts.mit.edu Git - autoinstalls/wordpress.git/blobdiff - wp-includes/class-wp-image-editor.php
WordPress 3.8
[autoinstalls/wordpress.git] / wp-includes / class-wp-image-editor.php
index 81909f89be0b54eee6b55006ebdb9903416c7784..7f6488f91f530e32ddff312e3f9b191ea809c327 100644 (file)
@@ -205,19 +205,49 @@ abstract class WP_Image_Editor {
         * @access public
         *
         * @param int $quality Compression Quality. Range: [1,100]
-        * @return boolean
+        * @return boolean|WP_Error True if set successfully; WP_Error on failure.
         */
-       public function set_quality( $quality ) {
+       public function set_quality( $quality = null ) {
+               if ( $quality == null ) {
+                       $quality = $this->quality;
+               }
+
                /**
-                * Filter the default quality setting.
+                * Filter the default image compression quality setting.
                 *
                 * @since 3.5.0
                 *
-                * @param int $quality Quality level between 0 (low) and 100 (high).
+                * @param int    $quality   Quality level between 1 (low) and 100 (high).
+                * @param string $mime_type Image mime type.
                 */
-               $this->quality = apply_filters( 'wp_editor_set_quality', $quality );
+               $quality = apply_filters( 'wp_editor_set_quality', $quality, $this->mime_type );
+
+               if ( 'image/jpeg' == $this->mime_type ) {
+                       /**
+                        * Filter the JPEG compression quality for backward-compatibility.
+                        *
+                        * The filter is evaluated under two contexts: 'image_resize', and 'edit_image',
+                        * (when a JPEG image is saved to file).
+                        *
+                        * @since 2.5.0
+                        *
+                        * @param int    $quality Quality level between 0 (low) and 100 (high) of the JPEG.
+                        * @param string $context Context of the filter.
+                        */
+                       $quality = apply_filters( 'jpeg_quality', $quality, 'image_resize' );
 
-               return ( (bool) $this->quality );
+                       // Allow 0, but squash to 1 due to identical images in GD, and for backwards compatibility.
+                       if ( $quality == 0 ) {
+                               $quality = 1;
+                       }
+               }
+
+               if ( ( $quality >= 1 ) && ( $quality <= 100 ) ){
+                       $this->quality = $quality;
+                       return true;
+               } else {
+                       return new WP_Error( 'invalid_image_quality', __('Attempted to set image quality outside of the range [1,100].') );
+               }
        }
 
        /**