B
B
Bestolkovy2016-08-17 16:13:17
PHP
Bestolkovy, 2016-08-17 16:13:17

Image resizing on the fly with WordPress?

Good afternoon!
I'm trying to change the image on the fly, using standard WordPress tools (without saving the thumbnail image). Having studied the code, I found a function that does this:

<?php
// загружаем объект
$image = wp_get_image_editor( 'http://wp-kama.ru/wp-content/uploads/2014/02/uka.png' );

// обрабатываем картинку
if ( ! is_wp_error( $image ) ) {
  // уменьшим её до размеров 80х80
  $image->resize( 80, 80, true );
  // сохраним в корне сайта под названием new_image.png
  $image->save( $_SERVER['DOCUMENT_ROOT'].'/new_image.png' );
}
?>

I changed it for myself, so that without any saving, it takes the image and displays it in the template, but for some reason it refuses to work with enviable persistence:
<?php
  $image = wp_get_image_editor( 'http://wp-kama.ru/wp-content/uploads/2014/02/uka.png' );

  if ( ! is_wp_error( $image ) ) {
    $image->resize( 80, 80, true );
?>

<img src="<?php echo $image; ?>">

<?php } ?>

After all, everything is right? Why doesn't it work? Is this a standard feature of WordPress?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Vorotnev, 2016-08-17
@HeadOnFire

wp_get_image_editor() requires a local image path , not a url. The URL may work, but not always - it depends on the server. Look at the $image dump, it's not a fact that the image from the remote server arrived in WP_Image_Editor and it was correctly initialized.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question