WordPress.org

Ready to get started?Download WordPress

Ideas

Image size as suffix instead of image dimensions

  1. Mdkart
    Member

    12345

    Here's my small hack for WordPress 3.5 so that image versions are named with suffix like that :

    PICTURENAME.jpg
    PICTURENAME-large.jpg
    PICTURENAME-medium.jpg
    PICTURENAME-thumbnail.jpg
    PICTURENAME-WidthxHeight.other.jpg

    In /wp-includes/class-wp-image-editor.php
    Line #308 (or near that)

    Change :

    public function get_suffix() {
    	if ( ! $this->get_size() )
    		return false;
    	return "{$this->size['width']}x{$this->size['height']}";
    }

    To :

    public function get_suffix() {
    	if ( ! $this->get_size() )
    		return false;
    	$size = array('large', 'medium', 'thumbnail');
    	foreach($size as $element) {
    		if( $this->size['width'] == intval(get_option($element.'_size_w')) || $this->size['height'] == intval(get_option($element.'_size_h')) )
    			return $element;
    	}
    	return "{$this->size['width']}x{$this->size['height']}.other";
    }
    }
    Posted: 1 year ago #
  2. Mdkart
    Member

    12345

    Can't edit post above but the last accolade "}" of the hack has to be deleted.
    the hack must end with only one "}".

    Posted: 1 year ago #
  3. Ipstenu (Mika Epstein)
    Administrator

    Do NOT edit CORE.

    This is terrible to suggest to people, because any upgrade will overwrite the changes. Please don't tell people this is how to 'fix' anything. The only time it's okay to edit core is if you're debugging and applying patches.

    Posted: 1 year ago #
  4. José Luís
    Member

    @Ipstenu,

    I do agree we should not edit core files. Apart losing the changes on an upgrade it is dangerous.

    But I (and as I can see, a few others too) really think there should be a hook (action of filter) to customize image suffixes.

    Could you please suggest this to dev team?

    About Mdkart's solution to the problem, I realy liked it. The only change I'd make would be on the last name convention: I'd put the "other" BEFORE the dimensions, like this:

    PICTURENAME-other-WIDTHxHEIGHT.jpg

    Posted: 1 year ago #
  5. José Luís
    Member

    @Mdkart, as I can see, jstnryan's solution doesn't work anymore. I hadn't time to check yet, but has that snippet (the switch) been replaced by this public function get_suffix?

    Does your solution work automatically with every uploaded image (or is it required to use the image editor)?

    If that is the case, is there a way to just add a customized version on the functions file and override the official one? I thought of something like (in /wp-includes/class-wp-image-editor.php):

    public function get_suffix() {
    	return my_get_suffix();
    }

    And, in the theme's functions.php:

    public function my_get_suffix() {
    	if ( ! $this->get_size() )
    		return false;
    	$size = array('large', 'medium', 'thumbnail');
    	foreach($size as $element) {
    		if( $this->size['width'] == intval(get_option($element.'_size_w')) || $this->size['height'] == intval(get_option($element.'_size_h')) )
    			return $element;
    	}
    	return "other-{$this->size['width']}x{$this->size['height']}";
    }
    Posted: 1 year ago #
  6. Ipstenu (Mika Epstein)
    Administrator

    Jose, then suggest a patch to core, but telling people that the fix is to edit core will make them miserable when they don't realize what that means. Fine for DEVS, not for the casual users :)

    That said, I suspect the answer would be no, since people change themes, and when they do that, the size of medium and large change. So how would your change compensate for that?

    Theme functions are per theme. You can override any function by, in your child theme, checking for it and using remove_action()

    Posted: 1 year ago #
  7. Pat Hawks
    Member

    12345

    But I (and as I can see, a few others too) really think there should be a hook (action of filter) to customize image suffixes.

    Why would there be a filter for every plugin or theme to create it's owned named version of the same image?
    If my theme creates it's own 150x150 thumbnail and calls it small-square and a theme creates another 150x150 thumbnail and calls it little-square, there would be two identical versions of the same image served to users.

    There is no need for that.

    Using something like JetPack's Photon, you can solve the problem of resizing images when a theme is changed without resorting to such a nasty hack.

    Posted: 1 year ago #
  8. Pat Hawks
    Member

    12345

    If you're only concerned with things running through the post editor, you could write a plugin to embed images using a Shortcode rather than a raw image tag.
    The Shortcode could reference the image ID and the desired size. That way, if you change the size later the images will resize as well, but you only ever have one copy of a given size hosted. The height and width attributes could also be updated this way.

    Posted: 1 year ago #
  9. José Luís
    Member

    @Ipstenu,

    Theme functions are per theme. You can override any function by, in your child theme, checking for it and using remove_action()

    I think that would help me, but as far as my (little) PHP knowledge goes, I cannot remove functions like this get_suffix because it is not an action, right? If I'm wrong, I'd be very happy to be pointed in the right direction...

    @Pat Hawks,

    The Shortcode could reference the image ID and the desired size. That way, if you change the size later the images will resize as well, but you only ever have one copy of a given size hosted. The height and width attributes could also be updated this way.

    I think you hit the bullseye! Including images via shortcode would be perfect!

    Do you know how to integrate such a plugin with WordPress Media Library? I think it would be easier to reuse a lot of features already implemented there (e.g. visual selection, alignment options and media buttons)? (Maybe that should be implemented in the core by default, who knows...)

    As I've said, I don't so much PHP knowledge (that's why I keep suggesting changes to WordPress). If anybody could point me in the right direction, I'd appreciate.

    Posted: 1 year ago #
  10. Pat Hawks
    Member

    12345

    A plugin could hook into the image_send_to_editor filter.
    Basically, it would just put all that information into a Shortcode.

    The Shortcode would just return the get_image_tag function.

    Looking at this, it really shouldn't be difficult to put the pieces together; I just don't think it belongs in core.

    Posted: 1 year ago #

RSS feed for this topic

Reply »

You must log in to post.

  • Rating

    12345
    9 Votes
  • Status

    Sorry, not right now