You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-dev@xmlgraphics.apache.org by "Etienne Baert (SPSInfoquest nv)" <eb...@sps.be> on 2001/08/30 13:30:37 UTC

How to scale images ?

Dear fop-ers,

I have to include a generated picture into a pdf document.
I am thus using <fo:external-graphic .../>.

However, as my image is the result of a servlet process sent
as jpeg, I do not know in advance the image size. Once the
size of the image fits the available size, I do not have any 
problem ... but in other cases, my image is not embedded
and extra blank pages are added to the final document.

How can I have my picture taking place into the available
space ?

Many thanks,
Etienne Baert

---------------------------------------------------------------------
To unsubscribe, e-mail: fop-dev-unsubscribe@xml.apache.org
For additional commands, email: fop-dev-help@xml.apache.org


Re: How to scale images ?

Posted by Harald Hett <h....@gis-systemhaus.de>.
Hi, Etienne!

Recently I have faced this problem, as well.

To solve this, I wrote an extension class ImageInfo, 
that provides the following functions:
	public int height(String anImageUrl);
	public int width(String anImageUrl);

The implementation of these functions use the classes/interfaces
	org.apache.fop.image.analyser.ImageReader
	org.apache.fop.image.analyser.ImageReaderFactory
which are supplied with the fop.jar.

For instance height() is implemented as follows:

	public int height(String anImageUrl)
	{
		try {
			URL u = new URL(anImageUrl);
			InputStream in = u.openStream();
			ImageReader ir = ImageReaderFactory.Make("", in);
			
			return ir.getHeight();
		} 
		catch (Exception ex) {
			return -1;
		}
	}

With these extension-functions a stylesheet could 
determine, whether to scale the image or not:

	<xsl:stylesheet ... 
		xmlns:img="my.extensions.ImageInfo" 
		extension-element-prefixes="img">
	...	
	<fo:external-graphic>
		<xsl:attribute name="src">http://www.foo.bar/image.gif</xsl:attribute>
		<xsl:if test="img:height('http://www.foo.bar/image.gif') > 200">
			<xsl:attribute name="height">200pt</xsl:attribute>
		</xsl:if>
		<xsl:if test="img:width('http://www.foo.bar/image.gif') > 200">
			<xsl:attribute name="width">200pt</xsl:attribute>
		</xsl:if>
	</fo:external-graphic>
	...

I hope this helps!
Harald 
--
Harald Hett <h....@gis-systemhaus.de>
Gesellschaft für integrierte Systemplanung

"Etienne Baert (SPSInfoquest nv)" wrote:
> 
> Dear fop-ers,
> 
> I have to include a generated picture into a pdf document.
> I am thus using <fo:external-graphic .../>.
> 
> However, as my image is the result of a servlet process sent
> as jpeg, I do not know in advance the image size. Once the
> size of the image fits the available size, I do not have any
> problem ... but in other cases, my image is not embedded
> and extra blank pages are added to the final document.
> 
> How can I have my picture taking place into the available
> space ?
> 
> Many thanks,
> Etienne Baert
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: fop-dev-unsubscribe@xml.apache.org
> For additional commands, email: fop-dev-help@xml.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: fop-dev-unsubscribe@xml.apache.org
For additional commands, email: fop-dev-help@xml.apache.org