crop image to a certain size in wordpress

2019-07-18 03:06发布

I am trying to crop a image to a certain.

I have tried using

add_image_size( 'other', 105, 70, true );
 $imageString.= '<div><a href="' . $linkstring . '">' . get_the_post_thumbnail($post->ID, 'other;) . '</a></div>';

But it does not seem to crop to that exact dimension.

Any Ides?

标签: wordpress
3条回答
Viruses.
2楼-- · 2019-07-18 03:22

The functions related to image resizing and cropping are all placed in media.php.

For example, start reading about image_resize_dimensions which will also give you dimensions for cropping. Those dimensions can then be used with imagecopyresampled.

查看更多
疯言疯语
3楼-- · 2019-07-18 03:26

Generally you add image sizes into your functions.php file.

//post thumbnail support
add_action( 'after_setup_theme', 'theme_setup' );

function theme_setup() {
      if ( function_exists( 'add_theme_support' ) ) {
        add_image_size( 'other', 105, 70, true ); 
    }
}

Then, once in place, for all new image uploads wordpress will create an image at that size.

If you want to create these image sizes for already uploaded images, have a look at http://wordpress.org/extend/plugins/regenerate-thumbnails/

查看更多
【Aperson】
4楼-- · 2019-07-18 03:42

In my experience, get_the_post_thumbnail doesn't always work if you use custom image size added with add_image_size.

I'd advise you to use add_image_size, but get the image throught wp_get_attachment_image_src like this:

$imageurl = wp_get_attachment_image_src($attachment->ID, "other" );

$imageString.= '<div><a href="' . $linkstring . '"><img src="' . $imageurl[0] . '"/></a></div>';
查看更多
登录 后发表回答