WordPress.org

Ready to get started?Download WordPress

Forums

consistent thumbnail width? (5 posts)

  1. alfresco
    Member
    Posted 6 years ago #

    I'm attempting to set my default thumbnail size in such a way that my width will always be consistent - ideally 550px. An example can be found here: http://www.look-closer.net

    I have managed to set my long edge to whatever my heart desires, but am having trouble making a static base, and dynamic height while preserving whatever ratio my original file happens to have.

    I found this on another board:

    if ( $metadata['width'] >= $metadata['height'] ) {
    $max_side = apply_filters( 'wp_thumbnail_max_side_length', 150, $attachment_id, $file );
    } else {
    $max_side = apply_filters( 'wp_thumbnail_max_side_length', 200, $attachment_id, $file );
    }

    to replace line 153 in my image.php file, the line that determines long edge length. Unfortunately, this only works for images of a specific ratio, as far as I have been able to tell.

    I am using Barthelme 1.1 for a theme, more or less, which came with my WP install. You can view my page in its infancy at alfresco.geminetix.com. I've just started playing with wordpress, so all the images are random pictures I've taken, with pretty random comments, just to get an idea of layout.

    I appreciate any help- and hurrah for a first time post!

  2. forresto
    Member
    Posted 6 years ago #

    Make thumbsize.php in your plugins directory and enable this plugin:

    <?php
    
    /*
    Plugin Name: Thumbnail Sizes
    Plugin URI: http://wordpress.org/support/topic/147572
    Description: Fixed thumbnail width.
    Version: 1.0
    Author: Forrest O.
    Author URI: http://sembiki.com/
    */
    
    // 	change thumbnail to consistent width
    function custom_thumbsize( $size, $attachment_id, $file )
    {
    	$th_width = 550;
    
    	$image_attr = getimagesize( $file );
    
    	if ( $image_attr[0] < $image_attr[1] )
    	{
    		// 	image is portrait, make it fit in the width
    		return round( $image_attr[1] / $image_attr[0] * $th_width );
    	}
    	else
    	{
    		// 	image square or landscape, make it fit the width
    		return $th_width;
    	}
    }
    
    add_filter( 'wp_thumbnail_max_side_length', 'custom_thumbsize', 10, 3 );
    
    ?>

    Does this work for you? Doing it as a plugin gives you the advantage of not overwriting changes on an upgrade.

  3. forresto
    Member
    Posted 6 years ago #

    alternately, just add this to functions.php for your theme:

    // 	change thumbnail to consistent width
    function custom_thumbsize( $size, $attachment_id, $file )
    {
    	$th_width = 550;
    
    	$image_attr = getimagesize( $file );
    
    	if ( $image_attr[0] < $image_attr[1] )
    	{
    		// 	image is portrait, make it fit in the width
    		return round( $image_attr[1] / $image_attr[0] * $th_width );
    	}
    	else
    	{
    		// 	image square or landscape, make it fit the width
    		return $th_width;
    	}
    }
    
    add_filter( 'wp_thumbnail_max_side_length', 'custom_thumbsize', 10, 3 );
  4. forresto
    Member
    Posted 6 years ago #

    On testing, the plugin version works for me, but the functions.php version doesn't seem to...

  5. cosmocanuck
    Member
    Posted 5 years ago #

    Hi! How would I modify this code/plugin to only affect images with the "medium" size, not "thumbnail"? I'd like them all to be 250px wide, whether landscape, square or vertical.

Topic Closed

This topic has been closed to new replies.

About this Topic