Search code examples
phpgd

how do i use imagecropauto() with IMG_CROP_TRANSPARENT?


When I try to crop transparant area of an image, its get to keep it original size, and the transparant areas are turned black.

If i run this code:

<?php
    // Create a 300x300px transparant image with a 100px wide red circle in the middle
    $i = imagecreatetruecolor(300, 300);
    imagealphablending($i, FALSE);
    imagesavealpha($i, TRUE);
    $transparant = imagecolorallocatealpha($i, 0xDD, 0xDD, 0xDD, 0x7F);
    imagefill($i, 0, 0, $transparant);
    $red = imagecolorallocate($i, 0xFF, 0x0, 0x0);
    imagefilledellipse($i, 150, 150, 100, 100, $red);
    imagepng($i, "red_300.png");

    // Crop away transparant parts and save
    $i2 = imagecropauto($i, IMG_CROP_TRANSPARENT);
    imagepng($i2, "red_crop_trans.png");
    imagedestroy($i2);

    // Crop away bg-color parts and save
    $i2 = imagecropauto($i, IMG_CROP_SIDES);
    imagepng($i2, "red_crop_sides.png");
    imagedestroy($i2);

    // clean up org image
    imagedestroy($i);

I end up with a red_crop_trans.png image that is a 300x300px black image with an 100x100px red circle in it. And a red_crop_sides.png that is a 100x100px black image with a 100x100px red circle in it.

Why is red_crop_trans.png not croped to 100x100px? and why is the background of both images black? And how do I crop them while keeping the transparace?


Solution

  • It took me a while to figure out what exactly was going on. It turned out $i2 = imagecropauto($i, IMG_CROP_TRANSPARENT); was returning false instead of true. According to the docs:

    imagecropauto() returns FALSE when there is either nothing to crop or the whole image would be cropped.

    So instead of IMG_CROP_TRANSPARENT I used IMG_CROP_DEFAULT:

    Attempts to use IMG_CROP_TRANSPARENT and if it fails it falls back to IMG_CROP_SIDES.

    This gave me the expected result. Now I didn't get any black backgrounds myself. But it's a known issue so the solution was quite easily found:

    imagecolortransparent($i, $transparant); // Set background transparent

    And that brings me to the final completed code:

    <?php
        // Create a 300x300px transparant image with a 100px wide red circle in the middle
        $i = imagecreatetruecolor(300, 300);
        imagealphablending($i, FALSE);
        imagesavealpha($i, TRUE);
        $transparant = imagecolorallocatealpha($i, 0xDD, 0xDD, 0xDD, 0x7F);
        imagecolortransparent($i, $transparant); // Set background transparent
        imagefill($i, 0, 0, $transparant);
        $red = imagecolorallocate($i, 0xFF, 0x0, 0x0);
        imagefilledellipse($i, 150, 150, 100, 100, $red);
        imagepng($i, "red_300.png");
    
        // Crop away transparant parts and save
        $i2 = imagecropauto($i, IMG_CROP_DEFAULT); //Attempts to use IMG_CROP_TRANSPARENT and if it fails it falls back to IMG_CROP_SIDES.
        imagepng($i2, "red_crop_trans.png");
        imagedestroy($i2);
    
        // Crop away bg-color parts and save
        $i2 = imagecropauto($i, IMG_CROP_SIDES);
        imagepng($i2, "red_crop_sides.png");
        imagedestroy($i2);
    
        // clean up org image
        imagedestroy($i);
    
    ?>