Image Controls - Distorting image

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
elinews
Forum Commoner
Posts: 38
Joined: Tue Aug 14, 2007 7:18 pm

Image Controls - Distorting image

Post by elinews »

Could anyone tell me how to distort an image using image controls to give it a 3d perspective like the pic below?

Thanks in advance!

Image
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Not fun. You will have to do the 3D math yourself.
elinews
Forum Commoner
Posts: 38
Joined: Tue Aug 14, 2007 7:18 pm

Post by elinews »

feyd wrote:Not fun. You will have to do the 3D math yourself.
I'm fine with that. I just don't even know where to begin. I've never used image controls before.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

I'm fine with that.
I wouldn't be. If you can write that sort of stuff with any ease you wouldn't be calling them "controls". Check out PEAR I believe there is a package especially designed for this kind of stuff, it has 3D in the title, shouldn't be hard to find.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

http://www.geocities.com/SiliconValley/2151/math3d.html talks about rotations. In the quick glance I took of it, it did not specify if the math was for the 2D projection of the rotation however. Although the math to convert them isn't all that complicated. You will probably need subpixel calculation however.
mrkite
Forum Contributor
Posts: 104
Joined: Tue Sep 11, 2007 4:19 am

Post by mrkite »

Using a lot of math and brain hurting, you can use the -fx flags in imagemagick's convert to do it.

http://www.imagemagick.org/Usage/script ... _transform

There's a perl script to output the proper fx values.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

It can be faked using imagecopyresampled, but the result isn't really that good...

Code: Select all

<?php

    //Fake perspective transform

    $source = imagecreatefromjpeg("image.jpg");
    $width = imagesx($source);
    $height = imagesy($source);

    $destination = imagecreatetruecolor($width,$height);
    $white = imagecolorallocate($destination,255,255,255);
    imagefill($destination,0,0,$white);

    for ($x = 0; $x<$width; $x++) {

        imagecopyresampled($destination,$source,$x,($height/4)-(($height/$width)*$x/4),$x,0,1,($height/2)+(($height/$width)*$x/2),1,$height);

    }

    header("Content-type: image/jpeg");
    imagejpeg($destination);

?>
elinews
Forum Commoner
Posts: 38
Joined: Tue Aug 14, 2007 7:18 pm

Post by elinews »

onion2k wrote:It can be faked using imagecopyresampled, but the result isn't really that good...

Code: Select all

<?php

    //Fake perspective transform

    $source = imagecreatefromjpeg("image.jpg");
    $width = imagesx($source);
    $height = imagesy($source);

    $destination = imagecreatetruecolor($width,$height);
    $white = imagecolorallocate($destination,255,255,255);
    imagefill($destination,0,0,$white);

    for ($x = 0; $x<$width; $x++) {

        imagecopyresampled($destination,$source,$x,($height/4)-(($height/$width)*$x/4),$x,0,1,($height/2)+(($height/$width)*$x/2),1,$height);

    }

    header("Content-type: image/jpeg");
    imagejpeg($destination);

?>
thanks onion you're a life saver.

Do you know where/how I can download and install php imagecontrols on my apache server?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

elinews wrote:Do you know where/how I can download and install php imagecontrols on my apache server?
What image controls are you referring to? I've never heard of image controls before.

The GD library can do basically anything that you are after with images, though you may run across times where you'll need to know more in-depth mathematics to accomplish what you are after. You can usually find examples, it's just a matter of knowing what to search for (which is a pain). You usually won't find examples in PHP, but C++ and Delphi always seem to have examples for image creation and manipulation.
Post Reply