I’m trying to ensure that all assets are the same size, no matter what resolution they are uploaded in. I’m stuck finding outdated info on the matter, I think.
I’ve gotten to the following snippet
if( get_the_post_thumbnail_url()){
$image = wp_get_image_editor(get_the_post_thumbnail_url());
if ( !is_wp_error( $image )){
$image->resize( 300, 300, true );
$image->save("image.jpg");
}
}
Which seems to work, but I have no idea how I get the resized URL (or insert it properly). As I want to add it to my page.
Something like
echo '<img src=" . $image.url() . ">";
Is what I’m thinking of.
Am I going about this wrong, or how should I continue from here?
Thanks!
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Why don’t you use this add_image_size( 'custom-thumbnail', 300, 300, true ); in funtions.php
and call it <img src="<?php echo get_the_post_thumbnail_url('custom-thumbnail'); ?>"> in your page?
Method 2
Maybe providing ID may help.
if( get_the_post_thumbnail_url()){
$image = wp_get_image_editor(get_the_post_thumbnail_url(get_the_ID()));
if ( !is_wp_error( $image )){
$image->resize( 300, 300, true );
$image->save("image.jpg");
}
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0