The MU forums have moved to WordPress.org

Uploaded image path generated incorrectly by (4 posts)

  1. boatwizard
    Member
    Posted 16 years ago #

    I am using some code to automatically retrieve the first image from my posts and display it, I am also using timthumb to resize these images into thumbnails. I have got everything setup correctly as the default image shows, but when the code tries to work with a image uploaded through the media uploader to the 'files' folder, the URL doesn't work. This is what is created:

    http://news.boatwizardwebsolutions.co.uk/cms/wp-content/themes/bc_features/scripts/timthumb.php?src=http://news.boatwizardwebsolutions.co.uk/cms/files/2009/04/istock_000001757949small.jpg&h=100&w=100&zc=1

    and I get the error message - file not found /cms/files/2009/04/istock_000001757949small.jpg. If I isolate the filepath at the end of the URL the image displays, but the timthumb resizer will not work unless I change the URL to this:

    http://news.boatwizardwebsolutions.co.uk/cms/wp-content/themes/bc_features/scripts/timthumb.php?src=http://news.boatwizardwebsolutions.co.uk/cms/wp-content/blogs.dir/2/files/2009/04/istock_000001757949small.jpg&h=100&w=100&zc=1

    The code to grab the first image is posted below, does anyone know how I could modify this so that it pulls the correct filepath?

    function catch_that_image() {
    global $post, $posts;
    $first_img = '';
    ob_start();
    ob_end_clean();
    $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
    $first_img = $matches [1] [0];

    if(empty($first_img)){ //Defines a default image
    $first_img = "http://news.boatwizardwebsolutions.co.uk/cms/wp-content/themes/bc_features/images/default.jpg";
    }
    return $first_img;
    }

    Thanks

  2. boatwizard
    Member
    Posted 16 years ago #

    I'm wondering if putting something like this into the code would work?

    str_replace("http://news.boatwizardwebsolutions.co.uk/cms/files/","http://news.boatwizardwebsolutions.co.uk/cms/wp-content/blogs.dir/2/files/",$matches);

    Trouble is I'm not sure where to put it, I'm pretty new to php!

  3. boatwizard
    Member
    Posted 16 years ago #

    Found the solution, used the str_replace once I'd found the right place for it. Here is the code for anyone else stuck in the same position:

    function catch_that_image() {
    global $post, $posts;
    $first_img = '';
    ob_start();
    ob_end_clean();
    $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
    $temp_img = $matches [1] [0];
    $first_img = str_replace("/cms/","/cms/wp-content/blogs.dir/2/",$temp_img);

    if(empty($first_img)){ //Defines a default image
    $first_img = "http://news.boatwizardwebsolutions.co.uk/cms/wp-content/themes/bc_features/images/default.jpg";
    }
    return $first_img;
    }

  4. gf8
    Member
    Posted 16 years ago #

    thank

About this Topic