Exemplo n.º 1
0
 /**
  * Process the thumbnail.
  *
  * @param  string  $url     Image URL (must be uploaded using wp media uploader).
  * @param  int     $width   Thumbnail width.
  * @param  int     $height  Thumbnail height.
  * @param  boolean $crop    Soft (false) or hard (true) crop.
  * @param  boolean $upscale Force the resize.
  *
  * @return string           New thumbnail.
  */
 public function process($url, $width = null, $height = null, $crop = false, $upscale = false)
 {
     // Validate inputs.
     if (!$url || !$width && !$height) {
         return false;
     }
     // Caipt'n, ready to hook.
     if (true === $upscale) {
         add_filter('image_resize_dimensions', array($this, 'aq_upscale'), 10, 6);
     }
     // Define upload path, directory and http prefix.
     $generate_paths = self::get_upload_path($url);
     $upload_dir = $generate_paths['upload_dir'];
     $upload_url = $generate_paths['upload_url'];
     // Check if $image_url is local.
     if (false === strpos($url, $upload_url)) {
         return false;
     }
     // Define path of image.
     $rel_path = str_replace($upload_url, '', $url);
     $image_path = $upload_dir . $rel_path;
     // Check if img path exists, and is an image indeed.
     if (!file_exists($image_path) || !getimagesize($image_path)) {
         return false;
     }
     // Get image info.
     $info = pathinfo($image_path);
     $ext = $info['extension'];
     list($original_width, $original_height) = getimagesize($image_path);
     // Get image size after cropping.
     $dimensions = image_resize_dimensions($original_width, $original_height, $width, $height, $crop);
     $original_width = $dimensions[4];
     $original_height = $dimensions[5];
     // Return the original image only if it exactly fits the needed measures.
     if (!$dimensions && (null === $height && $original_width == $width xor null === $width && $original_height == $height xor $height == $original_height && $width == $original_width)) {
         $image_url = $url;
     } else {
         // Use this to check if cropped image already exists, so we can return that instead.
         $suffix = $original_width . 'x' . $original_height;
         $original_rel_path = str_replace('.' . $ext, '', $rel_path);
         $destfilename = $upload_dir . $original_rel_path . '-' . $suffix . '.' . $ext;
         // Can't resize, so return false saying that the action to do could not be processed as planned.
         if (!$dimensions || true == $crop && false == $upscale && ($original_width < $width || $original_height < $height)) {
             return false;
             // Else check if cache exists.
         } elseif (file_exists($destfilename) && @getimagesize($destfilename)) {
             $image_url = $upload_url . $original_rel_path . '-' . $suffix . '.' . $ext;
             // Else, we resize the image and return the new resized image url.
         } else {
             $editor = wp_get_image_editor($image_path);
             if (is_wp_error($editor) || is_wp_error($editor->resize($width, $height, $crop))) {
                 return false;
             }
             $resized_file = $editor->save();
             if (!is_wp_error($resized_file)) {
                 $resized_rel_path = str_replace($upload_dir, '', $resized_file['path']);
                 $image_url = $upload_url . $resized_rel_path;
             } else {
                 return false;
             }
         }
     }
     self::$image_url = $image_url;
     self::$width = $original_width;
     self::$height = $original_height;
     // Okay, leave the ship.
     if (true === $upscale) {
         remove_filter('image_resize_dimensions', array($this, 'aq_upscale'));
     }
     return $image_url;
 }