function image_downsize($id, $size = 'medium') { if (!wp_attachment_is_image($id)) { return false; } $img_url = wp_get_attachment_url($id); $meta = wp_get_attachment_metadata($id); $width = $height = 0; // plugins can use this to provide resize services if ($out = apply_filters('image_downsize', false, $id, $size)) { return $out; } // try for a new style intermediate size if ($intermediate = image_get_intermediate_size($id, $size)) { $img_url = str_replace(basename($img_url), $intermediate['file'], $img_url); $width = $intermediate['width']; $height = $intermediate['height']; } elseif ($size == 'thumbnail') { // fall back to the old thumbnail if (($thumb_file = wp_get_attachment_thumb_file($id)) && ($info = getimagesize($thumb_file))) { $img_url = str_replace(basename($img_url), basename($thumb_file), $img_url); $width = $info[0]; $height = $info[1]; } } if (!$width && !$height && isset($meta['width'], $meta['height'])) { // any other type: use the real image and constrain it list($width, $height) = image_constrain_size_for_editor($meta['width'], $meta['height'], $size); } if ($img_url) { return array($img_url, $width, $height); } return false; }
function downsize($out, $id, $size) { $img_url = wp_get_attachment_url($id); $img_path = get_attached_file($id); $meta = wp_get_attachment_metadata($id); $width = $height = 0; $is_intermediate = false; $img_url_basename = wp_basename($img_url); $img_path_basename = wp_basename($img_path); // extract filter from size request if (!is_string($size)) { return $out; } $size_bits = explode(':', $size); $filter = isset($size_bits[1]) ? $size_bits[1] : false; $size = isset($size_bits[0]) ? $size_bits[0] : false; // start the reactor if ($filter) { // try for a new style intermediate size if ($intermediate = image_get_intermediate_size($id, $size)) { $img_url = str_replace($img_url_basename, $intermediate['file'], $img_url); $img_path = str_replace($img_path_basename, $intermediate['file'], $img_path); $width = $intermediate['width']; $height = $intermediate['height']; $is_intermediate = true; } elseif ($size == 'thumbnail') { // fall back to the old thumbnail if (($thumb_file = wp_get_attachment_thumb_file($id)) && ($info = getimagesize($thumb_file))) { $img_url = str_replace($img_url_basename, wp_basename($thumb_file), $img_url); $img_path = str_replace($img_path_basename, wp_basename($thumb_file), $img_path); $width = $info[0]; $height = $info[1]; $is_intermediate = true; } } if (!$width && !$height && isset($meta['width'], $meta['height'])) { // any other type: use the real image $width = $meta['width']; $height = $meta['height']; } if ($img_url && $img_path) { $input = $img_path; $output = $this->filtered_url($input, $filter); // generate filtered thumb if (!file_exists($output)) { $this->filter($filter, $input, $output); } // point to our new file $img_url = $this->filtered_url($img_url, $filter); // we have the actual image size, but might need to further constrain it if content_width is narrower list($width, $height) = image_constrain_size_for_editor($width, $height, $size); return array($img_url, $width, $height, $is_intermediate); } // don't continue the downsize funtion return true; } return $out; }
/** * Uses WordPress filter for image_downsize, kill wp-image-dimension * code by Andrew Rickmann * http://www.wp-fun.co.uk/ * @param $value, $id, $size */ function __wp_supercustom_cms_image_downsize($value = FALSE, $id = 0, $size = 'medium') { if (!wp_attachment_is_image($id)) { return FALSE; } $img_url = wp_get_attachment_url($id); // Mimic functionality in image_downsize function in wp-includes/media.php if ($intermediate = image_get_intermediate_size($id, $size)) { $img_url = str_replace(basename($img_url), $intermediate['file'], $img_url); } elseif ($size == 'thumbnail') { // fall back to the old thumbnail if ($thumb_file = wp_get_attachment_thumb_file() && ($info = getimagesize($thumb_file))) { $img_url = str_replace(basename($img_url), basename($thumb_file), $img_url); } } if ($img_url) { return array($img_url, 0, 0); } return FALSE; }
/** * Retrieve URL for an attachment thumbnail. * * @since 2.1.0 * * @param int $post_id Attachment ID * @return string|bool False on failure. Thumbnail URL on success. */ function wp_get_attachment_thumb_url($post_id = 0) { $post_id = (int) $post_id; if (!($post =& get_post($post_id))) { return false; } if (!($url = wp_get_attachment_url($post->ID))) { return false; } $sized = image_downsize($post_id, 'thumbnail'); if ($sized) { return $sized[0]; } if (!($thumb = wp_get_attachment_thumb_file($post->ID))) { return false; } $url = str_replace(basename($url), basename($thumb), $url); return apply_filters('wp_get_attachment_thumb_url', $url, $post->ID); }
/** * Retrieve URL for an attachment thumbnail. * * @since 2.1.0 * * @param int $post_id Optional. Attachment ID. Default 0. * @return string|false False on failure. Thumbnail URL on success. */ function wp_get_attachment_thumb_url($post_id = 0) { $post_id = (int) $post_id; if (!($post = get_post($post_id))) { return false; } if (!($url = wp_get_attachment_url($post->ID))) { return false; } $sized = image_downsize($post_id, 'thumbnail'); if ($sized) { return $sized[0]; } if (!($thumb = wp_get_attachment_thumb_file($post->ID))) { return false; } $url = str_replace(basename($url), basename($thumb), $url); /** * Filter the attachment thumbnail URL. * * @since 2.1.0 * * @param string $url URL for the attachment thumbnail. * @param int $post_id Attachment ID. */ return apply_filters('wp_get_attachment_thumb_url', $url, $post->ID); }
/** * Scale an image to fit a particular size (such as 'thumb' or 'medium'). * * Array with image url, width, height, and whether is intermediate size, in * that order is returned on success is returned. $is_intermediate is true if * $url is a resized image, false if it is the original. * * The URL might be the original image, or it might be a resized version. This * function won't create a new resized copy, it will just return an already * resized one if it exists. * * A plugin may use the 'image_downsize' filter to hook into and offer image * resizing services for images. The hook must return an array with the same * elements that are returned in the function. The first element being the URL * to the new image that was resized. * * @since 2.5.0 * * @param int $id Attachment ID for image. * @param array|string $size Optional. Image size to scale to. Accepts any valid image size, * or an array of width and height values in pixels (in that order). * Default 'medium'. * @return false|array Array containing the image URL, width, height, and boolean for whether * the image is an intermediate size. False on failure. */ function image_downsize($id, $size = 'medium') { if (!wp_attachment_is_image($id)) { return false; } /** * Filter whether to preempt the output of image_downsize(). * * Passing a truthy value to the filter will effectively short-circuit * down-sizing the image, returning that value as output instead. * * @since 2.5.0 * * @param bool $downsize Whether to short-circuit the image downsize. Default false. * @param int $id Attachment ID for image. * @param array|string $size Size of image. Image size or array of width and height values (in that order). * Default 'medium'. */ if ($out = apply_filters('image_downsize', false, $id, $size)) { return $out; } $img_url = wp_get_attachment_url($id); $meta = wp_get_attachment_metadata($id); $width = $height = 0; $is_intermediate = false; $img_url_basename = wp_basename($img_url); // try for a new style intermediate size if ($intermediate = image_get_intermediate_size($id, $size)) { $img_url = str_replace($img_url_basename, $intermediate['file'], $img_url); $width = $intermediate['width']; $height = $intermediate['height']; $is_intermediate = true; } elseif ($size == 'thumbnail') { // fall back to the old thumbnail if (($thumb_file = wp_get_attachment_thumb_file($id)) && ($info = getimagesize($thumb_file))) { $img_url = str_replace($img_url_basename, wp_basename($thumb_file), $img_url); $width = $info[0]; $height = $info[1]; $is_intermediate = true; } } if (!$width && !$height && isset($meta['width'], $meta['height'])) { // any other type: use the real image $width = $meta['width']; $height = $meta['height']; } if ($img_url) { // we have the actual image size, but might need to further constrain it if content_width is narrower list($width, $height) = image_constrain_size_for_editor($width, $height, $size); return array($img_url, $width, $height, $is_intermediate); } return false; }
/** * Filter all thumbnails and image attachments typically used in template parts, archive loops, and widgets */ static function convert_get_attachment_to_cloudinary_pull_request($override, $id, $size) { $account = static::get_option_value('cloud_name'); //if no account is set, do not continue if (empty($account)) { return false; } //prepare values for string replacements $img_url = wp_get_attachment_url($id); $meta = wp_get_attachment_metadata($id); $width = $height = 0; $is_intermediate = false; $img_url_basename = wp_basename($img_url); $cdn_fetch_prefix = static::get_cdn_prefix($account); // try for a new style intermediate size if ($intermediate = image_get_intermediate_size($id, $size)) { $width = $intermediate['width']; $height = $intermediate['height']; $original = image_get_intermediate_size($id, 'full'); $is_intermediate = true; } elseif ($size == 'thumbnail') { if (($thumb_file = wp_get_attachment_thumb_file($id)) && ($info = getimagesize($thumb_file))) { $width = $info[0]; $height = $info[1]; $is_intermediate = true; } } //make sure we have height and width values if (!$width && !$height && isset($meta['width'], $meta['height'])) { // any other type: use the real image $width = $meta['width']; $height = $meta['height']; } //if image found then modify it with cloudinary optimimized replacement if ($img_url) { $site_url = get_bloginfo('url'); // get_bloginfo( 'url' ) is called a few time, it should be move to a property of this class, i.e. $this->blogurl, or something $site_url_no_protocal = preg_replace('/http[s]?:\\/\\//', '', $site_url); // we have the actual image size, but might need to further constrain it if content_width is narrower list($width, $height) = image_constrain_size_for_editor($width, $height, $size); $cdn_fetch_options = static::get_cdn_options($height, $width); //strip protocal from image URL $cdn_img_url = preg_replace('/(http:|https:)?\\/\\/(.*)/i', '$1' . $cdn_fetch_prefix . $cdn_fetch_options . "/\$2", $img_url); // do you need the double quotes on /$2 here or would single quotes work? return array($cdn_img_url, $width, $height, $is_intermediate); } //if for some reason $img_url fails, disable filter by returning false return false; }
function wp_get_attachment_thumb_url( $post_id = 0 ) { $post_id = (int) $post_id; if ( !$post =& get_post( $post_id ) ) return false; if ( !$url = wp_get_attachment_url( $post->ID ) ) return false; if ( !$thumb = wp_get_attachment_thumb_file( $post->ID ) ) return false; $url = str_replace(basename($url), basename($thumb), $url); return apply_filters( 'wp_get_attachment_thumb_url', $url, $post->ID ); }
function get_attachment_icon_src( $id = 0, $fullsize = false ) { $id = (int) $id; if ( !$post = & get_post($id) ) return false; $imagedata = wp_get_attachment_metadata( $post->ID ); $file = get_attached_file( $post->ID ); if ( !$fullsize && $thumbfile = wp_get_attachment_thumb_file( $post->ID ) ) { // We have a thumbnail desired, specified and existing $src = wp_get_attachment_thumb_url( $post->ID ); $src_file = $thumbfile; $class = 'attachmentthumb'; } elseif ( wp_attachment_is_image( $post->ID ) ) { // We have an image without a thumbnail $src = wp_get_attachment_url( $post->ID ); $src_file = & $file; $class = 'attachmentimage'; } elseif ( $src = wp_mime_type_icon( $post->ID ) ) { // No thumb, no image. We'll look for a mime-related icon instead. $icon_dir = apply_filters( 'icon_dir', get_template_directory() . '/images' ); $src_file = $icon_dir . '/' . basename($src); } if ( !isset($src) || !$src ) return false; return array($src, $src_file); }
/** * * Modifying the orignial image_downsize function located in wp-includes/media.php * * Checks if the file with the specified dimesnions exists, before image_get_intermediate_size() * and if the file doesnt exist, it creates the file and updates the image metadata. So * image_get_intermediate_size()vwill always find the exact image user wants. */ function swift_image_downsize($content, $id, $size) { global $_wp_additional_image_sizes; global $swift_design_options; if (!wp_attachment_is_image($id)) { return false; } $img_url = wp_get_attachment_url($id); $meta = wp_get_attachment_metadata($id); $width = $height = 0; $is_intermediate = false; $img_url_basename = wp_basename($img_url); if (isset($_wp_additional_image_sizes)) { $intermediate_image_names = array_keys($_wp_additional_image_sizes); } else { $intermediate_image_names = array(''); } if (is_array($size)) { $width = $size[0]; $height = $size[1]; } elseif (in_array($size, $intermediate_image_names)) { $width = $_wp_additional_image_sizes[$size]['width']; $height = $_wp_additional_image_sizes[$size]['height']; } else { return; } if (is_array($size) || in_array($size, $intermediate_image_names)) { $uploads = wp_upload_dir(); if (!isset($meta['file'])) { return; } $file_path = $uploads['basedir'] . '/' . $meta['file']; if (file_exists($file_path)) { $orig_size = @getimagesize($file_path); /* * $path_parts = pathinfo('/www/htdocs/inc/lib.inc.php'); * echo $path_parts['dirname'], "\n"; * echo $path_parts['basename'], "\n"; * echo $path_parts['extension'], "\n"; * echo $path_parts['filename'], "\n"; // since PHP 5.2.0 * The above example will output: * * /www/htdocs/inc * lib.inc.php * php * lib.inc * */ $file_info = pathinfo($file_path); $extension = '.' . $file_info['extension']; if ($extension == '.jpeg') { $extension = '.jpg'; } // the image path without the extension $no_ext_path = $file_info['dirname'] . '/' . $file_info['filename']; // If height is set to zero, we will crop the image proportinally if ($height == 0) { $size[1] = $height = (int) ($width / $orig_size[0] * $orig_size[1]); } //Generate the cropped image path, and use it check if the image exists $cropped_img_path = $no_ext_path . '-' . $width . 'x' . $height . $extension; $cropped_img_name = $file_info['filename'] . '-' . $width . 'x' . $height . $extension; $cropped_img_path_2x = $no_ext_path . '-' . $width . 'x' . $height . '@2x' . $extension; $cropped_img_name_2x = $file_info['filename'] . '-' . $width . 'x' . $height . '@2x' . $extension; if ($orig_size[0] > $width && $orig_size[1] > $height) { // If the file doesn't exist, we generate it if (!file_exists($cropped_img_path)) { $intermediate = image_make_intermediate_size($file_path, $width, $height, true); if (!is_array($size) && $intermediate) { $meta['sizes'][$size] = $intermediate; wp_update_attachment_metadata($id, $meta); } } $img_url = preg_replace('/' . $file_info['basename'] . '/', $cropped_img_name, $img_url); } if ($swift_design_options['enable_retina_support'] && ($orig_size[0] > 2 * $width && 2 * $orig_size[1] > $height)) { if (!file_exists($cropped_img_path_2x)) { if ($width || $height) { $editor = wp_get_image_editor($file_path); if (!is_wp_error($editor) && !is_wp_error($editor->resize(2 * $width, 2 * $height, true))) { $editor->save($cropped_img_path_2x); } } } } } } if ($size == 'thumbnail') { // fall back to the old thumbnail if (($thumb_file = wp_get_attachment_thumb_file($id)) && ($info = @getimagesize($thumb_file))) { $img_url = str_replace($img_url_basename, wp_basename($thumb_file), $img_url); $width = $info[0]; $height = $info[1]; $is_intermediate = true; } } if (!$width && !$height && isset($meta['width'], $meta['height'])) { // any other type: use the real image $width = $meta['width']; $height = $meta['height']; } if ($img_url) { // we have the actual image size, but might need to further constrain it if content_width is narrower list($width, $height) = image_constrain_size_for_editor($width, $height, $size); return array($img_url, $width, $height, $is_intermediate); } return false; }
function h2only_image_downsize($value = false, $id, $size) { if (!wp_attachment_is_image($id)) { return false; } $img_url = wp_get_attachment_url($id); $is_intermediate = false; $img_url_basename = wp_basename($img_url); // try for a new style intermediate size if ($intermediate = image_get_intermediate_size($id, $size)) { $img_url = str_replace($img_url_basename, $intermediate['file'], $img_url); $is_intermediate = true; } elseif ($size == 'thumbnail') { // Fall back to the old thumbnail if (($thumb_file = wp_get_attachment_thumb_file($id)) && ($info = getimagesize($thumb_file))) { $img_url = str_replace($img_url_basename, wp_basename($thumb_file), $img_url); $is_intermediate = true; } } // We have the actual image size, but might need to further constrain it if content_width is narrower if ($img_url) { return array($img_url, 0, 0, $is_intermediate); } return false; }
/** * Get a thumbnail for an attachment, allowing us to override wp_options. * * @param int $id ID of attachment * @param int $max_w Maximum width (wp_option setting by default) * @param int $max_h Maximum height (wp_option setting by default) * @param bool $crop Should we crop rather than scale? (wp_option setting by default) * @return string URL of thumbnail, or false */ function thumb($id, $max_w = null, $max_h = null, $crop = null) { if ($max_w === null) { $max_w = get_option('thumbnail_size_w'); } if ($max_h === null) { $max_h = get_option('thumbnail_size_h'); } if ($crop === null) { $crop = get_option('thumbnail_crop'); } return str_replace(basename(wp_get_attachment_thumb_url($id)), basename(image_resize(wp_get_attachment_thumb_file($id), $max_w, $max_h, $crop)), wp_get_attachment_thumb_url($id)); }
function hello_img($value, $id, $size = 'medium') { $img_url = wp_get_attachment_url($id); $meta = wp_get_attachment_metadata($id); $width = $height = 0; $is_intermediate = false; $img_url_basename = wp_basename($img_url); // try for a new style intermediate size if ($intermediate = image_get_intermediate_size($id, $size)) { $img_url = str_replace($img_url_basename, $intermediate['file'], $img_url); $width = $intermediate['width']; $height = $intermediate['height']; $is_intermediate = true; } elseif ($size == 'thumbnail') { // fall back to the old thumbnail if (($thumb_file = wp_get_attachment_thumb_file($id)) && ($info = getimagesize($thumb_file))) { $img_url = str_replace($img_url_basename, wp_basename($thumb_file), $img_url); $width = $info[0]; $height = $info[1]; $is_intermediate = true; } } if (!$width && !$height && isset($meta['width'], $meta['height'])) { // any other type: use the real image $width = $meta['width']; $height = $meta['height']; } if ($img_url) { $header_response = get_headers($img_url, 1); if (strpos($header_response[0], "404") == false) { // we have the actual image size, but might need to further constrain it if content_width is narrower list($width, $height) = image_constrain_size_for_editor($width, $height, $size); return array($img_url, $width, $height, $is_intermediate); } else { $html = get_template_directory_uri() . '/assets/img/placeholder/' . $size . '.png'; return array($html, $width, $height, $is_intermediate); } } return false; }