Page 1 of 1

negative x y co-ords for image placement

Posted: 2009-05-07T23:12:43-07:00
by tobycarr
Hi,
How do I specify placement of an image on a canvas at negative x y co-ords e.g placing a 100x100 image at -34, -45 so that it is only partially visible on the canvas.
I would like to be able to do this using the simple -page command but +-34+-45 does not seem to be acceptable..
OR can you specify the co-ords to refer to another part of the image being place rather than 0,0 so that I could use positive values for the position on canvas and specify it aligning with a point say (in the -34.-55 example used) at 68,55 on the image being placed?
Many thanks for any help.

Re: negative x y co-ords for image placement

Posted: 2009-05-07T23:18:15-07:00
by tobycarr
Excuse typo in above..co-ords for alignment shift example should read

"(in the -34.-55 example used) at 66,45 on the image being placed?"

Re: negative x y co-ords for image placement

Posted: 2009-05-07T23:55:07-07:00
by Bonzo
I thought negative values worked but I can not try it at the moment.

How about starting with a larger canvas and then cropping it ?

Re: negative x y co-ords for image placement

Posted: 2009-05-08T00:03:19-07:00
by anthony
\( image.png -repage -34-55 \)
will do the job of giving the image a negative position.

however if you want it to 'clip' to the background image you should use -flatten, whcih will not expand the first image.

or use -geometry -34-55 -composite method instead.


try giving us the complete simplified command of what you are attempting.

Re: negative x y co-ords for image placement

Posted: 2009-05-08T01:04:43-07:00
by tobycarr
Thanks for that - much appreciated.
The first problem was that I was trying to get it to accept +-34+-55. but as per your example it will work with -34-55..

The problem occurred because I am using variables for the co-ords - so now I am trying to solve it by adding in the plus sign only when positive but it is reverting again to 0,0 positioning.. please see code example (from within php exec)..
When I put values in manually say -300-300 it works fine but the variable using line is putting the image at 0,0 -->

Code: Select all

		 $x = "-200";
			 if ($x < 0)
			 {
			  $op_x = "";
			 }
			 else
			 {
			  $op_x = "+";
			 }
			 $y = "-200";
                         //comment - just use op_x for now to test

			 
			   exec ("convert -size 1000x1000 xc:skyblue \\
          -page $op_x . $x . $op_y. $y image.png  \\
          -page -300-300 image.png  \\
          -flatten  painted_canvas.png");

Re: negative x y co-ords for image placement

Posted: 2009-05-08T01:06:47-07:00
by tobycarr
..sorry..
code does only use $op_x for testing --

Code: Select all

		 $x = "-200";
			 if ($x < 0)
			 {
			  $op_x = "";
			 }
			 else
			 {
			  $op_x = "+";
			 }
			 $y = "-200";
			 
			   exec ("convert -size 1000x1000 xc:skyblue \\
          -page $op_x . $x . $op_x. $y image.png  \\
          -page -300-300 image.png  \\
          -flatten  painted_canvas.png");

Re: negative x y co-ords for image placement

Posted: 2009-05-08T01:12:53-07:00
by tobycarr
..sorry again,,
that code was missing quotes to parse php code within the exec..so..
unless some wise programmer can tell me how to assign the operator more simply in php
..seems to work now..
so solved thanks --
ta

Re: negative x y co-ords for image placement

Posted: 2009-05-08T02:45:34-07:00
by Bonzo
I can not test this at the moment but something like this should work:

Code: Select all

$op_x = ($x < 0) ? $x: '"+".$x';
Search google for "php simple if" or "php ternary"

Re: negative x y co-ords for image placement

Posted: 2009-05-08T04:27:48-07:00
by tobycarr
thanks very much for the tips

Re: negative x y co-ords for image placement

Posted: 2009-05-08T08:43:08-07:00
by Bonzo
It should be:

Code: Select all

$op_x = ($x < 0) ? $x: "+".$x;

Re: negative x y co-ords for image placement

Posted: 2009-05-08T19:18:49-07:00
by anthony
The simplest way to handle this is either.
  • Look at the number string and if you see no '-' then add a '+' to the number string.
    That is similar to what the others have specified.
  • An even easier way is to use the printf() number format with a '+' sign in it.
    For example printf("%+d%+d", x, y)
    This tells the function to always include a sign with that number. It lets you do BOTH numbers and/or format the WHOLE geometry-style argument completely all in one go.
Both methods work well to ensure that the correct sign is always present on the number. And I have used them both, depending on exactly what I am doing.

Re: negative x y co-ords for image placement

Posted: 2009-05-09T01:37:52-07:00
by tobycarr
That's great,
Thanks Anthony