A possible solution:
Goal: Don’t use add_image_size()
to show a custom sized image.
If you calculate the image without saving it, all requests will sum up to a huge page load due to repeated unnecessary processing time. So caching or saving the results should be an additional goal.
Add a image-processor.php in your template/plugin:
//Load your WordPress enviroment
$path = dirname(__FILE__);
$path = substr($path, 0, stripos($path, "wp-content") );
require( $path . "wp-load.php");
//TODO: filter and handle your $_GET params
$post_id = absint($_GET['post_id']);
$testing_img_url = wp_get_attachment_image_url(
get_post_thumbnail_id($post->ID), 'full'
);
if (!$testing_img_url)
$testing_img_url="http://domain.tld/path/to/defaul-image.jpg";
$img = wp_get_image_editor( $testing_img_url );
if ( ! is_wp_error( $img ) ) {
header('Content-type:'.finfo_file ($testing_img_url));
$img->resize( 500, NULL, false );
$img->set_quality( 100 );
$img->stream();
}
You can display the image in a loop now like this:
Template:
<img src="https://wordpress.stackexchange.com/questions/191522/<?= get_template_directory_uri()?>/image-processor.php?post_id=<?= get_the_ID()?>">
Plugin:
<img src="https://wordpress.stackexchange.com/questions/191522/<?= plugin_dir_path(__FILE__)?>image-processor.php?post_id=<?= get_the_ID()?>">
Possible improvements:
- Caching/saving the processed images.
- Better error handling.