php 裁图,在PHP中裁剪图像

如果您正在尝试生成缩略图,您必须首先使用imagecopyresampled();.您必须调整图像大小,使图像较小边的大小等于缩略图的相应边。

例如,如果您的源图片为1280×800像素,而您的缩略图为200×150像素,则必须将图片大小调整为240×150像素,然后裁剪为200×150像素。这是为了使图像的宽高比不会改变。

以下是创建缩略图的一般公式:

$image = imagecreatefromjpeg($_GET['src']);

$filename = 'images/cropped_whatever.jpg';

$thumb_width = 200;

$thumb_height = 150;

$width = imagesx($image);

$height = imagesy($image);

$original_aspect = $width / $height;

$thumb_aspect = $thumb_width / $thumb_height;

if ( $original_aspect >= $thumb_aspect )

{

// If image is wider than thumbnail (in aspect ratio sense)

$new_height = $thumb_height;

$new_width = $width / ($height / $thumb_height);

}

else

{

// If the thumbnail is wider than the image

$new_width = $thumb_width;

$new_height = $height / ($width / $thumb_width);

}

$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );

// Resize and crop

imagecopyresampled($thumb,

$image,

0 - ($new_width - $thumb_width) / 2, // Center the image horizontally

0 - ($new_height - $thumb_height) / 2, // Center the image vertically

0, 0,

$new_width, $new_height,

$width, $height);

imagejpeg($thumb, $filename, 80);

没有测试这个,但它应该工作。

编辑

现在测试和工作。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值