gpt4 book ai didi

image - 基于 url 的 wordpress 图片大小

转载 作者:行者123 更新时间:2023-12-04 06:30:25 25 4
gpt4 key购买 nike

我正在使用自定义字段来选择图像的 url。

我的客户正在插入和上传所有图像,所以这需要非常简单。这就是为什么我试图在幕后处理它。

我遇到的问题是全尺寸图像的 url 中的所有内容,这确实减慢了加载时间。

有没有办法可以根据完整大小的 url 插入缩略图或其他图像大小?

我已经尝试过这种工作方式,但我遇到的问题是有些图像没有相同的牙列,所以这需要更加动态地完成。

<? $reduceimage = str_replace('.jpg', '-330x220.jpg' , $defaultimage); ?>

最佳答案

我认为最好依靠wordpress的原生wp_get_attachment_image_src()函数来获取不同大小的图像。但要这样做,您需要附件 ID,而不是 url。
将url转换为ID的函数:

function fjarrett_get_attachment_id_by_url( $url ) {

// Split the $url into two parts with the wp-content directory as the separator
$parsed_url = explode( parse_url( WP_CONTENT_URL, PHP_URL_PATH ), $url );

// Get the host of the current site and the host of the $url, ignoring www
$this_host = str_ireplace( 'www.', '', parse_url( home_url(), PHP_URL_HOST ) );
$file_host = str_ireplace( 'www.', '', parse_url( $url, PHP_URL_HOST ) );

// Return nothing if there aren't any $url parts or if the current host and $url host do not match
if ( ! isset( $parsed_url[1] ) || empty( $parsed_url[1] ) || ( $this_host != $file_host ) ) {
return;
}

// Now we're going to quickly search the DB for any attachment GUID with a partial path match

// Example: /uploads/2013/05/test-image.jpg
global $wpdb;
$attachment = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->prefix}posts WHERE guid RLIKE %s;", $parsed_url[1] ) );

// Returns null if no attachment is found
return $attachment[0];
}

感谢 Frankie Jarrett .

然后您可以轻松获得其他尺寸:
$medium_image = wp_get_attachment_image_src( fjarrett_get_attachment_id_by_url($image_link), 'medium');

如果您需要此图片的链接:
$medium_image_link = $medium_image[0];
$html = '<img src="'.$medium_image_link.'" alt="" />';

关于image - 基于 url 的 wordpress 图片大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5487444/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com