Discussion (7)3
Log in or sign up to continue

You could try this way (I assume, your image is in *.jpg, *.png, *.gif, *.ico or *.bmp  format and you use a Windows OS):

- save the stream to a temp file
- do $zf(-1,"pathToIrfanView.exe pathToInpFile  /resize=(100,100)  /aspectratio  /convert=pathToOutFile")
- read the output into a stream and proceed as needed

Don't forget, if one of the above path contains a space char, you have to put in quotes the whole path.

/resize=(100,100)  /aspectration  means, the picture will be resized to a max width/height of 100 by maintaning the aspect ratio. Example: input=2000x3000 out =67x100,  input=3000x2000  out=100x67

For Unix exists similar tools to use with $zf(-1,...), for example "identify" to get the dimensions of the picture  and "convert" or "imagemagick" for resizing.

I agree with general approach offered by @Julius Kavay, just wanted to add some notes:

1. Use $zf(-100) as a more secure alternative to $zf(-1) if it's available.

2. I'd go straight for imagemagick as it's crossplatform.

3. If speed is an issue you can consider using high-level MagickWand API (here's resize example) or low-level MagickCore API (here's resize functions) via Callout functionality. CoreAPI would be faster as there's in-memory image initializers so you can skip input/output file creation/consumption and work with inmemory streams.

Something like this:

#define USE_CALLIN_CHAR

#define ZF_DLL  /* Required only for dynamically linked libraries. */
#include <cdzf.h>
#include <windows.h>
#include <wand/magick_wand.h>

#ifdef __linux__
	#include <dlfcn.h>
#endif

void resize(char *file, char *fileOut)
{
	MagickWand *m_wand = NULL;
	
	int width,height;
	
	MagickWandGenesis();
	
	m_wand = NewMagickWand();
	// Read the image - all you need to do is change "logo:" to some other
	// filename to have this resize and, if necessary, convert a different file
	MagickReadImage(m_wand, file);
	
	// Get the image's width and height
	width = MagickGetImageWidth(m_wand);
	height = MagickGetImageHeight(m_wand);
	
	// Cut them in half but make sure they don't underflow
	if((width /= 2) < 1)width = 1;
	if((height /= 2) < 1)height = 1;
	
	// Resize the image using the Lanczos filter
	// The blur factor is a "double", where > 1 is blurry, < 1 is sharp
	// I haven't figured out how you would change the blur parameter of MagickResizeImage
	// on the command line so I have set it to its default of one.
	MagickResizeImage(m_wand,width,height,LanczosFilter,1);
	
	// Set the compression quality to 95 (high quality = low compression)
	MagickSetImageCompressionQuality(m_wand,95);
	
	/* Write the new image */
	MagickWriteImage(m_wand, fileOut);
	
	/* Clean up */
	if(m_wand)m_wand = DestroyMagickWand(m_wand);
	
	MagickWandTerminus();
	return ZF_SUCCESS;
}

ZFBEGIN
	ZFENTRY("resize","cc",resize)
ZFEND

You can now also use Embedded Python to resize images.