php图像处理函数imagecopyresampled用法详解

参数详解

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:原图要载入的高度
    案例
    1、图像裁减

<?php
  $targ_w = $targ_h = 150; // 设置目标宽度与高度
  $jpeg_quality = 90; // 图片质量90,满分为100
  $src = 'demo_files/pool.jpg'; // 被处理的图片
  $img_r = imagecreatefromjpeg($src); // 获取原图
  $dst_r = ImageCreateTrueColor( $targ_w, $targ_h ); // 获取新图
  imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
  $targ_w,$targ_h,$_POST['w'],$_POST['h']); // 目标图 源图 目标X坐标点 目标Y坐标点 源的X坐标点 源的Y坐标点 目标宽度 目标高度 源图宽度 源图高度
  header('Content-type: image/jpeg');
  imagejpeg($dst_r,null,$jpeg_quality); // 输出图象到浏览器或文件
?>

2、重新取样

<?php
// 源文件
$filename = '1.jpg';
// 设置最大宽高
$width = 400;
$height = 400;
// Content type
header('Content-Type: image/jpeg');
// 获取新尺寸
list($width_orig, $height_orig) = getimagesize($filename);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
  $width = $height*$ratio_orig;
} else {
  $height = $width/$ratio_orig;
}
// 重新取样
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// 输出
imagejpeg($image_p, null, 100);
?>

原文来自https://www.jb51.net/article/98879.htm

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值