How to generate image file using this PHP function?

丶灬走出姿态 提交于 2019-12-24 19:26:13

问题


I've been looking for a GD based solution for adding a perspective transformation to an image and eventually found something that seems promising: http://www.jqueryit.com/2010/03/set-perspective-of-image-using-php-gd.html

However, I'm not sure how to actually generate a new image using this function. My approach was this:

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

And unfortunately, it didn't produce an output. What is it that I'm doing wrong?


回答1:


Because you can't pass just a filename when the function requires an image resource. Try:

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

Read through the function you took from your link and look specifically where imagecopyresized() is called.




回答2:


If you're just trying to display it you should be able to change the header and echo the image.

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

The client browser will load this like any other jpg.



来源:https://stackoverflow.com/questions/11849552/how-to-generate-image-file-using-this-php-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!