1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Gaussian blur is removing all transparent pixels from image

Discussion in 'PHP' started by JEET, May 1, 2020.

  1. #1
    I am trying to make a transparent image with a black shape on it.

    Then use IMG_FILTER_GAUSSIAN_BLUR on it to blur the shape a little.

    Problem is as soon as I apply blur filter, entire image loses its transparent pixels, and entire image becomes black.


    So when I copy the final image on a white image, then instead of getting a transparent image with some blurred black shape, I get a completely black image...

    What is wrong with this?


    <?php

    $image= imagecreatetruecolor( 200, 200 );
    imagealphablending($image, false);
    imagesavealpha($image, true);
    $trans= imagecolorallocatealpha($image, 255, 255, 255, 127);
    imagefill( $image, 0,0, $trans);
    $black= imagecolorallocatealpha( $image, 0,0,0,0 );
    imagefilledrectangle( $image, 50,50, 150,150, $black );

    //header("Content-Type: image/png"); imagepng($image); exit();

    //above is our basic image

    //now I gaussian blur this

    imagefilter( $image, IMG_FILTER_GAUSSIAN_BLUR );


    /*
    //now get left most pixel which should be 127.127.127.127
    $rgb= imagecolorat( $image, 0,0 );
    $c= imagecolorsforindex($image, $rgb);
    echo $c['red']."/".$c['green']."/".$c['blue']."/".$c['alpha']."<br>";
    //should still be 255/255/255/127
    //instead is 0/0/0/0 now...
    */


    //copy this on a white image
    //everything becomes black...
    //this is the problem, all transparent pixels are lost after applying blur

    $w=imagesx($image);
    $h=imagesy($image);

    $img = imagecreatetruecolor($w, $h);
    imagesavealpha($img, true);
    $white= imagecolorallocatealpha($img, 255, 255, 255, 0);
    imagefill($img, 0,0, $white);

    imagecopyresampled( $img, $image, 0,0, 0,0, $w,$h, $w,$h );

    //this will output a black image.
    //instead it should have given a white image with a blurred "black" shape on it.
    //comment the imagefilter line to see what should appear (not blurred)
    //I want blurred


    header("Content-Type: image/png"); imagepng($img); exit();
     
    JEET, May 1, 2020 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #2
    A lot of GD image filters are incompatible with transparencies. That's just how it is.

    Again, why PHP is utterly and completely the wrong tool for these sorts of things. If you could separate the alpha channel, blur it separately, then re-apply it, you might be able to do something about that. Problem is that too could give strange results.

    Basically, stop trying to use PHP as something more than glue between database and markup. It's not suited to the task.
     
    deathshadow, May 9, 2020 IP
    JEET likes this.
  3. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #3
    I totally understand and completely agree with you that PHP GD functions are not really good for image manipulation.
    I think alpha support got added in PHP GD much later, hence existing functions before that still do not work with alpha colors.
    But if I want to put up an online tool, and my server does not has JAVA support, then this is the only option I am left with...
     
    JEET, May 9, 2020 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #4
    If this is for your little logo maker, this is why I said make it generate HTML and CSS instead of an image. Or do it client-side with canvas.

    Little trick, you can capture the contents of canvas, base64 encode it, and force it as a image download.
     
    deathshadow, May 9, 2020 IP
    JEET likes this.
  5. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #5
    Canvas is a good idea, but what I am trying to do will throw a huge amount of javascript to client browser. Secondly, I don't want my code logic to be just copied by some other webmaster either. With JS canvas, they can simply look at HTML source code and copy all my js...
     
    JEET, May 9, 2020 IP