« Follow up on SMTP wit… | Home | MBS FileMaker Plugin,… »

GraphicsMagick in FileMaker, part 7

🎄
7 of 24

Welcome to the 7th 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 how to crop an image. For this we use the GMImage.Crop function. This function returns an image section that you have to define in the parameters before. As with the other functions, we first specify the reference and then a geometry. The geometry describes the cut-out from the image. First we can define how big such a section should be - again in pixels. First the width and then the height which we separate from each other with an x. But now we don't only want to define the size of the section, but also the position. For this reason we have to set the offsets for X and Y in addition to the size information. The offsets determine the distance from the upper left edge. If we set a value for X we move away from the side edge, if we set a value for Y we move away from the top border. These two values can be appended with a plus. We will make an example now. We would like to crop the image so that it is square and shows the center of the image. Here we see the code:

Set Variable [ $GM ; Value: MBS("GMImage.NewFromFile"; "/Users/sj/Desktop/abc.png") ] 
Set Variable [ $Width ; Value: MBS( "GMImage.GetWidth"; $GM ) ] 
Set Variable [ $Height ; Value: MBS( "GMImage.GetHeight"; $GM ) ] 
If [ $Width>$Height ] 
	# Landscape
	Set Variable [ $Size ; Value: $Height & "x" & $Height ] 
	Set Variable [ $OffsetX ; Value: Round(($Width - $Height) / 2; 0) ] 
	Set Variable [ $OffsetY ; Value: 0 ] 
Else
	#  Portrait
	Set Variable [ $Size ; Value: $Width & "x" & $Width ] 
	Set Variable [ $OffsetX ; Value: 0 ] 
	Set Variable [ $OffsetY ; Value: Round(($Height - $Width ) / 2; 0) ] 
End If
Set Variable [ $Geometry ; Value: $Size & "+" &$OffsetX& "+" &$OffsetY ] 
Set Variable [ $r ; Value: MBS( "GMImage.Crop"; $GM; $Geometry ) ] 
Set Field [ GraphicsMagick Advent::Image ; MBS( "GMImage.WriteToContainer"; $GM ; "abc.png" ) ] 
Set Variable [ $r ; Value: MBS( "GMImage.Release"; $GM ) ] 

First, we again get an image file as a reference and queries height and width. We must of course be able to distinguish whether the image is in portrait or landscape orientation. If it is a landscape format image, so the width is greater than the height, then we want to specify the size of the image as heightxheight. Please note, that we use Round() here to make sure we have a whole number.

As the picture is wider, we now have to set the crop to the center, so we see in the picture that we have to move a certain number of pixels away from the edge. This is then the X Offset. The value for the X Offset results from the difference between width and height. Since we want to have the same distance on both sides, we divide this distance by two and get our value. Since we don't need a Y-Offset in this case, we set the value to 0. For a portrait image we have exactly the opposite case, we set the size with widthxwidth.The X offset is 0 and the Y offset is the difference between height and width divided by 2

The geometry is then composed as described:
Side dimension x Side dimension+OffsetX+OffsetY

Then the crop function is applied, the image is placed in the container, and then the reference is released again.

Our picture is now square. I hope you enjoyed this door and see you tomorrow at the next door

Previous day   Next day

Claris FileMaker Plugin
07 12 22 - 08:42