Use image url with add_image_size

I am looking for a way to use an image url with the WordPress function add_image_size, I have already setup an image size called 'custom-thumb' For example, instead of…

the_post_thumbnail( 'custom-thumb' );

Which would output the posts thumbnail at the custom size I specified, I want to do something like…

www.mydomain.com/image.jpg('custom-thumb');

Related posts

2 comments

  1. The pattern you posted…

    www.mydomain.com/image.jpg('custom-thumb');
    

    … is a bit odd. That would be pretty tricky. You’d need a PHP handler to load the image and you’d need to tell the server (Apache, Nginx, IIS, Whatever) to parse that file ending as PHP. Something like this would be simpler:

    www.mydomain.com/image.php?size=custom-thumb
    

    You would still need to create a PHP handler script to read the URL, parse the GET string, and display the image.

    You could probably get something like this…

    www.mydomain.com/image/custom-thumb-X
    

    … working with an endpoint.

    However, the easiest thing to do is use wp_get_attachment_image_src to create the URL. I don’t know if that is an option for you but there is an example in the Codex:

    <?php 
    $attachment_id = 8; // attachment ID
    
    $image_attributes = wp_get_attachment_image_src( $attachment_id ); // returns an array
    ?> 
    
    <img src="<?php echo $image_attributes[0]; ?>" width="<?php echo $image_attributes[1]; ?>" height="<?php echo $image_attributes[2]; ?>">
    
  2. TimThumb (source code) is probably what you think you need, and probably good enough now, considering that it’s been a while since it was last exploited. Like anything else, it comes with its own pros/cons (just google ’em).

    But you should know that once you get the hang of add_image_size and the_post_thumbnail functions you’ll never have to go back.

    PS: Besides it looks like an XY problem. So, considering that you think X is the solution to problem Y, why don’t you tell us more about Y, and see if we can suggest a better alternative to X?

Comments are closed.