Ask your Symfony questions! Pay money and get answers fast! (more info)

Preserving PNG transparency with ecrop and sfThumbnail plugin Symfony

  • SOLVED

If you upload a PNG image to this site, and it has a transparent background, the transparent background becomes black (because the image is resized). [[LINK href="http://bugs.php.net/39273"]]There was once a PHP bug with partial transparency[[/LINK]], but it was fixed a long time ago. [[LINK href="http://alexle.net/archives/131"]]Alex Le[[/LINK]] has written about this issue. [[LINK href="http://support.proboards.com/index.cgi?board=programming&action=print&thread=231268"]]There are ways to perserve PNG transparency with PHP[[/LINK]] but I'm trying to stick with the code in Symfony plugins. I'm having the problem with both the [[LINK href="http://www.symfony-project.org/plugins/eCropPlugin"]]eCrop[[/LINK]] plugin and [[LINK href="http://www.symfony-project.org/plugins/sfThumbnailPlugin"]]sfThumbnail[[/LINK]].

I am curious, were these plugins stupidly written, or this more a matter of my server's settings? Is anyone else having any problems with transparent PNGs, when resized in either of these plugins?


The relevant output of "php -i":

gd
GD Support => enabled
GD Version => 2.0 or higher
FreeType Support => enabled
FreeType Linkage => with freetype
FreeType Version => 2.3.5
T1Lib Support => enabled
GIF Read Support => enabled
GIF Create Support => enabled
JPG Support => enabled
PNG Support => enabled
WBMP Support => enabled
gettext
GetText Support => enabled


Symfony version 1.2.5.

Answers (3)

2010-03-06

Nicolò Pignatelli answers:

Yes, Lawrence. Is a plugin issue.
eCrop, for example, uses <em>imagecopyresampled()</em> to crop an image. Well, this function do not support transparency, unless you apply a workaround.

Try using [[LINK href="http://www.symfony-project.org/plugins/sfImageTransformPlugin"]]<em>sfImageTransformPlugin</em>[[/LINK]], instead; it allows for much more customization and configuration.

This is the workaround I was writing about before:

<?
...
$t_im = imageCreateTrueColor($t_wd,$t_ht);
imageAntiAlias($t_im,true);
imagealphablending($t_im, false);
imagesavealpha($t_im,true);
$transparent = imagecolorallocatealpha($t_im, 255, 255, 255, 0);
for($x=0;$x<$t_wd;$x++) {
for($y=0;$y<$t_ht;$y++) {
imageSetPixel( $t_im, $x, $y, $transparent );
}
}
imageCopyResampled($t_im, $o_im, 0, 0, 0, 0, $t_wd, $t_ht, $o_wd, $o_ht);
imagePNG($t_im, $image_name);
...
?>

The alphablending must be off so the imagesavealpha can be true.

The double FORs is need to fix the problem with imagefill function that not support colors with alpha setted.

2010-03-06

Andrei Dziahel answers:

I've recently implemented transparency preserving for pkImageConverterPlugin (only for GD engine) and planned to submit a patch to upstream. If you consider using this plugin, I can share a fix.

2010-03-06

JuG answers:

Hi,

If your application is running on on Linux (or Mac), I suggest you simply use "convert" !


<?php
function scale( $src_image, $dest_image, $width, $height )
{
$src = escapeshellarg( $src_image );
$dest = escapeshellarg( $dest_image );
$size = escapeshellarg( $width ).'x'.escapeshellarg( $height );

`convert -scale $size $src $dest`;
}


Whatever the image type you use as source or destination (PNG, GIF, JPEG, ...) it should work !