0

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!

2 Answers 2

0

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?

3
  • I tried added those two lines you mentioned. One in function.php and the other in my template. Nothing appears from that
    – Fluxian
    Commented Dec 5, 2020 at 16:10
  • I figured it out. Good tip with add_image_size that's definitely the way to go, thanks!
    – Fluxian
    Commented Dec 5, 2020 at 16:46
  • glad that help.
    – Tiyo
    Commented Dec 6, 2020 at 1:35
0

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");
    }
}
3
  • The issue is I don't know how to write the image to the page, it works just fine without an ID
    – Fluxian
    Commented Dec 5, 2020 at 13:59
  • What do you mean by write to the page. Commented Dec 5, 2020 at 14:01
  • I want to add the resized image to my page
    – Fluxian
    Commented Dec 5, 2020 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.