2

I'm using JImage to resize uploaded images but when I try to resize transparent png, the resulting image isn't copied to the directory. I'm not able to understand what the issue is. The toFile() method doesn't copy the transparent png in directory, it works well with jpg and png with no transparency.

The code that i'am using is the following :

$image = new JImage($originalFile);
$properties = JImage::getImageFileProperties($originalFile);
$resizedImage = $image->resize('200', '200', true);

$mime = $properties->mime;

if ($mime == 'image/jpeg')
{
    $type = IMAGETYPE_JPEG;
}
elseif ($mime = 'image/png')
{
    $type = IMAGETYPE_PNG;
}
elseif ($mime = 'image/gif')
{
    $type = IMAGETYPE_GIF;
}

$resizedImage->toFile($finalFile, $type);

Any ideas on how to make it work with transparent png?

  • I thought I fixed that here, you may try with this patch that hasn't been merged with master yet – piotr_cz Dec 1 '14 at 9:08
  • I have tried your fix in github.com/joomla/joomla-cms/pull/4234 @piotr_cz and it works fine now :) – user3533235 Dec 20 '14 at 15:27
  • If you have any ideas how to make it merged to core, let me know. – piotr_cz Dec 22 '14 at 9:58
1

If you look at the JImage implementation, it exposes no calls to imagealphablending and other routines typically used to handle transparent images; but looking into the implementation of resize() and crop() it seems to be handling the transparency itself. So (if you're using Joomla 3.x) the code should work with transparent images already.

If you have a use case where it doesn't work, and you're not trying to convert from or to jpg (which of course doesn't support transparency in the first place), just add more info here.

I could point out that the issue may lie with the apparent errors in your code:

elseif ($mime = 'image/png')

you seem to be using one "=" instead of two "=="

  • Thanks to have noticied that i'am using one "=" instead of two "==", i will try to put here the real example that i'am using – user3533235 Dec 1 '14 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.