代码部分
<?php
/**
* Created by PhpStorm.
* User: QLX
* Date: 2019/8/29
* Time: 10:16
* 图片的缩放和裁剪
*/
header('Content-Type:image/jpeg');
$img = imagecreatefromjpeg('./image/11.jpg');
$width = imagesx($img);
$height = imagesy($img);
$newImgWidth = $width / 2;
//假如宽缩小一半,让图片同比缩小
// $width/$heigh = $newImgWidth/$newImgHeight
$newImgHeight = ($height * $newImgWidth) / $width;
$newImg = imagecreatetruecolor($newImgWidth, $newImgHeight);
$colorFont = imagecolorallocate($img, 255, 255, 255);
$is_true = imagecopyresampled($newImg, $img, 0, 0, 0, 0, $newImgWidth, $newImgHeight, $width, $height);
//如果是需要实现裁剪 只需要根据需求改变imagecopyresampled() 里面的参数
if ($is_true) {
imagejpeg($newImg);
imagedestroy($newImg); //销毁照片
} else {
die('有错误');
}
缩放后的图片
![](https://img-blog.csdnimg.cn/20190830203736227.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1FMWDExOQ==,size_16,color_FFFFFF,t_70)