php imagecopyresampled问题
我对imagecopyresampled有一些问题,主要是图像缩放不正确,图像位置错误,边缘周围有黑色边框 我设置了以下VARphp imagecopyresampled问题,php,gd,Php,Gd,我对imagecopyresampled有一些问题,主要是图像缩放不正确,图像位置错误,边缘周围有黑色边框 我设置了以下VAR $tW = $width; // Original width of image $tH = $height; // Orignal height of image $w = postvar; // New width $h = postvar; // New height $x = postvar; // New X pos
$tW = $width; // Original width of image
$tH = $height; // Orignal height of image
$w = postvar; // New width
$h = postvar; // New height
$x = postvar; // New X pos
$y = postvar; // New Y pos
然后运行以下命令
$tn = imagecreatetruecolor($w, $h);
$image = imagecreatefromjpeg('filepathhere.jpeg');
imagecopyresampled($tn, $image, 0, 0, $x, $y, $w, $h, $tW, $tH);
如果有人有任何线索,那将是巨大的帮助!谢谢这里有几个问题。首先,不应使用指定的新高度和宽度创建新图像,而应根据原始图像的比率计算新高度和宽度,否则缩放后的图像将失真。例如,下面的代码将创建一个大小正确的图像,该图像将适合给定的
$w x$h
矩形:
$tW = $width; //original width
$tH = $height; //original height
$w = postvar;
$h = postvar;
if($w == 0 || $h == 0) {
//error...
exit;
}
if($tW / $tH > $w / $h) {
// specified height is too big for the specified width
$h = $w * $tH / $tW;
}
elseif($tW / $tH < $w / $h) {
// specified width is too big for the specified height
$w = $h * $tW / $tH;
}
$tn = imagecreatetruecolor($w, $h); //this will create it with black background
imagefill($tn, 0, 0, imagecolorallocate($tn, 255, 255, 255)); //fill it with white;
//now you can copy the original image:
$image = imagecreatefromjpeg('filepathhere.jpeg');
//next line will just create a scaled-down image
imagecopyresampled($tn, $image, 0, 0, 0, 0, $w, $h, $tW, $tH);
如果您提供更多关于您试图实现的目标的详细信息,我可能会进一步提供帮助。您到底想做什么,预期的结果是什么?抱歉,根据提供的信息无法判断。原始尺寸是多少?$tW和$tH的值是多少?您是否可能对原始图像之外的区域重新采样?值随“裁剪”工具提交的内容而变化。因此,每次有人运行该工具时,它们都会有所不同。$width和$height值来自一个列表($width,$height)=getimagesize('filepath.jpg');
$tW = $width - $x; //original width
$tH = $height - $y; //original height
$w = postvar;
$h = postvar;
if($w == 0 || $h == 0) {
//error...
exit;
}
if($tW / $tH > $w / $h) {
// specified height is too big for the specified width
$h = $w * $tH / $tW;
}
elseif($tW / $tH < $w / h) {
// specified width is too big for the specified height
$w = $h * $tW / $tH;
}
$tn = imagecreatetruecolor($w, $h); //this will create it with black background
imagefill($tn, 0, 0, imagecolorallocate($tn, 255, 255, 255)); //fill it with white;
//now you can copy the original image:
$image = imagecreatefromjpeg('filepathhere.jpeg');
//next line will create a scaled-down portion of the original image from coordinates ($x, $y) to the lower-right corner
imagecopyresampled($tn, $image, 0, 0, $x, $y, $w, $h, $tW, $tH);