Resize Image Wordpress Script

Asked

Viewed 112 times

1

I found this script for wordpress, to grab the image of the post and add it in the wordpress theme.

View the script:

function catch_that_image() {
global $post, $posts;
$first_img = '';
$new_img_tag = "";

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 with 0 width
$new_img_tag = "<img src='/images/noimage.jpg' width='0px' class='alignleft' />";
}

 else{
 $new_img_tag = "<img src='" .  $first_img . "' width='100px' height='100px'    class='alignleft' />";
}

return $new_img_tag;
}

end of script

This little code we added to call the image in some theme location.

    <?php echo catch_that_image() ?>

As you can see the script leaves the image sized in 100x100, I would like to control the image size by this code :

      <?php echo catch_that_image() ?>

I need to leave the image on each page with different dimensions.

1 answer

2


How about passing the dimensions by parameter?

function catch_that_image($w, $h) {
    global $post, $posts;
    $first_img = '';
    $new_img_tag = "";

    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 with 0 width
        $new_img_tag = "<img src='/images/noimage.jpg' width='0px' class='alignleft' />";
    }

    else{
       $new_img_tag = '<img alt="' . $post->post_title . '" src="' . $first_img . '" width="' . $w . '" height="' . $h . '" class="alignleft" />';
    }

    return $new_img_tag;
}

Once so, just call the function <?php echo catch_that_image(150, 150) ?>, or with any other values you want.

EDIT

I added the tag alt, using the get_the_title() wordpress. As this code is (is what it seems to me at least) out of the loop, the the_title() would not work, as the documentation.

EDIT 2

After a more detailed analysis, it is much smarter to use $post->post_title than echo get_the_title($post->ID). It took me a while to understand what I had said myself...

  • tested here and it worked! Could you add the Alt and Title tag to the image? using <?php the_title() as a base? > from wordpress? t

  • Probably. See the edited answer, @user3192159. I haven’t tried it, but it should work fine

  • The alt code seems to have some error, it is not showing the title only a part of the title, example: Title blue cars, in the alt tag it appears alt="blue cars" src=".

  • @user3192159 put a echo before the get_the_title(). I edited the answer

  • when adding echo before get_the_title() the code shows an error.

  • Now that I’ve touched. Since you have the global $post, you can give a echo right in the title. echo $post->post_title;

  • Hello caio, blz, I found an error in the script, I had not noticed because I had not seen all the pages of the site. It turns out that articles that have an iframe or youtube embed, the script instead of taking the image of the article, it’s taking the link from the iframe src="//www.youtube.com/get_player" would not have the strength of the script to take only images Jpeg, jpg, png, gif of the articles and ignore iframe?

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.