Jump to content
View in the app

A better way to browse. Learn more.

FMForums.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Hi, I use the CropImage function to insert an image from a file into a container field,

Base64Decode ( Base64Encode ( CropImage( $importpath ; myTable::img_X ; myTable::img_Y ; myTable::img_pic_width ; myTable::img_pic_height ) ) ; myTable::img_destination_fieldname )

It works perfectly but, if the XY coordinates are negatives, the background that fills the empty space is set automatically to black.

Ex:

Base64Decode ( Base64Encode ( CropImage( $importpath ; -200 ; -75 ; 500 ; 250 ) ) ; myTable::img_destination_fieldname )

https://punto-rosso.d.pr/GHn8Vv

I use negative coordinates to position the picture inside the container.

How I can avoid the black background? Or at least set it to white/transparent.

Thanks

Small update: the problem exist even with positive values. Ex.

Base64Decode ( Base64Encode ( CropImage( $importpath ; 25 ; 100; 500 ; 250 ) ) ;myTable::img_destination_fieldname )

https://punto-rosso.d.pr/u505b6

It seems that the Crop function simply add automatically a black background to fill any empty space.

For JPG

Add this at the top

import java.awt.Color

 Then add

//add these
d.setBackground(Color.WHITE)
//or choose any other Color you prefer
//see https://docs.oracle.com/javase/7/docs/api/java/awt/Color.html
d.clearRect(0, 0, widthInt, heightInt)
//before this
d.drawImage(img, xInt*-1, yInt*-1, null)

This line/param

BufferedImage.TYPE_INT_RGB

says it is an RGB image

If your source has transparency then you want to be using BufferedImage.TYPE_INT_ARGB, but the code to get/create transparency where you have a negtive x or y is a little more difficult...

Edited by john renfrew

  • Author

Hi John,

thats perfect!

The ARGB argument did the trick. A genius strike! ;)

Thanks a lot for your help.

Best

  • Author

Hi John,

I am trying to obtain the same result with the function "Rotate Image" but somehow your trick doesn't work here.

Any help is really appreciated.

Thanks

Are you trying to rotate and resize at the same time?? Surely when you rotate an image there is no 'spare' to be cast as transparent? 

If you rotate a 400*300px image by 90degrees then you get a 300*400px image, or are you trying to rotate by other angles??

  • Author

Jus rotate.

The problem is that all transparent areas of the image get lost when using the standard parameters and, when using .png, they turn to black.

I have made a small screencast example to show

https://punto-rosso.d.pr/C4H6Ul

Thanks

Here is a new function for rotation of PNG files, which should fix your issue, if you pass it a file with no transparency or alphait returns an error to you

If you do SMGetVariable("cm") after running it you will see the color model Java thinks the file has

 

// RotatePNG ( imgLocation ; degrees )
// v1.0
// 19_03_12 JR


import javax.imageio.ImageIO
import java.awt.image.BufferedImage
import java.awt.Graphics2D
import java.awt.geom.AffineTransform
import java.awt.RenderingHints


isTransparent = {image, x, y ->
	pixel = image.getRGB(x,y)
	return (pixel>>24) == 0x00
}

containsTransparency  = { image ->
	for (i in 0..<image.getHeight()) {
		for (j in 0..<image.getWidth()) {
			if (isTransparent(image, j, i)){
				return true
			} //end if
		} //end for
	} //end for
} //end containsTransparency

containsAlphaChannel ={ image ->
	return image.getColorModel().hasAlpha()
}//end containsAlphaChannel

degrees = Double.parseDouble(degrees)
radians = Math.toRadians(degrees)
isRightAngle = degrees % 90 == 0

BufferedImage img
if (imgLocation.indexOf("://") != -1) {
	img = ImageIO.read(new URL(imgLocation))
} else {
	img = ImageIO.read(new File(imgLocation))
}

cm = img.getColorModel()
//check for transparency
try{
isAlpha = containsAlphaChannel(img)
if ( !isAlpha) {
	isTransparent = containsTransparency(img)
	if ( !isTransparent){
		return 'ERROR - no transparency'
	}//end if
	return 'ERROR - no transparency'
} //end if
} catch (e) {
	return 'ERROR - unspecified'
}

int newWidth = Math.abs(Math.cos(radians) * img.getWidth()) + Math.abs(Math.sin(radians) * img.getHeight())
int newHeight = Math.abs(Math.cos(radians) * img.getHeight()) + Math.abs(Math.sin(radians) * img.getWidth())
at = AffineTransform.getRotateInstance(radians, img.getWidth()/2, img.getHeight()/2)

type = BufferedImage.TYPE_INT_ARGB

dest = new BufferedImage(newWidth, newHeight, type)

g2d = dest.createGraphics()
if (!isRightAngle) {
    // enable antialiasing for odd angles
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
}

g2d.setTransform(AffineTransform.getTranslateInstance((newWidth - img.getWidth()) / 2, (newHeight - img.getHeight()) / 2))
g2d.drawImage(img, at, null)

return dest

So only two parameters as you don't care about background colour

  • Author

Hi John,

you're the best!

A big, big thanks, I really appreciate your help.

Have a nice day.

 

Create an account or sign in to comment

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.