Denver Kunaka
Denver Kunaka

Reputation: 19

PHP imagecropauto not cropping transparency but returning a white background

public static function font_preview_img($font_name='', $path=''){
        $font_size = 25;
        $im = imagecreatetruecolor( strlen($font_name)*$font_size, $font_size*2);
        imagesavealpha($im, true);
        imagealphablending($im, false);
        // Create some colors
        $white = imagecolorallocatealpha($im, 255, 255, 255,127);
        //$grey = imagecolorallocate($im, 128, 128, 128);
        $black = imagecolorallocate($im, 1, 1, 1);
        imagefill($im, 0, 0, $white);
        // Add the text
        imagettftext($im, $font_size, 0, 0, $font_size, $black, $path, $font_name);
        
        $crop = imagecropauto($im,IMG_CROP_DEFAULT);
        if($crop !== false){
            imagedestroy($im);
            $im= $crop;
        }
        imagesavealpha($im, true);
        imagepng($im, pathinfo($path)['dirname']."/$font_name.png");
        imagedestroy($im);
    }

Whenever I save the image it returns a white background without being cropped. The alpha is lost. Tried imagesavealpha() after crop but preserves the alpha but no crop. I also moved alpha blend at the top.

Upvotes: 0

Views: 75

Answers (0)

Related Questions