VirtueMart Forum

VirtueMart 2 + 3 + 4 => Virtuemart Development and bug reports => Coding Central => Topic started by: tobias_conrad on October 31, 2013, 16:24:37 PM

Title: Virtuemart Resize GIF Black Background Issue
Post by: tobias_conrad on October 31, 2013, 16:24:37 PM
Can you confirm and include a solution for this bug in the next version?
I uploaded category .gif pictures with transparent background and the background turned black.
VM2 file for thumb creation:
/administrator/components/com_virtuemart/helpers/img2thumb.php

Virtuemart Resize GIF Black Background Issue
If you have VirtualMart 2.0.22 with Joomla 2.5's default installation, you may find a problem when uploading a picture with GIF format and letting the system generate a thumbnail image - the background of the generated image will have a black background while the original image has a transparent background.

We don't want that of course. It seems like the code has a bug.

Here's a fix for this:

Use your favorite text editor and edit the following file:

your_virtuemart_installation/administrator/components/ com_virtuemart/classes/class.img2thumb.php

Approximately line 210, comment out the following code:

/*
// New modification - creates new image at maxSize
if( $this->maxSize )
{
if( function_exists("imagecreatetruecolor") )
$im_out = imagecreatetruecolor($maxX,$maxY);
else
$im_out = imagecreate($maxX,$maxY);

// Need to image fill just in case image is transparent, don't always want black background
$bgfill = imagecolorallocate( $im_out, $this->bg_red, $this->bg_green, $this->bg_blue );
//$bgfill = imagecolorallocate( $im_out, $this->bg_red, $this->bg_green, $this->bg_blue );
if( function_exists( "imageAntiAlias" )) {
imageAntiAlias($im_out,true);
}
imagealphablending($im_out, false);
if( function_exists( "imagesavealpha")) {
imagesavealpha($im_out,true);
}
if( function_exists( "imagecolorallocatealpha")) {
$transparent = imagecolorallocatealpha($im_out, 255, 255, 255, 127);
}

//imagefill( $im_out, 0,0, $bgfill );
if( function_exists("imagecopyresampled") ){
ImageCopyResampled($im_out, $orig_img, $adjustX, $adjustY, 0, 0, $newxsize, $newysize,$orig_size[0], $orig_size[1]);
}
else {
ImageCopyResized($im_out, $orig_img, $adjustX, $adjustY, 0, 0, $newxsize, $newysize,$orig_size[0], $orig_size[1]);
}

}
else
{

if( function_exists("imagecreatetruecolor") )
$im_out = ImageCreateTrueColor($newxsize,$newysize);
else
$im_out = imagecreate($newxsize,$newysize);

if( function_exists( "imageAntiAlias" ))
imageAntiAlias($im_out,true);
imagealphablending($im_out, false);
if( function_exists( "imagesavealpha"))
imagesavealpha($im_out,true);
if( function_exists( "imagecolorallocatealpha"))
$transparent = imagecolorallocatealpha($im_out, 255, 255, 255, 127);

if( function_exists("imagecopyresampled") )
ImageCopyResampled($im_out, $orig_img, 0, 0, 0, 0, $newxsize, $newysize,$orig_size[0], $orig_size[1]);
else
ImageCopyResized($im_out, $orig_img, 0, 0, 0, 0, $newxsize, $newysize,$orig_size[0], $orig_size[1]);
}


return $im_out;
}

*/

Add the following code instead:

if( $this->maxSize )
{
$im_out = ImageCreateTrueColor($maxX,$maxY);
$bgfill = imagecolorallocate( $im_out, $this->bg_red, $this->bg_green, $this->bg_blue );
imagefill( $im_out, 0,0, $bgfill );
ImageCopyResampled($im_out, $orig_img, $adjustX, $adjustY, 0, 0,
$newxsize, $newysize,$orig_size[0], $orig_size[1]);
}
// Need to image fill just in case image is transparent, don't always want black background
else
{
$im_out = ImageCreateTrueColor($newxsize,$newysize);
$bgfill = imagecolorallocate( $im_out, $this->bg_red, $this->bg_green, $this->bg_blue );
imagefill( $im_out, 0,0, $bgfill );
ImageCopyResampled($im_out, $orig_img, 0, 0, 0, 0,
$newxsize, $newysize,$orig_size[0], $orig_size[1]);
}
return $im_out;
}

The original code was trying to handle this issue but it just doesn't work. The later code seems to be working but we have not fully tested it yet so use at your own risk.

Here's the reference.


posted by Votech Blogger at 11:00 PM
Title: Re: Virtuemart Resize GIF Black Background Issue
Post by: oO_Oo on July 21, 2015, 09:21:25 AM
The problem is still exist in VM 3.0.9.4. If I resized my gif based category image (transparent background), I get black background.
Title: Re: Virtuemart Resize GIF Black Background Issue
Post by: zakgr on March 25, 2017, 23:02:37 PM
I have the same problem with vm3.018
Title: Re: Virtuemart Resize GIF Black Background Issue
Post by: jenkinhill on March 26, 2017, 00:19:19 AM
The server GD library cannot handle transparent gifs or pngs.  Ideslly just use .jpg images.