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
  • Thanks. I'll try to find some time to have a look at it.
  • HarroHarro
    Accepted Answer
    Fixed: https://github.com/fuel/core/commit/c55321cf6f3e586a0dbdf47501e290fb27633c8c

    It was indeed because the watermark image uses alphablending, the insert method used imagecopymerge() does not support that, so you lost it when the watermark was copied in.
  • Great ! thanks a lot !
  • I think there is another problem : 

    I try to rotate an image with transparency. The rotated image has a black background.

    In /core/classes/image/gd.php there is : 
    protected function _rotate($degrees)
    {
    extract(parent::_rotate($degrees));
    $degrees = 360 - $degrees;
    $bgcolor = $this->config['bgcolor'] !== null ? $this->config['bgcolor'] : '#000';
    $color = $this->create_color($this->image_data, $bgcolor, 100);
    $this->image_data = imagerotate($this->image_data, $degrees, $color, false);
    }


    is it cause by the line :$bgcolor = $this->config['bgcolor'] !== null ? $this->config['bgcolor'] : '#000'; ?
    Because I try to define bgcolor as null, but it looks like the bgcolor is define as #000 then.
  • Well ... I don't see the black background on the windows explorer, but in my browser ... strange ...

    My code:
    Image::load($file)->config('bgcolor', null)->rotate(90)->save($file);
  • More on the list.
  • I can't reproduce it.

    If I take an image with transparency and rotate it using your code, the image is still transparent.

    Tried with both transparent GIF and PNG files.
  • Well ... if you download this image : http://tinyurl.com/hhnfnyt
    It looks have transparency, but if you open it in photoshop, the transparency background is turned to black.

    This image has been rotated from an image. The original image is OK.
  • Sorry, in fact, I have an image. I copy it with "File::copy()" and then I rotate. It gives the image : http://tinyurl.com/hhnfnyt

    Then my script resize this one has a visible black background.
  • In the documentation of the create_color methode, there is : * @param   integer   $newalpha  The alpha of the color, 0 (trans) to 100 (opaque)

    In the "_rotate" protected function, there is : 

    $color = $this->create_color($this->image_data, $bgcolor, 100);

    If 100 is used, the rotated image wouldn't be opaque ?
  • Hmm...

    Your original image, with your code, produces a rotated image with a vertical cigar and a transparent background here.

    http://zupimages.net/viewer.php?id=16/15/2djy.png

    You did update your local code with my last fixes regarding the watermark issue? Because I think that has an efffect on this issue too.
  • I updated the image classes : 
    /core/image/driver.php
    /core/image/gd.php
    /core/image/imagemagick.php
    /core/image/imagick.php

    I have the same result.

    In a loop, foreach images, I copy the image : File::copy(...), then I do a rotate : Image::load($file)->config('bgcolor', null)->rotate(90)->save($file);

    When I Debug the rotate method in gd.php : 

    protected function _rotate($degrees)
    {
    extract(parent::_rotate($degrees));
    $degrees = 360 - $degrees;
    $bgcolor = $this->config['bgcolor'] !== null ? $this->config['bgcolor'] : '#000';
    $color = $this->create_color($this->image_data, $bgcolor, 100);
            var_dump($bgcolor, $color);
    $this->image_data = imagerotate($this->image_data, $degrees, $color, false);
    }
    the result for my image is : 
    string '#000' (length=4)
    int 0

    Is it normal o have : $color = $this->create_color($this->image_data, $bgcolor, 100); ? The third parameter is 100 ? Doesn't is means opaque ?


    When I debug create_color : 

    protected function create_color(&$image, $hex, $newalpha = null)
    {
    // Convert hex to rgba
    extract($this->create_hex_color($hex));
    // If a custom alpha was passed, use that
    isset($newalpha) and $alpha = $newalpha;
    // Handling alpha is different among drivers
    if ($hex == null)
    {
    $alpha = 127;
    }
    else
    {
    $alpha = 127 - floor($alpha * 1.27);
    }
    var_dump($image, $red, $green, $blue, $alpha);

    // Check if the transparency is allowed
    return imagecolorallocatealpha($image, $red, $green, $blue, $alpha);
    }

    It return : 

    resource(2747, gd)
    int 0
    int 0
    int 0
    float 0
    string '#000' (length=4)

    Alpha is "0". It wouldn't be 127 to have alpha ?
  • You are right in that the code suggests you should get a black background. Problem is I can't reproduce it.

    I used your original image, and this test code:

            $result = Image::load(DOCROOT.'assets/img/cigar.png')
                ->config('bgcolor', null)
                ->rotate(90)
                ->save(DOCROOT.'assets/img/cigar_rotated.png');

    and I get an image of an upright cigar with a transparent background, and not a black background.

    And this makes it difficult for me to debug.
  • I can only force a black background by explicitly setting it, either in the config (and not overriding it in the code), or using

    ->config('bgcolor', '#000')
  • And if you test this : 

    // rotate
            $result = Image::load(DOCROOT.'assets/img/cigar.png')
                ->config('bgcolor', null)
                ->rotate(90)
                ->save(DOCROOT.'assets/img/cigar_rotated.png');
            
    // resize at the same dimension
            $result = Image::load(DOCROOT.'assets/img/cigar_rotated.png')
                ->config('bgcolor', null)
                ->resize(800,160)
                ->save(DOCROOT.'assets/img/cigar_resized.png');


    If I do that, cigar_rotated.png has a transparent background but a black background only if i open it in photophop
    If I do that, cigar_resized.png has a black background.


  • Both have a transparent background here.

    I don't have Photoshop (no Windows), but I've tested in two image viewers, in Firefox, and GIMP. If I remove the alpha channel of those images in GIMP, I get a white background.

    So I'm still clueless to what is going on...
  • OK. Thank to have looked. I will do this in an other way.

    Do you know why the rotated image has a lower quality ?
    even if I set the config : 
    Image::load($file)->config('bgcolor', null)->config('quality',100)->rotate(90)->save($file);
  • No.

    The code doesn't do anything with quality, it only calls http://php.net/manual/en/function.imagerotate.php to rotate the image.

Howdy, Stranger!

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

In this Discussion