Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Image class and bgcolor in preset
  • I have a problem with the Image Class. I use preset in order to create thumbnails like that : 

    Image::load($image_file)->preset('product_thumbnail_defined', $image->file);

    My preset is defined : 

    return array(
    'driver' => 'gd',
    'bgcolor' => null,
    'filetype' => null,
    'clear_queue' => true,
    'persistence' => false
    'product_thumbnail_defined' => array(
    'quality' => 100,
    'bgcolor' => null,
    'actions' => array(
                    array('resize', 160, 800, false, false),
    array('save', Config::get('product_images') . DS . Config::get('thumbnail_folder_defined_fiche') . DS.'$1'),
                    
    array('resize', 317, 70, false, false),
    array('save', Config::get('product_images') . DS . Config::get('thumbnail_folder_defined_liste') . DS.'$1'),
                    
                    array('resize', 50, 245, false, false),
    array('save', Config::get('product_images') . DS . Config::get('thumbnail_folder_defined_grille') . DS.'$1'),
    ),
    )
    )


    The files created are png but the transparancy is changed in black.
    If I define the main bgcolor to red for exemple, the first thumbnails background is red but the other are black.

    Even if I define bgcolor in the preset, it has no effect. Is it normal ? What i have done wrong ?
  • I've never used Image personally, so I have to dive into it.

    Which driver are you using (see your image.php config file)?
  • I'm using GD : 'driver' => 'gd',
  • Sorry, just noticed that in your first post. ;)

    I'll have to setup some test code to see exactly what happens.

    I read that a lot of people are struggling with this, and code that works for some doesn't work for others, so I wonder is it is GD version dependent.
  • In my PHPinfo I have :

    gd

    GD Supportenabled
    GD Versionbundled (2.1.0 compatible)
    FreeType Supportenabled
    FreeType Linkagewith freetype
    FreeType Version2.5.5
    GIF Read Supportenabled
    GIF Create Supportenabled
    JPEG Supportenabled
    libJPEG Version9 compatible
    PNG Supportenabled
    libPNG Version1.5.18
    WBMP Supportenabled
    XPM Supportenabled
    libXpm Version30411
    XBM Supportenabled
    WebP Supportenabled
  • I think I know what happens.

    The class first creates a transparent image with the new dimensions. It then fills the image with the background color defined (or black if bgcolor is NULL), and then sets this color as alpha, and enables transparency. It then copies the original image in using imagecopyresampled().

    If the original image is transparent too, but is uses a different transparency color, the background of the original image bleeds through, which is why you see black or red. This may also explain why people on StackOverflow say that sometimes transparency works, sometimes not. Because it depends on the color used as alpha channel. If you know the alpha channel color from the original image, can you check what happens if you set bgcolor to that color?

    The fact that the first one is red but the others are black suggests the color config is lost somewhere.

    What Fuel version are you using? If not 1.8, please upgrade first so that I don't spend time looking for something that may already be fixed.
  • I think I can reproduce the problem, but I have it the other way around. If I set the bgcolor to red, the large image has a black background, the medium and small images have a red background.

    Also understand that these actions are cascading. So you don't resize the original, you resize the previous image. So the last one is resized three times. This may also cause the difference in background color.
  • Yeah, if you process them individually, they all have a red background, so the different background issue is caused by the recursive resizing.
  • Thank for your answers.

    I tried to always defined bgcolor as null and change my preset (just one resize) :


            // default fiche
    'product_thumbnail_default' => array(
    'quality' => 100,
    'bgcolor' => null,
    'actions' => array(
                    
    array('resize', 300, 300, false, false),
    array('save', Config::get('product_images') . DS . Config::get('thumbnail_folder_default_fiche') . DS.'$1'),
    ),
    ),


    The background is still black.

    I don't understand ...
  • I don't know the alpha channel color from the original image
  • It looks like the parent bgcolor doesn't work. If i define 'bgcolor' => '#ff0000',, all the thumbnails are red. 
    If I defined 'bgcolor' => null, there are black.
  • You should not define a bgcolor. It does what it says, it forces the bgcolor onto the image.

    The problem seems to be that the transparent parent image, into which the loaded image is copied, isn't transparent. I've been busy with it for over two hours, but haven't found a solution yet, so please be patient.
  • HarroHarro
    Accepted Answer
  • OK thanks.
  • oh waw ! great ! thanks a lot!
  • Yes ! it works ! you saved my day !
  • You're welcome. Took a lot longer than expected. Happy to hear you're sorted now.
  • So I continue ... I add a watermark. 
    My watermark is a png with transparency.
    When the watermark is over a pixel on the parent image, no problem. But when is over a transparent pixel, it looks very bad, pixelated.

    Have you a nice idea for that ?
  • I have watermark_alpha:100.
    I test watermark_alpha:0, the watermark is visible only on th transparency.
  • Watermarks should be semi-transparent, the default in the config is 75.

    The watermark image itself should not have any transparency, the watermark() function will do that for you.
  • Just tried here, works fine, even if the watermark file itself has transparency.

    Try to put the watermark on after resizing, to avoid as many artifacts as possible.
  • In my preset, I resize, then i put the watermark.

    The image is the red, the watermark is the face : http://www.zupimages.net/viewer.php?id=16/15/2eep.png


    Do you see how the watermark is perfect on the red, but not at all on the transparency of the image ?
  • My preset is : 

    'watermark_alpha' => 100,
    'clear_queue' => true,
    'persistence' => false,

    'product_thumbnail_defined' => array(
    'quality' => 100,
    'bgcolor' => null,
    'actions' => array(

                    array('resize', 160, 800, true, true),
    array('watermark', Config::get('watermark_defined_fiche'), 'center right', -5),
    array('save', Config::get('product_images') . DS . Config::get('thumbnail_folder_defined_fiche') . DS.'$1'),
                    
    array('resize', 317, 70, true, true),
    array('watermark', Config::get('watermark_defined_liste'), 'left', '150'),
    array('save', Config::get('product_images') . DS . Config::get('thumbnail_folder_defined_liste') . DS.'$1'),
                    
                    array('resize', 50, 245, true, true),
    array('watermark', Config::get('watermark_defined_grille'), 'center center'),
    array('save', Config::get('product_images') . DS . Config::get('thumbnail_folder_defined_grille') . DS.'$1'),
    ),
    ),
  • I did another test :

    watermark with a fade transparency : 

    The result :

    We should see a blue near white at the left and the right of the red.

    Does it come from the alpha of the watermark ?
  • Possible. But it could also be something in the code. I looked at the result in GIMP, and the blue outside the red is not transparent.

    I need to test it, because the entire image class code is new to me. Not sure if I have time for it today.

    Can you also upload your red image, so I can test with exactly your code and images?
  • OK, thank you.

    I add the watermark to the image with photoshop, and it does : http://www.zupimages.net/viewer.php?id=16/15/8uye.png

    It may come from the quality of the watermak ... I will 


    Source : 
    Red image :

    Watermark : 

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion