简体   繁体   English

如何在Woocommerce中显示条件图像缩略图大小

[英]How to show conditional image thumbnail size in Woocommerce

As I'm developing a stock photo site I want to show my visitors a large preview of he image. 当我开发一个图片网站时,我想向访问者展示他的图像的大预览。 To avoid image theft I have watermarked my images except for the full size. 为避免图像被盗,除了全尺寸图像外,我都给图像加水印。 To serve a large preview I created a custom thumbnail size and change the code accordingly. 为了提供较大的预览,我创建了一个自定义缩略图大小,并相应地更改了代码。 See the thread here . 看到这里的线程。

The case is that sometimes an image is uploaded with smaller dimensions as the 3072 px (width or height). 情况是有时上传的图片尺寸较小,为3072像素(宽度或高度)。 What I want is that there is a fallback in this case to the thumbnail called 'large' which is one of the default Wordpress image sizes. 我想要的是在这种情况下存在称为“大”的缩略图的后备情况,该缩略图是默认的Wordpress图像大小之一。 As it is right now it is falling back to the full size image. 目前,它已退回到全尺寸图片。 It appears that no thumbnail of size 3072 px is generated when the full size image is smaller then 3072px. 当全尺寸图片小于3072像素时,似乎没有生成3072像素的缩略图。

I have tried to get it done by the piece of code below but can't get it right. 我试图通过下面的代码来完成它,但无法正确完成。 What's wrong? 怎么了?

$filemeta = wp_get_attachment_metadata( $post_thumbnail_id, FALSE ); 

$image_width       = $filemeta['width'];
$image_height      = $filemeta['height'];


if ($image_width < 3072 || $image_height < 3072){
$thumbnail_size    = apply_filters( 'woocommerce_product_thumbnails_large_size', 'large' );
} else {
$thumbnail_size    = apply_filters( 'woocommerce_product_thumbnails_large_size', 'preview' );
}

Found it. 找到了。

Changed this line in product_image.php 在product_image.php中更改了此行

$thumbnail_size    = apply_filters( 'woocommerce_product_thumbnails_large_size', 'preview' );

in

add_image_size( 'preview', $width = 3072, $height =3072, $crop = false ); 

$post_thumbnail_id = get_post_thumbnail_id( $post->ID );
$filemeta = wp_get_attachment_metadata( $post_thumbnail_id, FALSE );

if ($filemeta['width']>3071 || $filemeta['height']>3071){
    $thumbnail_size    = apply_filters( 'woocommerce_product_thumbnails_large_size', 'preview' );
}else{
    $thumbnail_size    = apply_filters( 'woocommerce_product_thumbnails_large_size', 'large' );
}

Maybe there is a smarter way but this works for me. 也许有一种更聪明的方法,但这对我有用。

EDIT 2018-06-26 I picked this project up after a while and noticed that it is not working anymore due to the WooCommerce update to 3.3. 编辑2018-06-26我过了一段时间才选择了这个项目,并注意到由于WooCommerce更新到3.3,该项目不再起作用。 WooCommerce has changes the way it handles product images. WooCommerce改变了处理产品图像的方式。 For me it means that I'm back at square one. 对我来说,这意味着我回到了第一广场。 WooCommerce displays the full-size image in the light box and that is not what I want. WooCommerce在灯箱中显示完整尺寸的图像,这不是我想要的。 Does someone have a clue? 有人有线索吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM