6

Is there any way to generate thumbnails of the same dimensions, but with a different crop?

I want to do something like:

add_image_size( 'thumbnail_top', 360 , 180 , array ('center', 'top') ); 
add_image_size( 'thumbnail_middle', 360 , 180 , array ('center', 'center') ); 
add_image_size( 'thumbnail_bottom', 360 , 180 , array ('center', 'bottom') );

but there is nothing in the filename to define the crop, so the thumbnail file always gets overwritten with the last in the list of the same size, therefore in this example, 'thumbnail_top' will always display as 'thumbnail_bottom'.

6
  • You are correct. Currently, the last image size with the same dimensions will be the one saved. May 12 '15 at 19:16
  • Thanks, @NathanPowell - any strategies for a workaround? Only obvious one I can think of is to increment dimensions by a pixel for each crop, but this has other implications such as potentially incorrectly-sized images displayed to user. May 13 '15 at 8:43
  • I thought of that as a workaround as well. Honestly I would need to know the goal for doing this to begin with. May 13 '15 at 18:27
  • User uploads a single featured image which is displayed in the post template. A "letterbox" cropped version appears on the archive page. A drop-down menu has been implemented as a meta-box to select whether crop is taken from top, middle or bottom (depending on composition of image). Aim is to minimise requirements for external editing of images and need for uploading multiple images. May 14 '15 at 8:34
  • I think there was a hook to change the naming. Currently on the go, might be able to look into it later.
    – kraftner
    Mar 24 '17 at 17:49
1

You can define your own cropping sizes but you can create a function that accepts image size + image position and then load your images accordingly.

For example-

<?php
  $size = 'medium';

  $pos = array(
    'top' => '100',
    'left' => '100'
  );

  function load_image_with_pos( $img_id, $size, $pos ) {
    $img_src = wp_get_attachment_image_src( $img_id, $size );
    $new_img = array(
      'url' => $img_src,
      'size' => $size,
      'position' => $pos
    );

    return $new_img;
  }
?>

I hope it helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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