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?

4
  • Do you actually have code in the //... section or is that what you're asking for help with? Commented Aug 7, 2012 at 15:54
  • @GigaWatt: it's in the link in his post. Commented Aug 7, 2012 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. Commented Aug 7, 2012 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). Commented Aug 7, 2012 at 16:44

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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.
@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?
Actually, for me it's not generating a jpeg file whatsoever, not even without any perspective applied to it.
@AndreiOniga Time to start adding debug statements and checking server error logs then, I'm afraid.
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.

1 Comment

I'm trying to actually save it as a standalone file, but even your approach didn't produce an output. Any other ideas?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.