예제 #1
0
/**
 * Adds 'srcset' and 'sizes' attributes to an existing 'img' element.
 *
 * @since 4.4.0
 *
 * @see wp_calculate_image_srcset()
 * @see wp_calculate_image_sizes()
 *
 * @param string $image         An HTML 'img' element to be filtered.
 * @param array  $image_meta    The image meta data as returned by 'wp_get_attachment_metadata()'.
 * @param int    $attachment_id Image attachment ID.
 * @return string Converted 'img' element with 'srcset' and 'sizes' attributes added.
 */
function wp_image_add_srcset_and_sizes($image, $image_meta, $attachment_id)
{
    // Ensure the image meta exists.
    if (empty($image_meta['sizes'])) {
        return $image;
    }
    $image_src = preg_match('/src="([^"]+)"/', $image, $match_src) ? $match_src[1] : '';
    list($image_src) = explode('?', $image_src);
    // Return early if we couldn't get the image source.
    if (!$image_src) {
        return $image;
    }
    // Bail early if an image has been inserted and later edited.
    if (preg_match('/-e[0-9]{13}/', $image_meta['file'], $img_edit_hash) && strpos(wp_basename($image_src), $img_edit_hash[0]) === false) {
        return $image;
    }
    $base_url = trailingslashit(_wp_upload_dir_baseurl());
    $image_base_url = $base_url;
    $dirname = dirname($image_meta['file']);
    if ($dirname !== '.') {
        $image_base_url .= trailingslashit($dirname);
    }
    $all_sizes = wp_list_pluck($image_meta['sizes'], 'file');
    foreach ($all_sizes as $key => $file) {
        $all_sizes[$key] = $image_base_url . $file;
    }
    // Add the original image.
    $all_sizes[] = $base_url . $image_meta['file'];
    // Bail early if the image src doesn't match any of the known image sizes.
    if (!in_array($image_src, $all_sizes)) {
        return $image;
    }
    $width = preg_match('/ width="([0-9]+)"/', $image, $match_width) ? (int) $match_width[1] : 0;
    $height = preg_match('/ height="([0-9]+)"/', $image, $match_height) ? (int) $match_height[1] : 0;
    if (!$width || !$height) {
        /*
         * If attempts to parse the size value failed, attempt to use the image meta data to match
         * the image file name from 'src' against the available sizes for an attachment.
         */
        $image_filename = wp_basename($image_src);
        if ($image_filename === wp_basename($image_meta['file'])) {
            $width = (int) $image_meta['width'];
            $height = (int) $image_meta['height'];
        } else {
            foreach ($image_meta['sizes'] as $image_size_data) {
                if ($image_filename === $image_size_data['file']) {
                    $width = (int) $image_size_data['width'];
                    $height = (int) $image_size_data['height'];
                    break;
                }
            }
        }
    }
    if (!$width || !$height) {
        return $image;
    }
    $size_array = array($width, $height);
    $srcset = wp_calculate_image_srcset($size_array, $image_src, $image_meta, $attachment_id);
    if ($srcset) {
        // Check if there is already a 'sizes' attribute.
        $sizes = strpos($image, ' sizes=');
        if (!$sizes) {
            $sizes = wp_calculate_image_sizes($size_array, $image_src, $image_meta, $attachment_id);
        }
    }
    if ($srcset && $sizes) {
        // Format the 'srcset' and 'sizes' string and escape attributes.
        $attr = sprintf(' srcset="%s"', esc_attr($srcset));
        if (is_string($sizes)) {
            $attr .= sprintf(' sizes="%s"', esc_attr($sizes));
        }
        // Add 'srcset' and 'sizes' attributes to the image markup.
        $image = preg_replace('/<img ([^>]+?)[\\/ ]*>/', '<img $1' . $attr . ' />', $image);
    }
    return $image;
}
예제 #2
0
/**
 * Create image tag markup for a custom header image.
 *
 * @since 4.4.0
 *
 * @param array $attr Optional. Additional attributes for the image tag. Can be used
 *                              to override the default attributes. Default empty.
 * @return string HTML image element markup or empty string on failure.
 */
function get_header_image_tag($attr = array())
{
    $header = get_custom_header();
    if (empty($header->url)) {
        return '';
    }
    $width = absint($header->width);
    $height = absint($header->height);
    $attr = wp_parse_args($attr, array('src' => $header->url, 'width' => $width, 'height' => $height, 'alt' => get_bloginfo('name')));
    // Generate 'srcset' and 'sizes' if not already present.
    if (empty($attr['srcset']) && !empty($header->attachment_id)) {
        $image_meta = get_post_meta($header->attachment_id, '_wp_attachment_metadata', true);
        $size_array = array($width, $height);
        if (is_array($image_meta)) {
            $srcset = wp_calculate_image_srcset($size_array, $header->url, $image_meta, $header->attachment_id);
            $sizes = !empty($attr['sizes']) ? $attr['sizes'] : wp_calculate_image_sizes($size_array, $header->url, $image_meta, $header->attachment_id);
            if ($srcset && $sizes) {
                $attr['srcset'] = $srcset;
                $attr['sizes'] = $sizes;
            }
        }
    }
    $attr = array_map('esc_attr', $attr);
    $html = '<img';
    foreach ($attr as $name => $value) {
        $html .= ' ' . $name . '="' . $value . '"';
    }
    $html .= ' />';
    /**
     * Filter the markup of header images.
     *
     * @since 4.4.0
     *
     * @param string $html   The HTML image tag markup being filtered.
     * @param object $header The custom header object returned by 'get_custom_header()'.
     * @param array  $attr   Array of the attributes for the image tag.
     */
    return apply_filters('get_header_image_tag', $html, $header, $attr);
}
예제 #3
0
/**
 * Adds 'srcset' and 'sizes' attributes to an existing 'img' element.
 *
 * @since 4.4.0
 *
 * @see 'wp_get_attachment_image_srcset()'
 * @see 'wp_get_attachment_image_sizes()'
 *
 * @param string $image         An HTML 'img' element to be filtered.
 * @param array  $image_meta    The image meta data as returned by 'wp_get_attachment_metadata()'.
 * @param int    $attachment_id Image attachment ID.
 * @return string Converted 'img' element with 'srcset' and 'sizes' attributes added.
 */
function wp_image_add_srcset_and_sizes($image, $image_meta, $attachment_id)
{
    // Ensure the image meta exists.
    if (empty($image_meta['sizes'])) {
        return $image;
    }
    $src = preg_match('/src="([^"]+)"/', $image, $match_src) ? $match_src[1] : '';
    list($src) = explode('?', $src);
    // Return early if we couldn't get the image source.
    if (!$src) {
        return $image;
    }
    // Bail early if an image has been inserted and later edited.
    if (preg_match('/-e[0-9]{13}/', $image_meta['file'], $img_edit_hash) && strpos(wp_basename($src), $img_edit_hash[0]) === false) {
        return $image;
    }
    $width = preg_match('/ width="([0-9]+)"/', $image, $match_width) ? (int) $match_width[1] : 0;
    $height = preg_match('/ height="([0-9]+)"/', $image, $match_height) ? (int) $match_height[1] : 0;
    if (!$width || !$height) {
        /*
         * If attempts to parse the size value failed, attempt to use the image meta data to match
         * the image file name from 'src' against the available sizes for an attachment.
         */
        $image_filename = wp_basename($src);
        if ($image_filename === wp_basename($image_meta['file'])) {
            $width = (int) $image_meta['width'];
            $height = (int) $image_meta['height'];
        } else {
            foreach ($image_meta['sizes'] as $image_size_data) {
                if ($image_filename === $image_size_data['file']) {
                    $width = (int) $image_size_data['width'];
                    $height = (int) $image_size_data['height'];
                    break;
                }
            }
        }
    }
    if (!$width || !$height) {
        return $image;
    }
    $size_array = array($width, $height);
    $srcset = wp_calculate_image_srcset($src, $size_array, $image_meta, $attachment_id);
    if ($srcset) {
        $sizes = wp_get_attachment_image_sizes($size_array, $image_meta, $attachment_id, $src);
    }
    if ($srcset && $sizes) {
        // Format the 'srcset' and 'sizes' string and escape attributes.
        $srcset_and_sizes = sprintf(' srcset="%s" sizes="%s"', esc_attr($srcset), esc_attr($sizes));
        // Add 'srcset' and 'sizes' attributes to the image markup.
        $image = preg_replace('/<img ([^>]+?)[\\/ ]*>/', '<img $1' . $srcset_and_sizes . ' />', $image);
    }
    return $image;
}
예제 #4
0
파일: media.php 프로젝트: nkeat12/dv
 /**
  * @ticket 33641
  * @ticket 34528
  */
 function test_wp_calculate_image_srcset_animated_gifs()
 {
     // Mock meta for an animated gif.
     $image_meta = array('width' => 1200, 'height' => 600, 'file' => 'animated.gif', 'sizes' => array('thumbnail' => array('file' => 'animated-150x150.gif', 'width' => 150, 'height' => 150, 'mime-type' => 'image/gif'), 'medium' => array('file' => 'animated-300x150.gif', 'width' => 300, 'height' => 150, 'mime-type' => 'image/gif'), 'large' => array('file' => 'animated-1024x512.gif', 'width' => 1024, 'height' => 512, 'mime-type' => 'image/gif')));
     $full_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_meta['file'];
     $large_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_meta['sizes']['large']['file'];
     // Test with soft resized size array.
     $size_array = array(900, 450);
     // Full size GIFs should not return a srcset.
     $this->assertFalse(wp_calculate_image_srcset($size_array, $full_src, $image_meta));
     // Intermediate sized GIFs should not include the full size in the srcset.
     $this->assertFalse(strpos(wp_calculate_image_srcset($size_array, $large_src, $image_meta), $full_src));
 }
예제 #5
0
        <div class="swiper-slide <?php 
        echo $slide->post_title;
        ?>
" >
            <?php 
        if (get_field('second', $slide->ID)) {
            $image_obj = get_field('second', $slide->ID);
            $default_attr = array('class' => "wp-post-image");
            $image_id = $image_obj["id"];
            $image_meta = wp_get_attachment_metadata($image_id);
            $image = wp_get_attachment_image_src($image_id, 'full');
            if ($image) {
                $image_src = $image[0];
                $size_array = array(absint($image[1]), absint($image[2]));
            }
            $srcset_value = wp_calculate_image_srcset($size_array, $image_src, $image_meta);
            $sizes_value = wp_get_attachment_image_sizes($image_id, 'full');
            $srcset = $srcset_value ? ' srcset="' . esc_attr($srcset_value) . '"' : '';
            $sizes = $sizes_value ? ' sizes="' . esc_attr($sizes_value) . '"' : '';
            ?>
              <picture>
  <source media="(max-width: 49.999em)" <?php 
            echo $srcset;
            ?>
 <?php 
            echo $sizes;
            ?>
 >
<?php 
            echo get_the_post_thumbnail($slide->ID);
            ?>
 /**
  * @ticket 34945
  * @ticket 33641
  */
 function test_wp_get_attachment_image_with_https_on()
 {
     // Mock meta for the image.
     $image_meta = array('width' => 1200, 'height' => 600, 'file' => 'test.jpg', 'sizes' => array('thumbnail' => array('file' => 'test-150x150.jpg', 'width' => 150, 'height' => 150), 'medium' => array('file' => 'test-300x150.jpg', 'width' => 300, 'height' => 150), 'large' => array('file' => 'test-1024x512.jpg', 'width' => 1024, 'height' => 512)));
     // Test using the large file size.
     $size_array = array(1024, 512);
     $image_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_meta['sizes']['large']['file'];
     $_SERVER['HTTPS'] = 'on';
     $expected = 'https://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test-1024x512.jpg 1024w, https://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test-300x150.jpg 300w, https://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg 1200w';
     $actual = wp_calculate_image_srcset($size_array, $image_url, $image_meta);
     $this->assertSame($expected, $actual);
 }
	<?php 
if (has_post_thumbnail()) {
    $image_title = esc_attr(get_the_title(get_post_thumbnail_id()));
    $image_link = wp_get_attachment_url(get_post_thumbnail_id());
    if ($presizeimage == 1) {
        $image_id = get_post_thumbnail_id($post->ID);
        $product_image = wp_get_attachment_image_src($image_id, 'full');
        $product_image_url = $product_image[0];
        // Get the cropped size
        $image_url = aq_resize($product_image_url, $productimgwidth, $productimgheight, true);
        if (empty($image_url)) {
            $image_url = $product_image_url;
        }
        // Get srcset
        $image_meta = get_post_meta($image_id, '_wp_attachment_metadata', true);
        $img_srcset = wp_calculate_image_srcset(array($productimgwidth, $productimgheight), $product_image_url, $image_meta, $image_id);
        $image = '<img width="' . esc_attr($productimgwidth) . '" height="' . esc_attr($productimgheight) . '" src="' . esc_url($image_url) . '" scrset="' . esc_attr($img_srcset) . '" sizes="(max-width: ' . esc_attr($productimgwidth) . 'px) 100vw, ' . esc_attr($productimgwidth) . 'px" class="attachment-shop_single wp-post-image" alt="' . esc_attr(get_the_title(get_post_thumbnail_id())) . '">';
    } else {
        $image = get_the_post_thumbnail($post->ID, apply_filters('single_product_large_thumbnail_size', 'shop_single'), array('title' => $image_title));
    }
    $attachment_count = count($product->get_gallery_attachment_ids());
    if ($attachment_count > 0) {
        $gallery = '[product-gallery]';
    } else {
        $gallery = '';
    }
    echo apply_filters('woocommerce_single_product_image_html', sprintf('<a href="%s" itemprop="image" class="woocommerce-main-image zoom" title="%s"  data-rel="lightbox' . $gallery . '">%s</a>', $image_link, $image_title, $image), $post->ID);
} else {
    echo apply_filters('woocommerce_single_product_image_html', sprintf('<img src="%s" alt="Placeholder" />', woocommerce_placeholder_img_src()), $post->ID);
}
?>
function kt_woocommerce_template_loop_product_thumbnail()
{
    global $virtue, $woocommerce_loop, $post;
    // Store column count for displaying the grid
    if (empty($woocommerce_loop['columns'])) {
        $woocommerce_loop['columns'] = apply_filters('loop_shop_columns', 4);
    }
    if ($woocommerce_loop['columns'] == '3') {
        $productimgwidth = 365;
    } else {
        $productimgwidth = 268;
    }
    if (isset($virtue['product_img_resize']) && $virtue['product_img_resize'] == 0) {
        $resizeimage = 0;
    } else {
        $resizeimage = 1;
    }
    if ($resizeimage == 1) {
        if (has_post_thumbnail()) {
            $image_id = get_post_thumbnail_id($post->ID);
            $product_image_array = wp_get_attachment_image_src($image_id, 'full');
            $product_image_url = $product_image_array[0];
            // Make sure there is a copped image to output
            $image_product = aq_resize($product_image_url, $productimgwidth, $productimgwidth, true);
            if (empty($image_product)) {
                $image_product = $product_image_url;
            }
            // Get srcset
            $image_meta = get_post_meta($image_id, '_wp_attachment_metadata', true);
            $img_srcset = wp_calculate_image_srcset(array($productimgwidth, $productimgheight), $product_image_url, $image_meta, $image_id);
            // Get alt and fall back to title if no alt
            $alt_text = get_post_meta($image_id, '_wp_attachment_image_alt', true);
            if (empty($alt_text)) {
                $alt_text = get_the_title();
            }
            ?>
 
                <img width="<?php 
            echo esc_attr($productimgwidth);
            ?>
" height="<?php 
            echo esc_attr($productimgheight);
            ?>
" 
                src="<?php 
            echo esc_url($image_product);
            ?>
"
                <?php 
            if (!empty($img_srcset)) {
                ?>
                srcset="<?php 
                echo esc_attr($img_srcset);
                ?>
"
                sizes="(max-width: <?php 
                echo esc_attr($productimgwidth);
                ?>
px) 100vw, <?php 
                echo esc_attr($productimgwidth);
                ?>
px" 
                <?php 
            }
            ?>
                class="attachment-shop_catalog size-<?php 
            echo esc_attr($productimgwidth . 'x' . $productimgheight);
            ?>
 wp-post-image" 
                alt="<?php 
            echo esc_attr($alt_text);
            ?>
">
        <?php 
        } elseif (woocommerce_placeholder_img_src()) {
            echo woocommerce_placeholder_img('shop_catalog');
        }
    } else {
        echo '<div class="kad-woo-image-size">';
        echo woocommerce_template_loop_product_thumbnail();
        echo '</div>';
    }
}
예제 #9
0
 /**
  * Get wp 4.4 responsive solution, return the srcset and sizes attributes in a string which starts with
  * a space
  *
  * @param $url
  * @return string
  */
 public static function responsive_wp($url)
 {
     $responsive = '';
     $srcset = false;
     $sizes = false;
     $image_size = SwiftyImageFunctions::$image_size;
     // only for wp 4.4+
     if (isset($url) && function_exists('wp_calculate_image_srcset')) {
         $attach_id = SwiftyImageFunctions::get_attachment_id_from_url($url);
         if ($attach_id) {
             $image = SwiftyImageFunctions::get_attachment_image_src_wp($attach_id, $image_size, false);
             if ($image) {
                 list($src, $width, $height) = $image;
                 $image_meta = wp_get_attachment_metadata($attach_id);
                 if (is_array($image_meta)) {
                     $size_array = array(absint($width), absint($height));
                     // wp_calculate_image_srcset will only return sizes smaller than 1600
                     $srcset = wp_calculate_image_srcset($size_array, $src, $image_meta, $attach_id);
                     $sizes = wp_calculate_image_sizes($size_array, $src, $image_meta, $attach_id);
                 }
             }
         }
     }
     if ($sizes && $srcset) {
         $responsive = ' srcset="' . esc_attr($srcset) . '" sizes="' . esc_attr($sizes) . '"';
     }
     return $responsive;
 }
                if ($home_sidebar == true) {
                    $textsize = 'tcol-md-12 tcol-sm-12 tcol-ss-12';
                    $imagesize = 'tcol-md-12 tcol-sm-12 tcol-ss-12';
                } else {
                    $textsize = 'tcol-md-7 tcol-sm-12 tcol-ss-12';
                    $imagesize = 'tcol-md-5 tcol-sm-12 tcol-ss-12';
                }
                $image_id = get_post_thumbnail_id($post->ID);
                $image_url = wp_get_attachment_image_src($image_id, 'full');
                $thumbnailURL = $image_url[0];
                $image = aq_resize($thumbnailURL, $img_width, 270, true);
                if (empty($image)) {
                    $image = $thumbnailURL;
                }
                $image_meta = get_post_meta($image_id, '_wp_attachment_metadata', true);
                $img_srcset = wp_calculate_image_srcset(array($img_width, '270'), $image, $image_meta, $image_id);
                ?>
									<div class="<?php 
                echo esc_attr($imagesize);
                ?>
">
									 	<div class="imghoverclass">
			                           		<a href="<?php 
                the_permalink();
                ?>
" title="<?php 
                the_title();
                ?>
">
			                           			<img src="<?php 
                echo esc_url($image);