• Hi there, I’m trying to find a way to have a set size and potentially hard crop images using the catch_that_image function.

    At the moment, I’m using featured images primarily, but have older posts which don’t have any. Because I have a section that show a random group of posts with images I decided to use catch_that_image in an if/else statement to make sure something showed up.

    Now for featured images I have the following code

    if ( function_exists( 'add_theme_support' ) ) {
    	add_theme_support( 'post-thumbnails' );
            set_post_thumbnail_size( 150, 150 ); }
    if ( function_exists( 'add_image_size' ) ) {
    	add_image_size( 'article', 600, 250, true );
    	add_image_size( 'recent', 150, 70, true );
    	add_image_size('homepage', 200, 200, true);
    	add_image_size('slider', 400, 190, true);

    So I really am hoping there is a way to use add_image_size or something similar to catch_that_image in order to do the 150×70 true option. I can set the size with html, but then it distorts the image pretty bad depending on original size.

    Also here is my catch_that_image code and the code I’m using to call that or the featured image.

    Catch_that_image code.

    <?php
    	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 = "/images/logo.png";
      }
      return $first_img;
    }
    ?>

    Code calling the images.

    <?php
    if ( has_post_thumbnail() ) { ?>
        <p class="image"><a title="<?php the_title(); ?>" href="<?php the_permalink(); ?>"><?php  the_post_thumbnail( 'recent' );  ?></a></p>
    <?php } else{ ?>
    	<p class="image"><a title="<?php the_title(); ?>" href="<?php the_permalink(); ?>"> <img src="<?php echo catch_that_image() ?>" alt="<?php the_title(); ?>" /></a></p>
    <?php } ?>

    I’ve tried just using the options from the add_image_size functions, but those don’t work, and I’ve tried different variations in the catch_that_image function as well.

    I appreciate any help or insight anyone has.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    add_image_size() only affects images uploaded after the image size is added. Existing posts and images remain unchanged. You can just use the browser to resize the image on the fly. You can get around the distortion issue by using PHP to calculate the proper height and width attributes based on the image’s actual aspect ratio.

    You can also use one of the image editor classes to crop and otherwise alter an image. The result can be saved to a file or simply streamed to output. Which one to use depends on what image module your server has installed – use either WP_Image_Editor_GD or WP_Image_Editor_Imagick.

    Documentation is pretty sparse, but you should be able to work it out between reading the source code and some experimenting.

    And if you need to resize images anew, even after they had been added, you can do it massively via plugins like https://www.ads-software.com/plugins/force-regenerate-thumbnails/ and https://www.ads-software.com/plugins/ajax-thumbnail-rebuild/.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘catch_that_image size and cropping’ is closed to new replies.