.png's changing to .jpg's
Permalink Browser Info Environment
I have a gallery with this add-on that is changing my .png's to .jpg's I need my images to be transparent. Has anyone else ran into this?
Type: | Discussion |
---|---|
Status: | New |
That didn't work. I cleared my cache to double check to make sure that wasn't the problem. Mind if I pm you a link to check it out?
Sure that's fine. I'll take a look for you.
JB
JB
Thanks man, I'm really confused.
It says .png now in my source but it still has black where it should be transparent.
I found this thread and posted a reply. Not for sure if it helps at all.
https://www.concrete5.org/community/forums/chat/c5-turns-png-into-a-...
https://www.concrete5.org/community/forums/chat/c5-turns-png-into-a-...
For anyone having this problem in the future... This was my fix for the Gallerific add-on in packages->jb_gallerific->helpers->imageresizer.php replace the first couple of functions with this...
[code]
public function create($originalPath, $newPath, $width, $height) {
// first, we grab the original image. We shouldn't ever get to this function unless the image is valid
$imageSize = @getimagesize($originalPath);
$oWidth = $imageSize[0];
$oHeight = $imageSize[1];
$finalWidth = 0;
$finalHeight = 0;
// first, if what we're uploading is actually smaller than width and height, we do nothing
if ($oWidth < $width && $oHeight < $height) {
$finalWidth = $oWidth;
$finalHeight = $oHeight;
} else {
// otherwise, we do some complicated stuff
// first, we divide original width and height by new width and height, and find which difference is greater
$wDiff = $oWidth / $width;
$hDiff = $oHeight / $height;
if ($wDiff > $hDiff) {
// there's more of a difference between width than height, so if we constrain to width, we should be safe
$finalWidth = $width;
$finalHeight = $oHeight / ($oWidth / $width);
} else {
// more of a difference in height, so we do the opposite
$finalWidth = $oWidth / ($oHeight / $height);
$finalHeight = $height;
}
}
$image = @imageCreateTrueColor($finalWidth, $finalHeight);
switch($imageSize[2]) {
case IMAGETYPE_GIF:
$im = @imageCreateFromGIF($originalPath);
break;
case IMAGETYPE_JPEG:
$im = @imageCreateFromJPEG($originalPath);
break;
case IMAGETYPE_PNG:
$im = @imageCreateFromPNG($originalPath);
break;
}
if ($im) {
// Better transparency - thanks for the ideas and some code from mediumexposure.com
if (($imageSize[2] == IMAGETYPE_GIF) || ($imageSize[2] == IMAGETYPE_PNG)) {
$trnprt_indx = imagecolortransparent($im);
// If we have a specific transparent color
if ($trnprt_indx >= 0) {
// Get the original image's transparent color's RGB values
$trnprt_color = imagecolorsforindex($im, $trnprt_indx);
// Allocate the same color in the new image resource
$trnprt_indx = imagecolorallocate($image, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
// Completely fill the background of the new image with allocated color.
imagefill($image, 0, 0, $trnprt_indx);
// Set the background color for new image to transparent
imagecolortransparent($image, $trnprt_indx);
} else if ($imageSize[2] == IMAGETYPE_PNG) {
// Turn off transparency blending (temporarily)
imagealphablending($image, false);
// Create a new transparent color for image
$color = imagecolorallocatealpha($image, 0, 0, 0, 127);
// Completely fill the background of the new image with allocated color.
imagefill($image, 0, 0, $color);
// Restore transparency blending
imagesavealpha($image, true);
}
}
$res = @imageCopyResampled($image, $im, 0, 0, 0, 0, $finalWidth, $finalHeight, $oWidth, $oHeight);
if ($res) {
switch($imageSize[2]) {
case IMAGETYPE_GIF:
$res2 = imageGIF($image, $newPath);
break;
case IMAGETYPE_JPEG:
$res2 = imageJPEG($image, $newPath, AL_THUMBNAIL_JPEG_COMPRESSION);
break;
case IMAGETYPE_PNG:
$res2 = imagePNG($image, $newPath);
break;
}
}
}
}
[\code]
[code]
public function create($originalPath, $newPath, $width, $height) {
// first, we grab the original image. We shouldn't ever get to this function unless the image is valid
$imageSize = @getimagesize($originalPath);
$oWidth = $imageSize[0];
$oHeight = $imageSize[1];
$finalWidth = 0;
$finalHeight = 0;
// first, if what we're uploading is actually smaller than width and height, we do nothing
if ($oWidth < $width && $oHeight < $height) {
$finalWidth = $oWidth;
$finalHeight = $oHeight;
} else {
// otherwise, we do some complicated stuff
// first, we divide original width and height by new width and height, and find which difference is greater
$wDiff = $oWidth / $width;
$hDiff = $oHeight / $height;
if ($wDiff > $hDiff) {
// there's more of a difference between width than height, so if we constrain to width, we should be safe
$finalWidth = $width;
$finalHeight = $oHeight / ($oWidth / $width);
} else {
// more of a difference in height, so we do the opposite
$finalWidth = $oWidth / ($oHeight / $height);
$finalHeight = $height;
}
}
$image = @imageCreateTrueColor($finalWidth, $finalHeight);
switch($imageSize[2]) {
case IMAGETYPE_GIF:
$im = @imageCreateFromGIF($originalPath);
break;
case IMAGETYPE_JPEG:
$im = @imageCreateFromJPEG($originalPath);
break;
case IMAGETYPE_PNG:
$im = @imageCreateFromPNG($originalPath);
break;
}
if ($im) {
// Better transparency - thanks for the ideas and some code from mediumexposure.com
if (($imageSize[2] == IMAGETYPE_GIF) || ($imageSize[2] == IMAGETYPE_PNG)) {
$trnprt_indx = imagecolortransparent($im);
// If we have a specific transparent color
if ($trnprt_indx >= 0) {
// Get the original image's transparent color's RGB values
$trnprt_color = imagecolorsforindex($im, $trnprt_indx);
// Allocate the same color in the new image resource
$trnprt_indx = imagecolorallocate($image, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
// Completely fill the background of the new image with allocated color.
imagefill($image, 0, 0, $trnprt_indx);
// Set the background color for new image to transparent
imagecolortransparent($image, $trnprt_indx);
} else if ($imageSize[2] == IMAGETYPE_PNG) {
// Turn off transparency blending (temporarily)
imagealphablending($image, false);
// Create a new transparent color for image
$color = imagecolorallocatealpha($image, 0, 0, 0, 127);
// Completely fill the background of the new image with allocated color.
imagefill($image, 0, 0, $color);
// Restore transparency blending
imagesavealpha($image, true);
}
}
$res = @imageCopyResampled($image, $im, 0, 0, 0, 0, $finalWidth, $finalHeight, $oWidth, $oHeight);
if ($res) {
switch($imageSize[2]) {
case IMAGETYPE_GIF:
$res2 = imageGIF($image, $newPath);
break;
case IMAGETYPE_JPEG:
$res2 = imageJPEG($image, $newPath, AL_THUMBNAIL_JPEG_COMPRESSION);
break;
case IMAGETYPE_PNG:
$res2 = imagePNG($image, $newPath);
break;
}
}
}
}
[\code]
I've looked into the code and found that it currently "hard-codes" the ".jpg" extension, so here's a quick workaround:
1) Open the packages/jb_gallerific/helpers/imageresizer.php file (in Notepad, so similar)
2) Find line 130 and change '.jpg' to '.png'
3) Save and upload the modified file
That should sort the problem. The only potential issue I can image by using PNGs with transparency is how the fading transition will look. I've never tested it, so let me know how that goes.
If you're happy with the add-on and support provided, I'd greatly appreciate a positive review in the C5 marketplace:
http://www.concrete5.org/marketplace/addons/gallerific/reviews/...
JB