0

我一直在寻找一种基于 GD 的解决方案来为图像添加透视变换,最终发现了一些看起来很有希望的东西:http ://www.jqueryit.com/2010/03/set-perspective-of-image-using- php-gd.html

但是,我不确定如何使用此功能实际生成新图像。我的方法是这样的:

function perspective($i,$gradient=0.85,$rightdown=true,$background=0xFFFFFF) {
    $mult=5;
    $w=imagesx($i);
    $h=imagesy($i);
    $image=imagecreatetruecolor($w*$mult,$h*$mult);
    imagecopyresized($image,$i,0,0,0,0,$w*$mult,$h*$mult,$w,$h);
    imagedestroy($i);
    $w*=$mult;
    $h*=$mult;
    $im=imagecreatetruecolor($w,$h);
    $background=imagecolorallocate($im,($background>>16)&0xFF,($background>>8)&0xFF,$background&0xFF);
    imagefill($im,0,0,$background);
    imageantialias($im,true);
    $nh=$h-($h*$gradient);
    for ($x=0; $x<$w; $x++) {
        $ni=(($rightdown) ? $x : $w-$x);
        $p=intval($h-(($ni/$w)*$nh));
        if (($p%2)<>0)
            $p-=1;
        $nx=intval(($p-$h)/2);
        imagecopyresampled($im,$image,$x,0,$x,$nx,1,$p,1,$h-1);
        imageline($im,$x,0,$x,-$nx-1,$background);
        imageline($im,$x,$h-1,$x,$h+$nx,$background);
    }
    imagedestroy($image);
    imagefilter($im,IMG_FILTER_SMOOTH,10);
    $i=imagecreatetruecolor($w/$mult,$h/$mult);
    imageantialias($i,true);
    imagecopyresampled($i,$im,0,0,0,0,$w,$h,$w*$mult,$h*$mult);
    imagedestroy($im);
    return $i;
}

$image = perspective("my_image.jpg");

imagejpeg($image , "my_image_converted.jpg");

不幸的是,它没有产生输出。我做错了什么?

4

2 回答 2

1

因为当函数需要图像资源时,您不能只传递文件名。尝试:

$image = perspective(imagecreatefromjpeg("my_image.jpg"));

通读您从链接中获取的函数,并具体查看调用imagecopyresized()的位置。

于 2012-08-07T15:54:29.227 回答
0

如果您只是想显示它,您应该能够更改标题并回显图像。

header("Content-Type: image/jpg");
echo imagejpeg($image , "my_image_converted.jpg");

客户端浏览器将像任何其他 jpg 一样加载它。

于 2012-08-07T15:54:07.197 回答