1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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

Discussion in 'XenForo Development Discussions' started by account8226, Jul 1, 2013.

  1. account8226

    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: Jul 1, 2013
  2. whynot

    whynot Well-Known Member

    Did you try it with another browser?
     
  3. account8226

    account8226 Guest

    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.
     
  4. account8226

    account8226 Guest

    Here is an example, I would like to upload this image :
    mario.png

    And after uploading it using my addon, the result is the following :
    0cf6f5401bd5d387c312e700468e3503.png
     
  5. AndyB

    AndyB Well-Known Member

    Have you tried running the convert from the command line to help isolate the problem?
     
  6. EQnoble

    EQnoble Well-Known Member

    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.
     
    account8226 likes this.
  7. account8226

    account8226 Guest

    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_GIFIMAGETYPE_PNGIMAGETYPE_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_GIFIMAGETYPE_JPEGIMAGETYPE_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.
     
  8. account8226

    account8226 Guest

    The same image, uploaded with XenForo's core is working fine.
     
  9. AndyB

    AndyB Well-Known Member

    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.
     
  10. AndyB

    AndyB Well-Known Member

    That's testing imagemagick on this server. You need to test it on your server.
     
  11. account8226

    account8226 Guest

    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.
     
  12. AndyB

    AndyB Well-Known Member

    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.
     
  13. account8226

    account8226 Guest

    Ok I'll try, but why the same image is working when I upload it as an avatar on my XenForo account ?
     
  14. AndyB

    AndyB Well-Known Member

    Are you asking why does the same image work here on xenforo.com ??
     
  15. account8226

    account8226 Guest

    No, on my server, if I upload the same image as an avatar, it's working. Maybe the avatar upload is not using imagemagick.
     
  16. AndyB

    AndyB Well-Known Member

    Check if you have GD or Imagemagick selected here:

    Admin CP -> Home -> Options -> Attachments -> Default Image Processor
     
  17. account8226

    account8226 Guest

    Sorry for the late reply,

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

    AndyB Well-Known Member

    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.jpg
     
  19. account8226

    account8226 Guest

    Sorry for my English too ;)

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

    AndyB Well-Known Member

    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.
     
    account8226 likes this.

Share This Page