如何使用PHP从WordPress调整大小的图像中获取完整大小的图像URL。

huangapple go评论116阅读模式
英文:

How to get full size image url from WordPress sized image with PHP

问题

以下是您要翻译的内容:

"使用了两种类似问题的两种方法:https://stackoverflow.com/questions/28945473/get-all-images-from-wordpress-posthttps://stackoverflow.com/questions/43677661/get-all-images-attached-to-any-post"

"我已经想出了两种代码。问题在于其中一种使用了 wp_get_attachment_image_url 并获取了附加到帖子的所有图像的全尺寸,但它不会动态更改,这意味着如果您从 WordPress 帖子中删除图像,它仍然会保留在那里。这个代码如下:"

"另一个使用正则表达式的函数来获取图像地址,来自 https://stackoverflow.com/questions/28945473/get-all-images-from-wordpress-post,但问题是它没有获取到全尺寸的图像。代码如下:"

"函数 functions.php 中的代码:"

"single.php 中的代码:"

"我的问题是如何将这两种方法结合起来,以动态获取帖子中的所有图像并获取全尺寸。我已经了解到,使用 wp_get_attachment_image_url 一旦将图像附加到帖子,该图像将会与此帖子粘在一起,无法取消附加。"

"此外,使用正则表达式通过 HTML 获取图像地址,其格式如下:"

"它似乎获取了缩略图版本的图像 300*125 从源中,而不是全尺寸。有关如何获取全尺寸的任何想法吗?"

"谢谢!"

英文:

After seening to similar questions <https://stackoverflow.com/questions/28945473/get-all-images-from-wordpress-post> and <https://stackoverflow.com/questions/43677661/get-all-images-attached-to-any-post>

I have come up with two codes. The problem is that one uses wp_get_attachment_image_url and gets all images attached to a post in full size but it does not change dynamically, meaning that if you remove the image from your WordPress post it will still be there. The code for this one is:

&lt;?php get_header(); ?&gt;
&lt;div id=&quot;main&quot;&gt;

    &lt;header class=&quot;post-header single-header&quot;&gt;
        &lt;div class=&quot;container&quot;&gt;
            &lt;h1 class=&quot;post-title text-center single__header_title&quot;&gt;&lt;?php the_title(); ?&gt;&lt;/h1&gt;
            &lt;p class=&quot;post-excerpt text-center single__header_text&quot;&gt;&lt;?php the_excerpt(); ?&gt;&lt;/p&gt;
            &lt;ul class=&quot;tag post-categoty tag-lg&quot;&gt;
                &lt;li&gt;
                    &lt;?php esc_html_e(&#39;Category: &#39;, &#39;seedwise&#39;); ?&gt; &lt;?php the_category(&#39; &#39;); ?&gt;
                &lt;/li&gt;
            &lt;/ul&gt;
            &lt;ul class=&quot;tag post-tags tag-lg&quot;&gt;
                &lt;li&gt;&lt;?php esc_html_e(&#39;Tags: &#39;, &#39;seedwise&#39;); ?&gt; &lt;?php custom_display_tags(&#39;&#39;, &#39;, &#39;) ?&gt;&lt;/li&gt;
            &lt;/ul&gt;
        &lt;/div&gt;
    &lt;/header&gt;

    &lt;div class=&quot;container&quot;&gt;
        &lt;?php while (have_posts()):
            the_post();
        ?&gt;

            &lt;div class=&quot;gallery&quot;&gt;
                &lt;div class=&quot;container&quot;&gt;
                    &lt;div class=&quot;row&quot;&gt;
                        &lt;?php
                        $post_id = get_the_ID();
                        $image_query = new WP_Query(array(
                            &#39;post_type&#39; =&gt; &#39;attachment&#39;,
                            &#39;post_status&#39; =&gt; &#39;inherit&#39;,
                            &#39;post_mime_type&#39; =&gt; &#39;image&#39;,
                            &#39;posts_per_page&#39; =&gt; -1,
                            &#39;post_parent&#39; =&gt; $post_id
                        ));

                        if ($image_query-&gt;have_posts()) {
                            while ($image_query-&gt;have_posts()) {
                                $image_query-&gt;the_post();
                                $full_size_image_url = wp_get_attachment_url(get_the_ID()); // Get the full-size image URL
                                $thumbnail_image_url = wp_get_attachment_image_url(get_the_ID(), &#39;large&#39;); // Get the thumbnail image URL
                                $image_alt = get_post_meta(get_the_ID(), &#39;_wp_attachment_image_alt&#39;, true);
                                ?&gt;
                                &lt;div class=&quot;col-md-6 col-sm-12&quot;&gt;
                                    &lt;div class=&quot;gallery__row&quot;&gt;
                                        &lt;a data-fslightbox href=&quot;&lt;?php echo esc_url($full_size_image_url); ?&gt;&quot; class=&quot;gallery__link&quot; data-lightbox=&quot;image-gallery&quot;&gt;
                                            &lt;figure class=&quot;gallery__thumb&quot;&gt;
                                                &lt;img src=&quot;&lt;?php echo esc_url($thumbnail_image_url); ?&gt;&quot; alt=&quot;&lt;?php echo esc_attr($image_alt); ?&gt;&quot; class=&quot;gallery__image&quot;&gt;
                                                &lt;figcaption class=&quot;gallery__caption&quot;&gt;&lt;?php echo esc_html(get_theme_mod(&#39;portrait_by_settings&#39;, &#39;Portrait by: Glauco Paganotti&#39;)); ?&gt;&lt;/figcaption&gt;
                                            &lt;/figure&gt;
                                        &lt;/a&gt;
                                    &lt;/div&gt;
                                &lt;/div&gt;
                                &lt;?php
                            }
                            wp_reset_postdata(); // Reset the image attachment query
                        }
                        ?&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;

        &lt;?php endwhile; ?&gt;
    &lt;/div&gt;
&lt;/div&gt;

&lt;?php get_footer(); ?&gt;

The other one uses the function with a regular expression from <https://stackoverflow.com/questions/28945473/get-all-images-from-wordpress-post> the problem is that it does not fetch the full size image. The code is:
the function on functions.php:

function fpw_show_full_size_images( $content ) {
    global $wp_query;
    if ( isset( $wp_query-&gt;query_vars[&#39;images&#39;] ) ) {
        preg_match_all( &#39;/&lt;img[^&gt;]+src=[\&#39;&quot;]([^\&#39;&quot;]+)[\&#39;&quot;][^&gt;]*&gt;/i&#39;, $content, $matches );
        $image_urls = $matches[1];
        $numberOfImages = count( $image_urls );
        
        if ( 0 == $numberOfImages ) {
            $out = &#39;&lt;h3&gt;No images&lt;/h3&gt;&#39;;
        } else {
            $out = &#39;&lt;h3&gt;Number of images: &#39; . $numberOfImages . &#39;&lt;/h3&gt;&#39;;
            $out .= &#39;&lt;table style=&quot;width:100%&quot;&gt;&#39;;
            $i = 0;
            foreach ( $image_urls as $img_url ) {
                if ( 0 == $i )
                    $out .= &#39;&lt;tr&gt;&#39;;
                $out .= &#39;&lt;td style=&quot;width:100%&quot;&gt;&lt;img src=&quot;&#39; . $img_url . &#39;&quot; /&gt;&lt;/td&gt;&#39;;
                $i++;
                if ( 4 == $i ) {
                    $i = 0;
                    $out .= &#39;&lt;/tr&gt;&#39;;
                }
            }
            if ( 0 &lt; $i )
                $out .= &#39;&lt;/tr&gt;&#39;; 
            $out .= &#39;&lt;/table&gt;&#39;; 
        }
        return $out;
    }
    return $content;
}
add_filter( &#39;the_content&#39;, &#39;fpw_show_full_size_images&#39; );

function fpw_images_parameter( $qvars ) {
    $qvars[] = &#39;images&#39;;
    return $qvars;
}
add_filter( &#39;query_vars&#39;, &#39;fpw_images_parameter&#39; );

the code for single.php:

&lt;?php get_header(); ?&gt;
&lt;div id=&quot;main&quot;&gt;

    &lt;header class=&quot;post-header single-header&quot;&gt;
        &lt;div class=&quot;container&quot;&gt;
            &lt;h1 class=&quot;post-title text-center single__header_title&quot; &gt;&lt;?php the_title(); ?&gt;&lt;/h1&gt;
            &lt;p class=&quot;post-excerpt text-center single__header_text&quot;&gt;&lt;?php the_excerpt(); ?&gt;&lt;/p&gt;
            &lt;ul class=&quot;tag post-categoty tag-lg&quot;&gt;
                &lt;li&gt;
                    &lt;?php esc_html_e( &#39;Category: &#39;, &#39;seedwise&#39; ); ?&gt; &lt;?php the_category( &#39; &#39; ); ?&gt;
                &lt;/li&gt;
            &lt;/ul&gt;
            &lt;ul class=&quot;tag post-tags tag-lg&quot;&gt;
                &lt;li&gt;&lt;?php esc_html_e( &#39;Tags: &#39;, &#39;seedwise&#39; ); ?&gt; &lt;?php custom_display_tags( &#39;&#39;, &#39;, &#39; ) ?&gt;&lt;/li&gt;
            &lt;/ul&gt;
        &lt;/div&gt;
    &lt;/header&gt;
    
    &lt;div class=&quot;container&quot;&gt;
        &lt;?php while( have_posts() ):
                the_post();
        ?&gt; 
        
        &lt;div class=&quot;gallery&quot;&gt;
            &lt;div class=&quot;container&quot;&gt;
                &lt;div class=&quot;row&quot;&gt;
                    &lt;?php
                    $content = get_the_content(); // Get the content of the post
                    preg_match_all( &quot;/&lt;img[^&gt;]+\&gt;/i&quot;, $content, $matches );
                    if (empty($matches[0])) {
                        echo &#39;&lt;p&gt;No images found in the content.&lt;/p&gt;&#39;;
                    } else {
                        foreach ($matches[0] as $image_tag) {
                            preg_match( &quot;/src=[&#39;\&quot;](.*?)[&#39;\&quot;]/&quot;, $image_tag, $src_match );
                            $image_url = $src_match[1];
                            $image_alt = get_post_meta( attachment_url_to_postid($image_url), &#39;_wp_attachment_image_alt&#39;, true );
                            ?&gt;
                            &lt;div class=&quot;col-md-6 col-sm-12&quot;&gt;
                                &lt;div class=&quot;gallery__row&quot;&gt;
                                    &lt;a data-fslightbox href=&quot;&lt;?php echo esc_html($image_url); ?&gt;&quot; class=&quot;gallery__link&quot; data-lightbox=&quot;image-&lt;?php echo esc_html(attachment_url_to_postid($image_url)); ?&gt;&quot;&gt;
                                        &lt;figure class=&quot;gallery__thumb&quot;&gt;
                                            &lt;img src=&quot;&lt;?php echo esc_html($image_url); ?&gt;&quot; alt=&quot;&lt;?php echo esc_html($image_alt); ?&gt;&quot; class=&quot;gallery__image&quot;&gt;
                                            &lt;figcaption class=&quot;gallery__caption&quot;&gt;&lt;?php echo esc_html(get_theme_mod(&#39;portrait_by_settings&#39;, &#39;Portrait by: Glauco Paganotti&#39;)); ?&gt;&lt;/figcaption&gt;
                                        &lt;/figure&gt;
                                    &lt;/a&gt;
                                &lt;/div&gt;
                            &lt;/div&gt;
                            &lt;?php
                        }
                    }
                    ?&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
        
        &lt;?php endwhile; ?&gt;
    &lt;/div&gt;
&lt;/div&gt;

&lt;?php get_footer(); ?&gt;

My question is how to blend these two approaches and get dynamically all images from a post in full size. I have understood that by using wp_get_attachment_image_url once an image attached to a post will be sticky to this post and cannot be unattached.

Also, using the regular expression to get the the image address throught html its passing the following formart:

&lt;img src=&quot;http://glauco.local/wp-content/uploads/2023/07/Opera-House-09-300x125.jpg&quot; alt=&quot;&quot; class=&quot;gallery__image&quot;&gt;

It seems that it is getting the a thumbnail version of the image 300*125 from the source insted of the full size. Any ideas on how to get the full size?
Cheers!

答案1

得分: 1

获取WP 大小图像URL的完整尺寸,您可以使用以下正则表达式:

$image_url = 'http://glauco.local/wp-content/uploads/2023/07/Opera-House-09-300x125.jpg';

// 从URL中删除WP尺寸
$image_url_full = preg_replace('~-[0-9]+x[0-9]+.~', '.', $image_url);

echo '<pre>' . print_r( $image_url_full, true ) . '</pre>';

您将获得完整尺寸的图像URL:

http://glauco.local/wp-content/uploads/2023/07/Opera-House-09.jpg
英文:

To get the full size from a WP sized image URL, you can use the following regular expression :

$image_url = &#39;http://glauco.local/wp-content/uploads/2023/07/Opera-House-09-300x125.jpg&#39;

// Remove WP dimensions from URL
$image_url_full = preg_replace(&#39;~-[0-9]+x[0-9]+.~&#39;, &#39;.&#39;, $image_url);

echo &#39;&lt;pre&gt;&#39;. print_r( $image_url_full, true ) . &#39;&lt;/pre&gt;&#39;;

You will get the full size image URL:

http://glauco.local/wp-content/uploads/2023/07/Opera-House-09.jpg

huangapple
  • 本文由 发表于 2023年8月10日 19:44:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76875432.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定