在wordpress中将图像裁剪为一定大小

时间:2012-01-26 00:22:24

标签: wordpress

我正在尝试裁剪图像。

我尝试过使用

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

但它似乎没有裁剪到那个确切的维度。

任何Ides?

3 个答案:

答案 0 :(得分:9)

通常,您将图像大小添加到functions.php文件中。

//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 ); 
    }
}

然后,一旦到位,对于所有新图像上传,wordpress将创建该大小的图像。

如果您要为已上传的图片创建这些图片尺寸,请查看http://wordpress.org/extend/plugins/regenerate-thumbnails/

答案 1 :(得分:1)

根据我的经验,如果您使用add_image_size添加自定义图像大小,则get_the_post_thumbnail并不总是有效。

我建议你使用add_image_size,但是像这样通过wp_get_attachment_image_src获取图像:

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

$imageString.= '<div><a href="' . $linkstring . '"><img src="' . $imageurl[0] . '"/></a></div>';

答案 2 :(得分:0)

与图像大小调整和裁剪相关的功能都放在media.php中。

例如,开始阅读有关image_resize_dimensions的内容,这也会为您提供裁剪尺寸。然后可以将这些尺寸与imagecopyresampled一起使用。

相关问题