S
S
someden2015-11-25 12:05:48
Images
someden, 2015-11-25 12:05:48

How to resize images in wordpress?

Task: display a cropped image of a certain size (sizes are set in the admin panel in pixels).
I tried the_post_thumbnail() function, but the image is not cropped, the proportions are preserved. I looked for more options, found the wp_get_image_editor() function , which can resize and crop images, but it doesn’t need the image ID, but the path to it on the server, and the function also returns the path to the image on the server, so I had to add a little (I took as a basis image_make_intermediate_size() function ):
the_post_thumbnail( array( 360, 360 ) );

function get_intermediate_size_image_src( $file, $width, $height, $crop = false ) {
  if ( $width || $height ) {
    $editor = wp_get_image_editor( $file );

    if ( is_wp_error( $editor ) || is_wp_error( $editor->resize( $width, $height, $crop ) ) )
      return false;

    $resized_file = $editor->save();

    if ( ! is_wp_error( $resized_file ) && $resized_file ) {
      $upload_dir = wp_upload_dir();
      return str_replace( $upload_dir[ 'basedir' ], $upload_dir[ 'baseurl' ], $resized_file[ 'path' ] );
    }
  }
  return false;
}

But it looks like a bicycle, isn't there an easier way to resize and crop an image in WordPress?
The option to set your own size with a crop (see below) is not suitable, since it is necessary for the admin to be able to set the desired size of the image for output in the admin panel in pixels.
add_image_size('square_small__crop', 360, 360, true);

Answer the question

In order to leave comments, you need to log in

3 answer(s)
L
lakegull, 2015-11-25
@lakegull

I do a resize in ACDSEE, batch processing, then upload where necessary. I didn't look for an easier way.

S
sera2007, 2015-11-26
@sera2007

I use timthumb
https://www.binarymoon.co.uk/projects/timthumb/
but this solution is not out of the box, though I don’t know the best option yet (

D
Dmitry, 2015-11-26
@dimasmagadan

timthumb cannot be used. It is full of holes, it is no longer updated, the project is abandoned.
try something from this page
www.deluxeblogtips.com/2015/01/resize-image-fly-wo...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question