How to use BILINEARWARP with PHP

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
handellphp

How to use BILINEARWARP with PHP

Post by handellphp »

Ive search the web for an hour and cannot seem to find any PHP / Imagemagick usage of on BILINEARWARP
from Freds Pages - http://www.fmwconcepts.com/imagemagick/ ... /index.php

I am trying to get sort of a reverse Star Wars text effect
Here is what I was thinking:

Code: Select all

<?php
$convert =  "~/apps/im/bin/convert";

$input="test_image.jpg";
$output = "image_result.png";

exec("$convert $input -matte -virtual-pixel transparent -distort perspective -f input \"0,0 255,20 255,255 233,255\" $output")

echo "<br><img src=\"". $output ."\"><br>";
echo "<br>im here and I am done<br>";
?>
Obviously Im missing something here.
Can anyone help?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: How to use BILINEARWARP with PHP

Post by Bonzo »

This is not realy a Developers question and should go into the user section.

To use Freds script:
1/ Upload the script to your server - I add an extension to the script e.g. bilinearwarp.sh
2/ Change the permissions to 755 on the script
3/ Write you code like this:

Code: Select all

<?php
    // Run the script
    exec("/FULL PATH TO SCRIPT/bilinearwarp.sh -virtual-pixel black -distort perspective \"0,0 52,0 255,0 228,46 255,255 255,229 0,255 0,246\" input.jpg output.png 2>&1", $array); 
    //Display any errors
    echo "<br>".print_r($array)."<br>"; 
    echo "</pre>";
    ?> 
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to use BILINEARWARP with PHP

Post by fmw42 »

exec("/FULL PATH TO SCRIPT/bilinearwarp.sh -virtual-pixel black -distort perspective \"0,0 52,0 255,0 228,46 255,255 255,229 0,255 0,246\" input.jpg output.png 2>&1", $array);
Not quite correct:

1) leave off the 2>&1 as that is only needed when you are trying to get help or other textual information and not output images.

2) this is not the syntax for my script. it seems to be mixing my bilinearwarp with a normal perspective command in ONE command line. You cannot do that. You can run my bilinear or run a simple -distort perspective in one command. But not both.

If you want to run a simple -distort perspective, then the syntax is (command line)

convert input.jpg -virtual-pixel black -distort perspective " 4 pairs of x,y control points in the correct ordering" output.png

If you want to run my bilinearwarp script (which is very slow and you are better running -distort perspective unless you actually want the curved sides), then although I am not sure of the exact PHP code, try

exec("/FULL PATH TO SCRIPT/bilinearwarp.sh -v transparent -f box \"4 pairs of x,y control points\" input.jpg output.png", $array);

the -distort perspective is not part of my script and -v black is the default. You may have to decide to specify -f (format for output) as box or input. The default is a bounding box just large enough to hold the result.
I am trying to get sort of a reverse Star Wars text effect
I don't know what this is to be able to help. You will need to provide an example or explain further.

The following perspective should work, but I don't use PHP to be sure,

exec("full path to/convert $input -channel rgba -matte -virtual-pixel transparent -distort perspective \"4 pairs of x,y control points\" $output", $array);

But you have not supplied enough control points (only 2 and you need 4 pairs of x,y.
You need srcx1,srcy1 destx1,desty1 ... srcx4,srcy4 destx4,desty4 with the more recent versions of IM. The scr correspond to the corners of the image and dest to where you want the corners to be moved.
(A while back the order of the control points was changed)

see Bonzo's information above
handellphp

Re: How to use BILINEARWARP with PHP

Post by handellphp »

Star Wars text example:
Image

Thanks for the input.
I understand the 4 sets of coordinates on the distort perspective however I got some strange results.
And I think its because I do not know the input image size w,h so I just thought it would be easier to use BILINEAR because it appeared to me (and i may be wrong) that the srcx,srcy... coordinates of the 4 corners where a given.

Is there any way to use percentages in place of the pixels for -distort perpective ?

Code: Select all

 exec("$convert $output -matte -virtual-pixel transparent -distort Perspective \
 \"0,0,0,0 0,100%,10%,100% 100%,0,100%,0 100%,100%,90%,100%\"   $output");
 
the outcome i am trying to achieve is:
Image
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to use BILINEARWARP with PHP

Post by fmw42 »

you are right, my bilinear script starts with the 4 input corners and so you only need to supply the 4 desired coords.

However the easier thing is to use my script 3Drotate or rotate3D. There you just tell it how much you want the image tilted up or down (rotated in degrees), see the examples
handellphp

Re: How to use BILINEARWARP with PHP

Post by handellphp »

I guess I should have mentioned, not all the images are going to be in uniform.
I looked at the examples for 3Drotate & rotate3D but did not see a non-unform effect.

Here is another example of the desired effect:
Image

Basically the top edge of the image will be stationary,
the bottom left src position will go to the right $x
and the bottom right src position will go to the left $y.

Ive been tinkering with the -distort perspective but no luck. Maybe Im not understanding the coordinates.
here is how I understand them:
Image
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to use BILINEARWARP with PHP

Post by fmw42 »

works for me but I don't have your input

convert sample2_DNT.jpg -channel rgba -matte -virtual-pixel transparent -distort perspective "0,0 0,0 0,100 30,100 100,0 100,0 100,100 90,100" sample2_DNT_persp.png

Image

If this is too blurry due to the use of EWA anti-aliasing, then add -filter point or some other filter type. see http://www.imagemagick.org/Usage/resize/#filter

Image
Here is another image the same size:
convert cyclops.png -channel rgba -matte -virtual-pixel transparent -distort perspective "0,0 0,0 0,100 30,100 100,0 100,0 100,100 90,100" cyclops_persp.png
Image


I think you still miss several things in your PHP code that you overlooked in my earlier message:

1) you have to give full path to convert (and no $) unless you have set up your environment to know where it is:
exec("/fullpathto/convert ...

2) you need to specify -channel rgba to get transparency in addition to -matte

3) you have the $output for your $input

4) the order of control points is fine for current versions of IM. You may be on an older version. There (pre 6.3.6-0) the order is: srcx1,srcy1 ... srcx4,scry4 destx1,desty1 ... destx4,dsty4

What version of IM are you using?

<?php
system("/usr/local/bin/convert -version");
?>

Perhaps you should look at http://www.rubblewebs.co.uk/imagemagick/ for more detailed examples and information about using IM with PHP exec.
handellphp

Re: How to use BILINEARWARP with PHP

Post by handellphp »

Ok yeah Well! that makes me a goofup.
It was the '$output' being in the wrong part.
Was a copy paste from something else I was working on.

That fixed the problem.

Code: Select all

<?php
$convert =  "~/apps/im/bin/convert";

$input="test_in.jpg";
$output = "test_out.png";

exec("$convert $input -matte -virtual-pixel transparent -distort Perspective \
 \"0,0,0,0 0,100,20,100 100,0,100,0 100,100,80,100\" $output");
 
echo "<br><img src=\"". $output ."\"><br>";
?>
Also, i do run the latest version of IM outside root directory because 'bluehost' will not update.

Thanks!
I am happy again.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to use BILINEARWARP with PHP

Post by fmw42 »

no problem. glad you got it cleared up. sorry for the misunderstanding of your configuration
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to use BILINEARWARP with PHP

Post by anthony »

handellphp wrote:Is there any way to use percentages in place of the pixels for -distort perpective ?
Not at this time. the problem is the number mean different things to different
distortion methods. Some times it is a width, somethings a height, sometimes an angle or scaling factor.

HOWEVER you can use % escapes in the argument string...
'0,0,0,0 0,100%,10%,100% 100%,0,100%,0 100%,100%,90%,100%'
translates to
'0,0 0,0 0,%h,%[fx:w*0.1],%h %w,0,%w,0 w%,h%,%[fx:w*0.9],%h'
NOTE you can also have white space in the argument such as returns!
or read the values (no escapes in that case) from a file.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to use BILINEARWARP with PHP

Post by fmw42 »

'0,0,0,0 0,100%,10%,100% 100%,0,100%,0 100%,100%,90%,100%'

translates to

'0,0 0,0 0,%h,%[fx:w*0.1],%h %w,0,%w,0 w%,h%,%[fx:w*0.9],%h'
but then you could also do

'0,0 0,0 0,%h,%[fx:w*10/100],%h %w,0,%w,0 w%,h%,%[fx:w*90/100],%h'

so that 10 and 90 represent percents

or you could compute them ahead of time as variables

w1=`convert infile -format "%[fx:w*10/100]" info:`
w2=`convert infile -format "%[fx:w*90/100]" info:`

and use them as variable in your coord string (with double quotes)

"0,0 0,0 0,%h,$w1,%h %w,0,%w,0 w%,h%,$w2,%h"

Anthony should correct me if this is wrong as I have never used % or fx calcs in the coord string, but I do use variables all the time in my bash scripts that use -distort perspective
handellphp

Re: How to use BILINEARWARP with PHP

Post by handellphp »

Meanwhile I came up with this:

Code: Select all

<?php
$convert = "~/apps/im/bin/convert";

$input="test_in.jpg";
$output = "test_out.png";

list($x, $y, $type2, $attr2) = getimagesize($input);

///percentages as coordinates
$ps= array(0,0,30,100,100,0,90,100);

$a = ($x*$ps[0])/100;
$b = ($y*$ps[1])/100;
$c = ($x*$ps[2])/100;
$d = ($y*$ps[3])/100;
$e = ($x*$ps[4])/100;
$f = ($y*$ps[5])/100;
$g = ($x*$ps[6])/100;
$h = ($y*$ps[7])/100;

exec("$convert $input -matte -virtual-pixel transparent -distort Perspective \
\"0,0,$a,$b  0,$y,$c,$d $x,0,$e,$f $x,$y,$g,$h\" $output");
 
echo "<br><img src=\"". $output ."\"><br>";
?>
This allows the assumption that all 4 corners are known similar to the BILINEARWARP from Freds Pages.
Post Reply