php imagecopyresampled参数,PHP imagecopyresampled()用法及代码示例

imagecopyresampled()函数是PHP中的内置函数,用于将一个图像的矩形部分复制到另一幅图像,平滑地内插像素值,因此,尤其是减小图像的大小仍然保持很高的清晰度。

用法:

bool imagecopyresampled( resource $dst_image, resource $src_image, int $dst_x, int $dst_y, int $src_x, int $src_y, int $dst_w, int $dst_h, int $src_w, int $src_h )

参数:该函数接受上述和以下所述的十个参数:

$dst_image:它指定目标图像资源。

$src_image:它指定源图像资源。

$dst_x:它指定终点的x坐标。

$dst_y:它指定终点的y坐标。

$src_x:它指定源点的x坐标。

$src_y:它指定源点的y坐标。

$dst_w:它指定目标宽度。

$dst_h:它指定目标高度。

$src_w:它指定源宽度。

$src_h:它指定源高度。

返回值:如果成功,则此函数返回TRUE;如果失败,则返回FALSE。

下面给出的程序说明了PHP:Program 1中的imagecopyresampled()函数(将图像重采样为其宽度和高度的一半):

// Get dimensions of new image

list($width, $height) = getimagesize(

'https://media.geeksforgeeks.org/wp-content/uploads/20200123100652/geeksforgeeks12.jpg');

// Reduce width and height to half

$new_width = $width * 0.5;

$new_height = $height * 0.5;

// Resample the image

$image_p = imagecreatetruecolor($new_width, $new_height);

$image = imagecreatefromjpeg(

'https://media.geeksforgeeks.org/wp-content/uploads/20200123100652/geeksforgeeks12.jpg');

imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// Output the image to browser

header('Content-Type:image/jpeg');

imagejpeg($image_p, null, 100);

?>

输出:

a000dff3a0694e2ab4f394e51f5923c1.png

程序2(以固定的宽度和高度重新采样图像):

// Set a  fixed height and width

$width = 300;

$height = 300;

// Get image dimensions

list($width_orig, $height_orig) = getimagesize(

'https://media.geeksforgeeks.org/wp-content/uploads/20200123100652/geeksforgeeks12.jpg');

// Resample the image

$image_p = imagecreatetruecolor($width, $height);

$image = imagecreatefromjpeg(

'https://media.geeksforgeeks.org/wp-content/uploads/20200123100652/geeksforgeeks12.jpg');

imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

// Output the image

header('Content-Type:image/jpeg');

imagejpeg($image_p, null, 100);

?>

输出:

50effc5a3d6a1cf56cdf8f52ee16a423.png

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值