negative x y co-ords for image placement

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
tobycarr

negative x y co-ords for image placement

Post 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.
tobycarr

Re: negative x y co-ords for image placement

Post 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?"
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: negative x y co-ords for image placement

Post 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 ?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: negative x y co-ords for image placement

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
tobycarr

Re: negative x y co-ords for image placement

Post 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");
tobycarr

Re: negative x y co-ords for image placement

Post 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");
tobycarr

Re: negative x y co-ords for image placement

Post 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
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: negative x y co-ords for image placement

Post 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"
tobycarr

Re: negative x y co-ords for image placement

Post by tobycarr »

thanks very much for the tips
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: negative x y co-ords for image placement

Post by Bonzo »

It should be:

Code: Select all

$op_x = ($x < 0) ? $x: "+".$x;
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: negative x y co-ords for image placement

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
tobycarr

Re: negative x y co-ords for image placement

Post by tobycarr »

That's great,
Thanks Anthony
Post Reply