Resize and change DPI of image and preserve transparency

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
Gekiboy

Resize and change DPI of image and preserve transparency

Post by Gekiboy »

I'm having trouble using IMagick to resize images and change their DPI while retaining their transparency. I have tried with both PNGs and GIFs. For PNGs the transparency turns black and for GIFs the transparency turns white. This happens whether I just do the resize or just change the DPI setting. Details are as follows

ImageMagick 6.5.5-7 2010-01-29 Q16
PHP 5.2.10 with Suhosin-Patch 0.9.7 (cli) (built: Nov 16 2009 20:12:44)
FreeBSD 8.0-RELEASE FreeBSD 8.0-RELEASE #0: Sat Nov 21 15:02:08 UTC 2009 amd64

Code is as follows:

Code: Select all

	$image_file = new Imagick(file_directory_path() . "/image.gif");
	$image_file->setImageOpacity(1.0);
	if (!$image_file->resampleImage(300, 300, 0, 1) || !$image_file->resizeImage(70 * (300/72), 70 * (300/72), 0, 1)) {
		#Error out
	}
	else {
		$image_file->writeImage(file_directory_path() . "/image.gif");
	}
Gekiboy

Re: Resize and change DPI of image and preserve transparency

Post by Gekiboy »

Realized I should also include my imagick library info :P

imagick module: enabled
imagick module version: 2.2.2
imagick classes: Imagick, ImagickDraw, ImagickPixel, ImagickPixelIterator
ImageMagick version: ImageMagick 6.5.5-7 2010-01-29 Q16 http://www.imagemagick.org
ImageMagick copyright: Copyright (C) 1999-2009 ImageMagick Studio LLC
ImageMagick release date: 2010-01-29
ImageMagick Number of supported formats: 194
ImageMagick Supported formats: A, AI, ART, ARW, AVI, AVS, B, BGR, BIE, BMP, BMP2, BMP3, BRG, C, CAPTION, CIN, CIP, CLIP, CMYK, CMYKA, CR2, CRW, CUR, CUT, DCM, DCR, DCX, DDS, DFONT, DNG, DPS, DPX, EPDF, EPI, EPS, EPS2, EPS3, EPSF, EPSI, EPT, EPT2, EPT3, ERF, FAX, FITS, FPX, FRACTAL, FTS, G, G3, GBR, GIF, GIF87, GRADIENT, GRAY, GRB, HISTOGRAM, HRZ, HTM, HTML, ICB, ICO, ICON, INFO, INLINE, IPL, JBG, JBIG, JNG, JP2, JPC, JPEG, JPG, JPX, K, K25, KDC, LABEL, M, M2V, M4V, MAP, MAT, MATTE, MIFF, MNG, MONO, MOV, MP4, MPC, MPEG, MPG, MRW, MSL, MSVG, MTV, MVG, NEF, NULL, O, ORF, OTB, OTF, PAL, PALM, PAM, PATTERN, PBM, PCD, PCDS, PCL, PCT, PCX, PDB, PDF, PDFA, PEF, PFA, PFB, PFM, PGM, PGX, PICON, PICT, PIX, PJPEG, PLASMA, PNG, PNG24, PNG32, PNG8, PNM, PPM, PREVIEW, PS, PS2, PS3, PSD, PTIF, PWP, R, RADIAL-GRADIENT, RAF, RAS, RBG, RGB, RGBA, RGBO, RLA, RLE, SCR, SCT, SFW, SGI, SHTML, SR2, SRF, STEGANO, SUN, SVG, SVGZ, TEXT, TGA, THUMBNAIL, TIFF, TIFF64, TILE, TIM, TTC, TTF, TXT, UIL, UYVY, VDA, VICAR, VID, VIFF, VST, WBMP, WMV, WPG, X, X3F, XBM, XC, XCF, XPM, XPS, XV, XWD, Y, YCbCr, YCbCrA, YUV

Directive Local Value: Master Value
imagick.locale_fix: 0 0
imagick.progress_monitor: 0 0
Gekiboy

Re: Resize and change DPI of image and preserve transparency

Post by Gekiboy »

Okay, for anyone else who might experience this issue, I've discovered the solution. Basically, you need to load in your image like this:

Code: Select all

	$image_file = new Imagick(file_directory_path() . "/image.png");
	$image_file->setImageColorspace(imagick::COLORSPACE_TRANSPARENT);
	$image_file->readImage(file_directory_path() . "/image.png");
The reason for this is that you need to have the colorspace set to transparent when you load your image, but you also can't set the colorspace until after an image has been loaded. So basically, you need to load your image twice (or load a tiny image the first time I guess if you want to streamline things).
Post Reply