简体   繁体   English

调整the_post_thumbnail的大小以正确拉伸,wordpress

[英]resize the_post_thumbnail to stretch properly, wordpress

Hi Im using the_post_thumbnail for the nivo slider, my issue is that I cant seem to find a nice easy way to automatically resize the images to exact dimensions. 嗨,我在the nivo滑块上使用the_post_thumbnail ,我的问题是我似乎找不到一种简便的方法来自动将图像调整为确切的尺寸。 When I use add_image_size it doesnt stretch it correctly, it keeps the constraint on. 当我使用add_image_size时,它不能正确拉伸,因此会保持约束。 I dont want cropping or anything. 我不想播种。 I just want the user to upload the picture and not have to do anything after that. 我只希望用户上传图片,而后无需执行任何操作。

Its probably a stupid but I have googled it and even though there is lots about it, including various plugins, I cant seem to find a solid answer to this. 它可能是一个愚蠢的东西,但是我已经用谷歌搜索了它,尽管它有很多东西,包括各种插件,但我似乎找不到一个可靠的答案。

Thanks 谢谢

I think you need to clarify your question. 我认为您需要澄清您的问题。 If I understand correctly you want the uploaded image to be the exact image without cropping? 如果我理解正确,那么您希望上传的图像是准确的图像而不会裁剪吗? Make sure that the upload is saved before calling the_post_thumbnail function of the nivo slider. 在调用nivo滑块的the_post_thumbnail函数之前,请确保已保存上载。 That way you have the raw image to manipulate before the function gets it. 这样,您就可以在函数获得原始图像之前对其进行操作。

The add_image_size()-method has a flag for the crop method you wish to use. add_image_size()方法具有您要使用的裁剪方法的标志。 both of them will keep the proportions of the image intact however. 但是,它们都将保持图像的比例完整。

You could extend the functionality or build your own plugin to handle the resizing in the way you want - you may want to take a look at /wp-includes/media.php and a simple resizing class like https://github.com/mdular/ReSample.php/blob/master/ReSample.php 您可以扩展功能或构建自己的插件以所需的方式处理调整大小-您可能想看看/wp-includes/media.php和一个简单的调整大小类,例如https://github.com/ mdular / ReSample.php / blob / master / ReSample.php

I'm not a big fan of distorting the image proportions though. 我不是很喜欢扭曲图像比例。 If the client is unable to differentiate between a portrait or landscape aspect ratio then cropping the image rather than stretching it seems like the more elegant solution in most cases. 如果客户无法区分纵向或横向宽高比,则在大多数情况下,裁剪图像而不是拉伸图像似乎是更优雅的解决方案。

For a quick & dirty solution to your problem though i suggest the following: 为了快速解决您的问题,我建议您执行以下操作:

  1. add the image size and contrain it by the width OR height to fit your sliders width OR height (this example constrains only by width): 添加图像大小并用宽度或高度限制它,以适合滑块的宽度或高度(此示例仅按宽度限制):

    add_image_size( 'slider-image', 760, 9999 ); add_image_size('slider-image',760,9999); //760 pixels wide, "unlimited" height // 760像素宽,“无限”高度

  2. when outputting the image tags, force the dimensions to 760 x 474 in the image tag 输出图像标签时,在图像标签中强制尺寸为760 x 474

    < img src="" width="760" height="474" ...> <img src =“” width =“ 760” height =“ 474” ...>

This will leave you with image files that are potentially taller or wider than what you want but will be forced to the set dimensions in the browser, and therefore fit the slider. 这将使您留下的图像文件可能比您想要的更高或更大,但是将被强制设置为浏览器中的设置尺寸,因此适合滑块。 It's dirty, yes, but it will work with the tools you currently have. 是的,它很脏,但是可以与您当前拥有的工具一起使用。

Update in response to your comment: you can construct the image tag yourself by getting the raw image info. 根据您的评论进行更新 :您可以通过获取原始图像信息来自己构建图像标签。

1) grab the post meta, like so (assuming the_post() is already executed, it should be if you are in the while-loop): 1)抓取post元数据,像这样(假设the_post()已经执行,应该在while循环中):

$meta = get_post_meta($post->ID)

2) check for the thumbnail id to avoid empty output (and do steps 3 + 4 inside the if-block) 2)检查缩略图ID以避免空输出(并在if块内执行步骤3 + 4)

if(!empty($meta['_tumbnail_id'][0])

3) grab the actual thumbnail raw data - be sure to fill in the 'THUMBNAIL VERSION' (size) you want ('thumbnail', 'medium', etc.. or your own if you used add_image_size() in your functions.php) 3)获取实际的缩略图原始数据-确保填写所需的“缩略图版本”(尺寸)(“缩略图”,“中号”等。如果您在functions.php中使用了add_image_size(),则请自行填写) )

$image = wp_get_attachment_image_src( $meta['_thumbnail_id'][0], 'VERSION');

4) construct the image tag: 4)构造图片标签:

$image[0] = the src url
$image[1] = the width - you may use this
$image[2] = the height - you may wish to force this to your liking

hope this helps :) 希望这可以帮助 :)

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

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