/**
  * 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;
 }
예제 #2
0
/**
 * Get a image URL.
 *
 * @param  int     $id      Image ID.
 * @param  int     $width   Image width.
 * @param  int     $height  Image height.
 * @param  boolean $crop    Image crop.
 * @param  boolean $upscale Force the resize.
 *
 * @return string
 */
function odin_get_image_url($id, $width, $height, $crop = true, $upscale = false)
{
    $resizer = Odin_Thumbnail_Resizer::get_instance();
    $origin_url = wp_get_attachment_url($id);
    $url = $resizer->process($origin_url, $width, $height, $crop, $upscale);
    if ($url) {
        return $url;
    } else {
        return $origin_url;
    }
}
예제 #3
0
파일: helpers.php 프로젝트: brenoalvs/odin
/**
 * Custom post thumbnail.
 *
 * @since  2.2.0
 *
 * @param  int     $width  Width of the image.
 * @param  int     $height Height of the image.
 * @param  string  $class  Class attribute of the image.
 * @param  string  $alt    Alt attribute of the image.
 * @param  bool    $crop   Image crop.
 * @param  string  $class  Custom HTML classes.
 *
 * @return string         Return the post thumbnail.
 */
function odin_thumbnail($width, $height, $alt, $crop = true, $class = '')
{
    if (!class_exists('Odin_Thumbnail_Resizer')) {
        return;
    }
    $thumb = get_post_thumbnail_id();
    if ($thumb) {
        $resizer = Odin_Thumbnail_Resizer::get_instance();
        $url = wp_get_attachment_url($thumb, 'full');
        $image = $resizer->process($url, $width, $height, $crop);
        $html = '<img class="wp-image-thumb img-responsive ' . sanitize_html_class($class) . '" src="' . $image . '" width="' . esc_attr($width) . '" height="' . esc_attr($height) . '" alt="' . esc_attr($alt) . '" />';
        return apply_filters('odin_thumbnail_html', $html);
    }
}