PHP - 使用 imagecopyresampled() 裁剪图像?

发布于 2024-10-17 03:42:57 字数 862 浏览 4 评论 0原文

我想使用 imagecreatetruecolor 裁剪图像,它总是裁剪它留下黑色空间,或者缩放太大。我希望图像的宽度恰好为 191 像素,高度为 90 像素,因此我还需要调整图像的大小以及裁剪,因为必须保持比例。好吧,有一些该项目的示例:

在此处输入图像描述

调整大小脚本(简化)如下所示:

$src_img=imagecreatefromjpeg($photoTemp);    
list($width,$height)=getimagesize($photoTemp);
$dst_img=imagecreatetruecolor(191, 90);
imagecopyresampled($dst_img, $src_img, 0, 0, $newImage['crop']['x'], $newImage['crop']['y'], $newImage['crop']['width'], $newImage['crop']['height'], $width, $height);

$ newImage['crop'] 数组包括:

['x'] => $_POST['inp-x']
['y'] => $_POST['inp-x']
['width'] => $_POST['inp-width']
['height'] => $_POST['inp-height']

但我得到的是:

在此处输入图像描述

任何人都看到,我是什么做错了吗?

谢谢,迈克。

I'd like to crop an image using imagecreatetruecolor and it always crops it leaving black spaces, or the zoom is too big. I want the image to be exactly 191px wide and 90px high, so I also need to resize the image, as well as crop, because the ratio has to be kept. Well, there are some samples of the project:

enter image description here

The resize script (simplified) goes like this:

$src_img=imagecreatefromjpeg($photoTemp);    
list($width,$height)=getimagesize($photoTemp);
$dst_img=imagecreatetruecolor(191, 90);
imagecopyresampled($dst_img, $src_img, 0, 0, $newImage['crop']['x'], $newImage['crop']['y'], $newImage['crop']['width'], $newImage['crop']['height'], $width, $height);

The $newImage['crop'] array includes:

['x'] => $_POST['inp-x']
['y'] => $_POST['inp-x']
['width'] => $_POST['inp-width']
['height'] => $_POST['inp-height']

But what I get is:

enter image description here

Anyone sees, what I'm doing wrong?

Thanks, Mike.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

廻憶裏菂餘溫 2024-10-24 03:42:58

尝试

<?php

$dst_img = imagecreatetruecolor($newImage['crop']['width'], $newImage['crop']['height']);

imagecopyresampled($dst_img, $src_img, 0, 0, $newImage['crop']['x'], $newImage['crop']['y'], 0, 0, $width, $height);

Try

<?php

$dst_img = imagecreatetruecolor($newImage['crop']['width'], $newImage['crop']['height']);

imagecopyresampled($dst_img, $src_img, 0, 0, $newImage['crop']['x'], $newImage['crop']['y'], 0, 0, $width, $height);
毁我热情 2024-10-24 03:42:58

好吧,我自己发现了问题,代码应该是这样的:

imagecopyresampled($dst_img, $src_img, 0, 0, $newImage['crop']['x'], $newImage['crop']['y'], $newImage['newWidth'], 191, 90, $newImage['crop']['height']); 

Ok, I found the problem myself, the code should be like this:

imagecopyresampled($dst_img, $src_img, 0, 0, $newImage['crop']['x'], $newImage['crop']['y'], $newImage['newWidth'], 191, 90, $newImage['crop']['height']); 
卷耳 2024-10-24 03:42:58

还有 imagecrop 函数,它允许您传入一个数组xy宽度高度 值。

There's also the imagecrop function, which allows you to pass in an array with x, y, width, and height values.

请恋爱 2024-10-24 03:42:57

你也可以这样做,我自己就这样做了,并且有效

(x1,y1)=>作物开始的位置

(x2,y2)=>作物结束的地方

$filename = $_GET['imageurl'];
$percent = 0.5;

list($width, $height) = getimagesize($filename);

$new_width  = $_GET['x2'] - $_GET['x1'];
$new_height = $_GET['y2'] - $_GET['y1'];

$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);

imagecopyresampled($image_p, $image, 0, 0 , $_GET['x1'] , $_GET['y1'] , $new_width, $new_height, $new_width, $new_height);

// Outputs the image
header('Content-Type: image/jpeg');
imagejpeg($image_p, null, 100);

you can do it too, I myself did it, and it works

(x1,y1)=> where crop starts

(x2,y2)=> where crop ends

$filename = $_GET['imageurl'];
$percent = 0.5;

list($width, $height) = getimagesize($filename);

$new_width  = $_GET['x2'] - $_GET['x1'];
$new_height = $_GET['y2'] - $_GET['y1'];

$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);

imagecopyresampled($image_p, $image, 0, 0 , $_GET['x1'] , $_GET['y1'] , $new_width, $new_height, $new_width, $new_height);

// Outputs the image
header('Content-Type: image/jpeg');
imagejpeg($image_p, null, 100);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文