php图片裁剪(使用GD库进行图片剪裁)

        根据指定的目标尺寸对原始图片进行等比例缩放,然后在新的画布上居中显示缩放后的图片,最后保存为剪裁后的图片文件。

<?php
// 原始图片路径
$sourceImage = 'path/to/original_image.jpg';
 
// 创建新的画布并加载源图片
$newWidth = 200; // 设置目标宽度
$newHeight = 150; // 设置目标高度
$targetImage = imagecreatetruecolor($newWidth, $newHeight);
$sourceImageObj = imagecreatefromjpeg($sourceImage);
 
// 将源图片按比例缩放到目标大小
if ($newWidth / imagesx($sourceImageObj) > $newHeight / imagesy($sourceImageObj)) {
    $widthRatio = $newWidth / imagesx($sourceImageObj);
} else {
    $heightRatio = $newHeight / imagesy($sourceImageObj);
}
$resizedImage = imagecreatetruecolor(imagesx($sourceImageObj), imagesy($sourceImageObj));
imagecopyresampled($resizedImage, $sourceImageObj, 0, 0, 0, 0, imagesx($sourceImageObj), imagesy($sourceImageObj), imagesx($sourceImageObj), imagesy($sourceImageObj));
 
// 在新的画布上居中显示缩放后的图片
$startX = (int)(($newWidth - imagesx($resizedImage)) / 2);
$startY = (int)(($newHeight - imagesy($resizedImage)) / 2);
imagecopy($targetImage, $resizedImage, $startX, $startY, 0, 0, imagesx($resizedImage), imagesy($resizedImage));
 
// 保存剪裁后的图片
$outputPath = 'path/to/cropped_image.jpg';
imagejpeg($targetImage, $outputPath);
imagedestroy($targetImage);
?>
 

  • 16
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这里提供一个基于jcrop图片裁剪演示,您可以在下面的代码框中运行它,体验一下: ```html <!DOCTYPE html> <html> <head> <title>Jcrop 图片裁剪演示</title> <meta charset="utf-8"> <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/jquery-jcrop/0.9.15/css/jquery.Jcrop.min.css" /> <script src="//code.jquery.com/jquery-3.6.0.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/jquery-jcrop/0.9.15/js/jquery.Jcrop.min.js"></script> <style> #preview { width: 200px; height: 200px; overflow: hidden; } </style> </head> <body> <h1>Jcrop 图片裁剪演示</h1> <form method="post" action="crop.php"> <input type="hidden" name="image" value="path/to/image.jpg"> <img src="path/to/image.jpg" id="cropbox"> <div id="preview"></div> <p> <button type="submit">保存裁剪结果</button> </p> </form> <script> $(function() { $('#cropbox').Jcrop({ aspectRatio: 1, boxWidth: 500, onChange: updatePreview, onSelect: updatePreview }); }); function updatePreview(coords) { var rx = 200 / coords.w; var ry = 200 / coords.h; $('#preview').css({ width: Math.round(rx * $('#cropbox').width()) + 'px', height: Math.round(ry * $('#cropbox').height()) + 'px', marginLeft: '-' + Math.round(rx * coords.x) + 'px', marginTop: '-' + Math.round(ry * coords.y) + 'px' }); $('input[name="x"]').val(coords.x); $('input[name="y"]').val(coords.y); $('input[name="width"]').val(coords.w); $('input[name="height"]').val(coords.h); } </script> </body> </html> ``` 这段代码会加载jcrop,并在页面中显示一张图片,您可以使用鼠标拖动裁剪框来选择需要裁剪的区域。当您选择好区域后,可以点击“保存裁剪结果”按钮,将裁剪结果提交给服务器。 为了使代码正常工作,您需要将上面代码中的 `path/to/image.jpg` 修改为您自己的图片路径,并创建一个名为 `crop.php` 的PHP脚本来处理裁剪结果。下面是一个裁剪处理脚本的示例: ```php // 获取裁剪参数 $x = $_POST['x']; $y = $_POST['y']; $width = $_POST['width']; $height = $_POST['height']; // 打开原始图像 $src = imagecreatefromjpeg($_POST['image']); // 创建新图像并复制裁剪区域 $dest = imagecreatetruecolor($width, $height); imagecopy($dest, $src, 0, 0, $x, $y, $width, $height); // 保存新图像 imagejpeg($dest, 'path/to/cropped/image.jpg'); // 释放内存 imagedestroy($src); imagedestroy($dest); ``` 这个脚本会从POST参数中获取裁剪参数和原始图像路径,然后使用GD创建新图像并保存。请注意,上述代码仅供参考,并且需要根据具体情况进行适当的修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值