PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, October 10, 2022

[FIXED] how do i use imagecropauto() with IMG_CROP_TRANSPARENT?

 October 10, 2022     gd, php     No comments   

Issue

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);

?>


Answered By - icecub
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing