• Is it possible to show featured image (medium sized) relative URL path (relative to the website root)?

    For example, this is what I need “uploads/2014/07/image-600×400.png”

    This is what I tried so far:

    if ( has_post_thumbnail()) {
    $medium_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'medium');
    echo $medium_image_url[0];  }

    This actually works quite fine, I get path of medium sized featured image, but not relative path, I get it with domain name also, and I need it without domain name.

    After that I tried something like this (global wordpress sql data base query) to show relative path of featured image:

    global $wpdb;
    $the_thumbnail_id = get_post_thumbnail_id($post->ID);
    $the_thumbnail_name = $wpdb->get_var( "SELECT meta_value FROM $wpdb->postmeta WHERE post_id = '$the_thumbnail_id' AND meta_key = '_wp_attached_file'" );
    //
    echo $the_thumbnail_name;

    This is also working fine, I get just relative path, but not of “medium” sized image which I need, I get path of “full” sized featured image.

    Any help, maybe redesign (some parameter for “medium” size) for second function, or some new function?

    Best regards.

Viewing 7 replies - 1 through 7 (of 7 total)
  • I haven’t tested this, but maybe you can just use PHP’s parse_url to get the path from the URL:

    if ( has_post_thumbnail()) {
    $medium_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'medium');
    echo parse_url($medium_image_url[0], PHP_URL_PATH);  }

    Thread Starter Advanced SEO

    (@jole5)

    Thanks, it works.

    But, I also get sub-folder of wordpress installation like:

    /wp-install-folder/uploads/2014/07/image-600×400.png”

    Is there any way to trim (cut off) some part of url, for example I need just like this:

    “uploads/2014/07/image-600×400.png”

    So, without WP install sub folder and without first slash (/) at the beginning of upload folder (without “/wp-install-folder/”).

    Could you please look at what I tried so far:

    if ( has_post_thumbnail()) {
      $domain = get_site_url(); // returns domain like https://www.my-site-domain.com
      $relative_url = str_replace( $domain, '', $medium_image_url[0] );
      echo $relative_url;
    }

    This is working like this:

    /uploads/2014/07/image-600×400.png”

    So, any help to remove just first slash (“/“) before “uploads” for second function or modification of yours?

    How about just doing this:

    if ( has_post_thumbnail()) {
      $medium_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'medium');
      echo substr($medium_image_url[0], strpos($medium_image_url[0], 'uploads'), strlen($medium_image_url[0]));
    }

    Thread Starter Advanced SEO

    (@jole5)

    Thank you, it works exactly what I wish.

    Now, I wish to use it as function inside function to get image file size of medium sized featured image.

    function getSize($file){
    $bytes = filesize($file);
    $s = array('b', 'Kb', 'Mb', 'Gb');
    $e = floor(log($bytes)/log(1024));
    return sprintf('%.2f '.$s[$e], ($bytes/pow(1024, floor($e))));}
    
    // echo size
    echo getSize("uploads/2014/07/image-600x400.png");

    I tried to make function and put it on place of manually entered image path above, but without success.

    Could you please make it (combine it) for me?

    Your function is using the PHP filesize method, which will need the complete path to the file. Depending on how your server account is set up, something like:

    /home/username/public_html/wp-content/uploads/2014/07/image-600x400.png

    Otherwise, it won’t be able to find the file. You should also check for the existence of the file in your code so you don’t end up throwing a bunch of 404 errors if the path is wrong, but you can use the errors to troubleshoot your own code while you’re testing it.

    You could use something like this in your function:

    $pathtofile = get_home_path() . 'wp-content/' . $file;
    if (is_file($pathtofile)) {
      $bytes = filesize($pathtofile);
    }

    Thread Starter Advanced SEO

    (@jole5)

    I tried to make function to get absolute path like you suggested:

    <?php
    //Absolute path
    function medium_featured_image_absolute_path() {
       if ( has_post_thumbnail()) {
        $domain = get_site_url();
    	$path_to_www_folder = getcwd();
    	$medium_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'medium');
        $absolute_url = str_replace( $domain . '', $path_to_www_folder, $medium_image_url[0] );
        return $absolute_url;
       }
    }
    ?>

    And I checked if it shows right path with:

    <?php
    echo medium_featured_image_absolute_path();
    ?>

    What I get is right absolute path on server to image file.

    Than I tried to use file_size function to get file size of that file:

    <?php
        function file_size($url){
            $size = filesize($url);
            if($size >= 1073741824){
                $fileSize = round($size/1024/1024/1024,1) . ' GB';
            }elseif($size >= 1048576){
                $fileSize = round($size/1024/1024,1) . ' MB';
            }elseif($size >= 1024){
                $fileSize = round($size/1024,1) . ' KB';
            }else{
                $fileSize = $size . ' bytes';
            }
            return $fileSize;
        }
    
    echo file_size("$get_featured_image_custom_path_full");
    
    ?>

    But, results is ‘ bytes’ (like it is 0 bytes file).

    How to make it work?

    I suspect you aren’t correctly passing the path you get from the function medium_featured_image_absolute_path to your function file_size. You don’t show that part of your code, so I can’t tell.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Get relative path of mediul sized featured image?’ is closed to new replies.