php图片缩放与裁剪初学小结

文章目录


1、运用imagecopyresampled函数
2、缩放操作
3、裁剪操作

1、imagecopyresampled的使用
imagecopyresampled
(1、目标函数资源
2、源图像资源<要采样的图片资源>
3、x(0,0指图左上角)
4、y(x,y确定一个坐标,坐标确定了把采样的部分放到目标图像资源的位置)
5、源x(0,0指图右上角)
6、源y(源x与源y确定一个坐标,你要采用的原图像资源的某个部分的起始位置)
7、w
8、h(weight与height确定了放到目标图像资源上面的尺寸)
9、源w
10、源h(源w与源h确定了采样原图像资源的某个部分))

2、$height=$width/($imgWidth/$imgHeight);使得图片整体不会被裁剪,缩放代码只需控制width即可

<?php
header('Content-type:image/jpeg');
$width=300;
$img=imagecreatefromjpeg('1/php1.jpg');
$imgWidth=imagesx($img);
$imgHeight=imagesy($img);
$height=$width/($imgWidth/$imgHeight);
$img1=imagecreatetruecolor(500,500);
imagecopyresampled($img1,$img,100,100,100,100,$width,$height,$imgWidth,$imgHeight);
imagejpeg($img1);
imagedestroy($img1);
imagedestroy($img);

在这里插入图片描述

<?php
header('Content-type:image/jpeg');
$width=200;
$img=imagecreatefromjpeg('1/php1.jpg');
$imgWidth=imagesx($img);
$imgHeight=imagesy($img);
$height=$width/($imgWidth/$imgHeight);
$img1=imagecreatetruecolor(500,500);
imagecopyresampled($img1,$img,100,100,100,100,$width,$height,$imgWidth,$imgHeight);
imagejpeg($img1);
imagedestroy($img1);
imagedestroy($img);

在这里插入图片描述
3、控制x、y与源x、源y可以进行裁剪

<?php
header('Content-type:image/jpeg');
$width=500;
$img=imagecreatefromjpeg('1/php1.jpg');
$imgWidth=imagesx($img);
$imgHeight=imagesy($img);
$height=$width/($imgWidth/$imgHeight);
$img1=imagecreatetruecolor(500,500);
imagecopyresampled($img1,$img,0,0,300,300,$width,$height,$imgWidth,$imgHeight);
imagejpeg($img1);
imagedestroy($img1);
imagedestroy($img);

在这里插入图片描述

<?php
header('Content-type:image/jpeg');
$width=500;
$img=imagecreatefromjpeg('1/php1.jpg');
$imgWidth=imagesx($img);
$imgHeight=imagesy($img);
$height=$width/($imgWidth/$imgHeight);
$img1=imagecreatetruecolor(500,500);
imagecopyresampled($img1,$img,330,330,0,0,$width,$height,$imgWidth,$imgHeight);
imagejpeg($img1);
imagedestroy($img1);
imagedestroy($img);

在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值