« Run JavaScript synchr… | Home | ISO FileMaker Magazin… »

GraphicsMagick in FileMaker, part 20

🎄
20 of 24

Welcome to the 20th door of our advent calendar. In this advent calendar I would like to take you on a journey through the GraphicsMagick component in December. Every day I will introduce you to one or more functions from this component. In this component you will find functions with which you can analyze images, convert them, change them with filters, draw them and much much more. In the end, you too can take the magic of GraphicsMagick to your images. I wish you a lot of fun in the process.

Today I want to show you that with GraphicsMagick you can not only edit images, but we can also draw by ourselves. Today I will introduce you to the basic shapes.

To have a working environment on which we can work, we first use the GMImage.New function. This creates a working environment for us. We determine the size and the background color in the parameters. Here we draw a working environment of 500x500 pixels and a white background color.

Set Variable [ $GM ; Value: MBS( "GMImage.New"; "500x500"; "#FFFFFF" ) ] 

We have several basic shapes available in the plugin:

  • Line
  • Circle
  • Ellipse
  • Rectangle
  • Rounded rectangle
  • Arc

I will show you these shapes one after the other.

So that you can bring color directly into action in our example I would like to show you again briefly the functions for the color which you have already learned in the text annotation on pictures.

We have the line color that we can set. We specify the reference and the color, here the line color is red.

Set Variable [ $r ; Value: MBS("GMImage.SetStrokeColor"; $GM; "#FF0000") ] 

Then we have the fill color, which is the color with which our figures will be filled. Here a blue.

Set Variable [ $r ; Value: MBS("GMImage.SetFillColor"; $GM; "#0000FF") ] 

And we have the line thickness that we can set.

Set Variable [ $r ; Value: MBS("GMImage.SetLineWidth"; $GM; 10) ] 

Let's start with the line. For this we have the function GMImage.DrawLine. In the parameters we define the two points with their coordinates that should be connected. The origin of the coordinate system is in the upper left corner. This means that the higher the x value, the more we move to the right. For the y value, the larger it gets, the more we move down. You can also use the function GMImage.TransformOrigin to move the coordinate origin, but I don't want to do that now. In the script step you see here we connect the points (50/50) and (250/350) with each other.

Set Variable [ $r ; Value: MBS("GMImage.DrawLine"; $GM; 50; 50; 250; 350) ] 

Our result is a line that slopes from top left to bottom right.

Next we come to the circle. For this we use the GMImage.DrawCircle function. We first set the coordinates for the circle center. In our example we want to have it in the center of our workspace (250/250). Also, we set a point that is on the outer surface of the circle. I would like to have a circle of 150 pixels radius (300 px diameter). So the point 100/100 is on my circle outer circle. I also specify this in the parameters.

Set Variable [ $r ; Value: MBS( "GMImage.DrawCircle";$GM; 250; 250; 100; 100 ) ]

Our circle look like that

We define the ellipse with the GMImage.DrawEllipse function. Again, we first define the center of the ellipse. With the next value we determine the radius to the right and left to the ellipse edge (100) and with the following parameter (140) the radius of the ellipse to the top and bottom. With the last two parameters we can specify how much of the ellipse should be drawn. So we specify the segment of the circle. If we have a complete ellipse, we specify 0 as the start angle and 360 as the target angle.

Set Variable [ $r ; Value: MBS("GMImage.DrawEllipse"; $GM; 250; 250; 100; 140; 0; 360) ] 

If we give the ellipse a start angle of 0 and a target angle of 180 then our ellipse looks like this

Then we have our rectangle and our rounded rectangle. The functions for these shapes are similar. Let's start with the simpler function for the rectangle. For this we use the GMImage.DrawRectangle function. We define the rectangle by first specifying the upper left corner of the rectangle (50/50) and then the lower right corner (400/350).

Set Variable [ $r ; Value: MBS( "GMImage.DrawRectangle"; $GM; 50; 50; 400; 350 ) ] 

The difference between a rectangle and a rounded rectangle are the four rounded corners. We can create such a rectangle with the GMImage.DrawRoundRectangle function. The first parameters are the same as the rectangle (upper left corner and lower left corner) and the last two parameters specify the radius of the corners.

Set Variable [ $r ; Value: MBS("GMImage.DrawRoundRectangle"; $GM; 50; 50; 450; 450; 10; 10) ] 

The arc is a bit more complicated. An arc is a section of an ellipse and we use the GMImage.DrawArc function to draw it. Here we first define a box in which an ellipse would fit exactly. The last two parameters describe what we see of the ellipse. To illustrate this, I have defined 4 individual arcs, each of which has been assigned a different color.

Set Variable [ $GM ; Value: MBS( "GMImage.New"; "500x500"; "#FFFFFF" ) ] 

Set Variable [ $r ; Value: MBS("GMImage.SetStrokeColor"; $GM; "#FF0000") ] 
Set Variable [ $r ; Value: MBS("GMImage.SetFillColor"; $GM; "#0000FF") ] 
Set Variable [ $r ; Value: MBS("GMImage.SetLineWidth"; $GM; 10) ] 
Set Variable [ $r ; Value: MBS("GMImage.DrawArc"; $GM; 100; 100; 300; 250; 0; 90) ] 

Set Variable [ $r ; Value: MBS("GMImage.SetStrokeColor"; $GM; "#FFFF00") ] 
Set Variable [ $r ; Value: MBS("GMImage.DrawArc"; $GM; 100; 100; 300; 250; 90; 180) ] 

Set Variable [ $r ; Value: MBS("GMImage.SetStrokeColor"; $GM; "#00FF00") ] 
Set Variable [ $r ; Value: MBS("GMImage.DrawArc"; $GM; 100; 100; 300; 250; 180; 270) ] 

Set Variable [ $r ; Value: MBS("GMImage.SetStrokeColor"; $GM; "#0000FF") ] 
Set Variable [ $r ; Value: MBS("GMImage.DrawArc"; $GM; 100; 100; 300; 250; 270; 360) ] 

To clarify the box that defines the size of the ellipse, it is drawn in black.

To get our image displayed correctly we use the function GMImage.WriteToPNGContainer to save the graphic environment as PNG image.

Set Field [ GraphicsMagick Advent::Image ; MBS("GMImage.WriteToPNGContainer"; $GM; "abc.png") ] 

I hope you enjoyed it again this time and will join us tomorrow when we open the next door.

Previous day   Next day

20 12 22 - 08:01