繁体   English   中英

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

[英]PHP - cropping image with imagecopyresampled()?

我想使用imagecreatetruecolor裁剪图像,它总是裁剪它留下黑色空间,或者缩放太大。 我希望图像精确到191px宽和90px高,所以我还需要调整图像大小以及裁剪,因为必须保持比例。 那么,有一些项目的样本:

在此输入图像描述

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

$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']

但我得到的是:

在此输入图像描述

有人看到,我做错了什么?

谢谢,迈克。

你也可以这样做,我自己也做了,而且它有效

(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);

尝试

<?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);

还有imagecrop函数,它允许您传入具有xywidthheight值的数组。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM