Bad quality .png images when I save them using XF's createImageFromFile method.

  • Thread starter Thread starter account8226
  • Start date Start date
A

account8226

Guest
Hello,

When I upload an image, and save it as a .png, the quality is quite bad, it's not anymore transparant and there are kinds of colored stripes/backgrounds.

It's not doing that for avatars, so I guess the problem is in my code.

Any ideas ?
 
Last edited by a moderator:
Did you try it with another browser?

The problem is nor from the web server, nor from the client, since it's working for xenforos' core image
uploads.

It seems that in my addon, it uploads all the time the image, but in some cases the .png transparent image got a big loss of quality on the transparent background.
 
Here is an example, I would like to upload this image :
mario.webp

And after uploading it using my addon, the result is the following :
0cf6f5401bd5d387c312e700468e3503.webp
 
Have you tried running the convert from the command line to help isolate the problem?
 
Hello,

When I upload an image, and save it as a .png, the quality is quite bad, it's not anymore transparant and there are kinds of colored stripes/backgrounds.

Is not doing that for avatars, so I guess the problem is in my code.

Any ideas ?

Have you tried running the convert from the command line to help isolate the problem?
If you want to convo me with the block of code that processes the image I will try what Andy suggested and see if I can find the problem.
 
If you want to convo me with the block of code that processes the image I will try what Andy suggested and see if I can find the problem.

That's kind of you, I can put the code on here :
PHP:
public function uploadImage($id, $logo) {
        $uploadedImage = $logo;
     
        if (!$logo->isImage()) {
            throw new XenForo_Exception(new XenForo_Phrase('uploaded_file_is_not_valid_image'), true);
        }
     
        $dimensions = array(
                'width' => $uploadedImage->getImageInfoField('width'),
                'height' => $uploadedImage->getImageInfoField('height'),
        );
 
        $imageType = $logo->getImageInfoField('type');
        if (!in_array($imageType, array(IMAGETYPE_GIF, IMAGETYPE_PNG, IMAGETYPE_JPEG))) {
            throw new XenForo_Exception(new XenForo_Phrase('uploaded_file_is_not_valid_image'), true);
        }
 
        $imageInfo = getimagesize($logo->getTempFile());
        if (!$imageInfo) {
            throw new XenForo_Exception('Non-image passed in to applyAvatar');
        }
        $width = $imageInfo[0];
        $height = $imageInfo[1];
        $imageType = $imageInfo[2];
 
        if (!in_array($imageType, array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG))) {
            throw new XenForo_Exception('Invalid image type passed in to applyAvatar');
        }
 
        if (!XenForo_Image_Abstract::canResize($width, $height)) {
            throw new XenForo_Exception(new XenForo_Phrase('uploaded_image_is_too_big'), true);
        }
 
        $target = XenForo_Helper_File::getExternalDataPath().'/profil/'.$id.'.jpg';

        $image = XenForo_Image_Abstract::createFromFile($logo->getTempFile(), $logo->getImageInfoField('type'));
        $image->output(IMAGETYPE_JPEG, $target);    //save
 
        return true;
    }

Some code can be redondant, but I really don't find the mistake. That is in my model, and I'm passing out an ID and the upload image that I catched in my controller with :
PHP:
$logo = XenForo_Upload::getUploadedFile('logo');

That's realy strange, it's working in some images, and not on others. The upload never fail, but on some images there is that "bug".

The bug happens always on the same images. When I upload these images as a default avatar from XenForo's core, there isn't nay problem.

Thanks for the help.
 
So the image is being converted from png to jpg.

You can test this from the command line like this:

convert test.png test.jpg

If you get the same results, it's an issue with imagemagick and or the png file.
 
So the image is being converted from png to jpg.

You can test this from the command line like this:

convert test.png test.jpg

If you get the same results, it's an issue with imagemagick and or the png file.

Nice find dude! When I convert from png to jpg with the command "convert" I have the exact same buggy result.

What is strange, is that when I posted this thread, I was saving from png to png.
 
Happy to help.

You might like to see if you have the latest version of imagemagick and if not try upgrading it to solve the problem.

Ok I'll try, but why the same image is working when I upload it as an avatar on my XenForo account ?
 
Are you asking why does the same image work here on xenforo.com ??

No, on my server, if I upload the same image as an avatar, it's working. Maybe the avatar upload is not using imagemagick.
 
Check if you have GD or Imagemagick selected here:

Admin CP -> Home -> Options -> Attachments -> Default Image Processor

Sorry for the late reply,

I have the first "PHP" option checked, it's not GD (I can't even tick the GD setting).
 
Sorry for the late reply,

I have the first "PHP" option checked, it's not GD (I can't even tick the GD setting).

Sorry it's unclear what you are using by your reply. Here's a screen print of mine. Perhaps you could provide a similar screen print.

pic001.webp
 
Sorry for my English too ;)

Here is what I have, I can't tick the ImageMagick option :
2e7d03208736e49fda7a0b8e2e98a4c2.webp
 
Here is what I have, I can't tick the ImageMagick option :

Okay so the reason when you uploaded that png file and it didn't mess it up is that Xenforo is using GD to convert it to a jpg file.

I suggest you ask your web hosting company to install ImageMagick with the PECL Extension. ImageMagick produces much cleaner converted images. Also there's a hack to turn off the compression so avatars look even better.
 
Top Bottom