Page 1 of 1

Resized PNGs are black...what am I doing incorrectly?

Posted: 2015-12-31T11:34:59-07:00
by TomXampp
When I use Imagick in PHP to resize PNG images, the output images are black.

I've tested the conversion using ImageMagick on my local computer using this command:

Code: Select all

convert x.png -resize 528 -filter Lanczos2Sharp x-resized.png
...and it works; the resulting PNG file displays and is resized appropriately.

However, when I attempt to do the same on my server (both localhost and live), the result is a black image.

The PHP code I'm using is:

Code: Select all

$source = "x.png" // I provide the complete path in my routine, etc.
$destination = "x-resized.png" // likewise.

$im = new Imagick();
$im->readImage($source);
$im->setImageFormat("png");
$im->resizeImage(528, null, Imagick::FILTER_LANCZOS2SHARP, 1);
$im->writeImage($destination);
$im->clear();
What am I doing incorrectly?

Re: Resized PNGs are black...what am I doing incorrectly?

Posted: 2015-12-31T13:48:54-07:00
by fmw42
According to http://us3.php.net/manual/en/imagick.co ... ts.filters, there is no FILTER_LANCZOS2SHARP listed.

Imagick has not kept up with Imagemagick, though I believe some one is now working on that.

Re: Resized PNGs are black...what am I doing incorrectly?

Posted: 2015-12-31T14:03:01-07:00
by TomXampp
Yes, there is; it's documented here:

https://pecl.php.net/package/imagick/3.2.0b2

...and I've successfully used it on JPGs. The PHP page you referenced is simply not updated. Notice that I used it successfully in the command-line version of ImageMagick described in the first part of my message. I've also tried other filters, in case that filter is problematic with PNG, but the result is the same: a solid black image is the result of the conversion.

So: the problem, as I originally stated, is determining the correct sequence of PHP imagick commands to convert the file. Those commands work perfectly with JPGs, but not with PNGs.

Any suggestions?

Re: Resized PNGs are black...what am I doing incorrectly?

Posted: 2015-12-31T15:05:53-07:00
by TomXampp
P.S. I should mention that the LANCZOS2SHARP filter works with JPGs in the PHP routine provided in my posting. Just to double-check, I've tried the routine with the plain LANCZOS filter, and the result is the same for PNG files: a black image is created.

Re: Resized PNGs are black...what am I doing incorrectly?

Posted: 2015-12-31T16:28:17-07:00
by magick
We tried your script with ImageMagick 6.9.2-10 and iMagick 3.3.0. It worked as expected, the resulting image looked like the source image, only larger. We used the rose: image as the source.

Re: Resized PNGs are black...what am I doing incorrectly?

Posted: 2015-12-31T19:48:59-07:00
by TomXampp
I downloaded this PNG image, which I suspect is the rose image you used:

http://www.imagemagick.org/Usage/compose/rose.png

My ImageMagick/imagick info is as follows (here culled from phpinfo.php):
imagick module version 3.4.0RC4
imagick classes Imagick, ImagickDraw, ImagickPixel, ImagickPixelIterator, ImagickKernel
Imagick compiled with ImageMagick version ImageMagick 6.9.2-0 Q16 x86 2015-09-10 http://www.imagemagick.org
Imagick using ImageMagick library version ImageMagick 6.9.2-0 Q16 x86 2015-09-10 http://www.imagemagick.org
ImageMagick copyright Copyright (C) 1999-2015 ImageMagick Studio LLC
ImageMagick release date 2015-09-10
I have the current versions of XAMPP (with PHP 7.01) and WAMP (with PHP 7.00).

On XAMPP, where I do most of my work, it created a black image. Then I tried it on WAMP, and...

...it produced an enlarged version of the original rose image, albeit a bit blurry as you had indicated.

My first thought was, "Wait, is XAMPP malfunctioning? Why did it work correctly in WAMP?" And so I then refreshed the screen in WAMP to run the routine again.

BINGO: It was BLACK.

And every refresh or hard-refresh produced the same result: a black image. Only the first time I had run it in WAMP had it converted correctly. Why? Hmm...

So, I downloaded other PNGs, and try as I might, they all converted to BLACK...from the first time on. It was only the first time I used the routine on WAMP after starting WAMP that it converted correctly...never again.

So, then I tried it on my live server...and...it worked every time, on both JPGs and PNGs. No problem whatsoever, no matter how many times I refreshed.

For some reason, after Imagick has been run on my localhost *once*, it ceases to handle PNG files...but it's fine with JPGs...they may be reprocessed indefinitely.

What follows is my complete testing script. Look at the bottom for the function call [ImagickResize()] where you feed in the prefix and extension (minus the ".") as arguments.

On my localhosts (both XAMPP and WAMP), when I run this repeatedly on the same JPG (by refreshing the screen), it runs anew, and it converts the JPG correctly and displays it correctly each time. On the other hand, it only produces *black* images for PNG files, except for the very first time it is run after first starting the localhost server. Perhaps that's a clue.

I'm using Windows 8.1 Pro.

P.S. Happy New Year!

Code: Select all

<?php
header( 'Content-type: text/html; charset=utf-8' );

while (@ob_end_flush());

function sendMsg($msg) {
	echo $msg;
	flush();
}

function ImagickResize($image_prefix, $ext) {
	
	$server = 'live';
	$separator = "/";
	$basename = basename(__DIR__);
	if (($_SERVER["REMOTE_ADDR"] == "127.0.0.1") || ($_SERVER["REMOTE_ADDR"] == "127.0.0.1/" . $basename . "/")  || ($_SERVER["REMOTE_ADDR"] == "::1")) {
		$server = 'localhost';
		$separator = "/" . $basename . "/";
	}
	
	$source_image_file = $image_prefix . "." . $ext;
	$source_image_path = $_SERVER['DOCUMENT_ROOT'] . $separator . $source_image_file;
	$destination_image_file = $image_prefix . "-resized." . $ext;
	$destination_image_path = $_SERVER['DOCUMENT_ROOT'] . $separator . $destination_image_file;
	
	$w = "Initializing Imagick object...<br>";
	sendMsg($w);
	
	$im = new Imagick();
	// Needed because of variable memory on shared live server:
	if ($server == 'live') {
		$im->setResourceLimit(imagick::RESOURCETYPE_MEMORY, 256);
		$im->setResourceLimit(imagick::RESOURCETYPE_MAP, 256);
		$im->setResourceLimit(imagick::RESOURCETYPE_AREA, 1512);
		$im->setResourceLimit(imagick::RESOURCETYPE_FILE, 768);
		$im->setResourceLimit(imagick::RESOURCETYPE_DISK, -1);
	}

	$source_image_size = filesize($source_image_file);
	$source_image_size_in_MB = $source_image_size / 1000000;

	$x = $source_image_file . " = " . $source_image_size_in_MB . " MB<br>";
	sendMsg($x);
	$y = "Working on $source_image_file ...<br>";
	sendMsg($y);
	
	$im->readImage($source_image_path);
	
	$im->resizeImage(528, null, Imagick::FILTER_LANCZOS2SHARP, 1);
	$im->setImageFormat("jpg");
	$im->stripImage();
	$im->writeImage($destination_image_path);
	$im->clear();
	
	$z = "Done.<br><br>";
	sendMsg($z);
	echo "<img src='" . $destination_image_file . "'>";
}

// Function parameters: ImagickResize([filename-prefix], [filename-extension-WITHOUT-DOT]);
// e.g.: ImagickResize("0", "jpg");
ImagickResize("rose", "png");

?>

Re: Resized PNGs are black...what am I doing incorrectly?

Posted: 2015-12-31T20:09:45-07:00
by fmw42
He was probably referring to the IM internal image "rose:" which is in ppm format. See http://www.imagemagick.org/script/forma ... tin-images

Code: Select all

convert rose: ....

Re: Resized PNGs are black...what am I doing incorrectly?

Posted: 2015-12-31T22:27:43-07:00
by snibgo
I don't know PHP or Imagick, but you seem to be setting the format to "jpg", even though you write an image with "png" extension. If so, then whatever viewer you use to examine the image may be confused.

Re: Resized PNGs are black...what am I doing incorrectly?

Posted: 2016-01-01T01:16:07-07:00
by TomXampp
Thanks for catching that--it was a typo in my example code *only*; the code I was using had 'PNG'. And I double-checked the code on my live site.

However...by changing it to "png" (in double quotes and lower case), suddenly the PNG images converted. That really shouldn't have made a difference. Incredulous, I then left that as-is (with "png" as the image format) and specified JPG files for inputing, and the converter happily processed the files, keeping their JPG extension (presumably changing the internal format to PNG?)

I'm baffled. Surely I did something wrong somewhere. Whatever the case, it's all working now. Many thanks for your patience with a Luddite. (ImageMagick and Imagick are a savior for my website!)