0

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?

WWW
  • 9,734
  • 1
  • 29
  • 33
Andrei Oniga
  • 8,219
  • 15
  • 52
  • 89
  • Do you actually have code in the `//...` section or is that what you're asking for help with? – Mr. Llama Aug 07 '12 at 15:54
  • @GigaWatt: it's in the link in his post. – WWW Aug 07 '12 at 15:55
  • @GigaWatt Exactly, the code is on the page I gave the link to. I Just didn't want to fill up this post with ballast, since the function is already there. – Andrei Oniga Aug 07 '12 at 16:00
  • I added the function to your post because the problem lies within it (or how you're trying to use it, at least). – WWW Aug 07 '12 at 16:44

2 Answers2

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.

WWW
  • 9,734
  • 1
  • 29
  • 33
  • I'm afraid it didn't work, though. I changed the first line to "$image = perspective(imagecreatefromjpeg("home-bg-3.jpg"));" and the second one remained unchanged ("imagejpeg($image , "my_image_converted.jpg");"), but with no effect. – Andrei Oniga Aug 07 '12 at 16:15
  • @AndreiOniga So it wrote out `my_image_converted.jpg` but that image didn't have the perspective effect applied to it, is that what you're saying? – WWW Aug 07 '12 at 16:40
  • Actually, for me it's not generating a jpeg file whatsoever, not even without any perspective applied to it. – Andrei Oniga Aug 07 '12 at 16:48
  • @AndreiOniga Time to start adding debug statements and checking server error logs then, I'm afraid. – WWW Aug 07 '12 at 17:31
0

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.

pdizz
  • 4,100
  • 4
  • 28
  • 42
  • I'm trying to actually save it as a standalone file, but even your approach didn't produce an output. Any other ideas? – Andrei Oniga Aug 07 '12 at 16:16