PHP GD library - All colors added are Transparent

115 Views Asked by At

I'm trying to write a small program to create cartoon eye's and output them to a folder (PHP CLI) and I'm running into trouble with the expected output. I have two functions that copy sections from an image, one for rotating while preserving the original size and one to get a small section for styling needs. When the functions are used in the order of rotatePreserve() then partialEye() the addcolorallocate in partialEye always becomes transparent. I've tried various colors, all end up with the same result. I've searched and read, but I don't understand why this is happening?

I've tried outputting the image halfway through and createimagefrompng with the same results, I'm failing to understand why the color isn't added correctly. If you run the code, you'll see that the output testing along the way works unless rotatePreserve is called before the partialEye function.

Please help me understand what going wrong here.

Thanks

// Setup Variables
$fixed_x = 512;
$fixed_y = 512;

$output_dir = './output';
$output_filename = 'eye_test.png';

// Output info
echo "\e[1;30;40mBeginning Eye Creator......\n";
echo "Image Size: $fixed_x x $fixed_y \n";

// Functions
function partialEye ($img, $cut, $x, $y, $brow) {
    //New image, same size, get bottom part of Eye (Optionally add eye lid )

    $part_eye = imagecreate($x, $y);
    imagealphablending($part_eye, true);
    $transparent = imagecolorallocatealpha($part_eye , 0, 0, 0, 127);
    imagefill($part_eye, 0, 0, $transparent);
    imagecopy($part_eye, $img, 0, $cut, 0, $cut, $x, $y-$cut);
    $blk = imagecolorallocate($part_eye, 0,0,0);
    if ($brow === 1) {
        //If ran after the rotate// draws in transparent??
        imagefilledellipse($part_eye, 256-15, $cut, 256, 15, $blk);
    } elseif ($brow === 2) {
        imagefilledellipse($part_eye, 256+15, $cut, 256, 15, $blk);
    }

    return $part_eye;
    imagedestroy($part_eye);
}

function rotatePreserve($img, $orig_x, $orig_y, $deg) {
    $width_before = $orig_x;
    $height_before = $orig_y;

    $rotateImgTransparency = imagecolorallocatealpha($img , 1, 1, 1, 127);
    $img = imagerotate($img, $deg, $rotateImgTransparency);
    //but imagerotate scales, so we clip to the original size
    $img2 = imagecreatetruecolor($orig_x, $orig_y);
    imagealphablending($img2, false);
    imagesavealpha($img2, true);

    $rotateImg2Transparency = imagecolorallocatealpha($img2 , 0, 0, 0, 127);
    imagefill($img2, 0, 0, $rotateImg2Transparency);

    $new_width = imagesx($img); // where dimensions are
    $new_height = imagesy($img);// the scaled ones (by imagerotate)
    imagecopyresampled(
        $img2, $img,
        0, 0,
        ($new_width-$orig_x)/2,
        ($new_height-$orig_y)/2,
        $orig_x,
        $orig_y,
        $orig_x,
        $orig_y
    );
    return $img2;
    imagedestroy($img);
    imagedestroy($img2);
}

//New Image
$new_image = imagecreatetruecolor($fixed_x, $fixed_y);
imagealphablending($new_image, false);

//Colors for $new_image
$white_color = imagecolorallocate($new_image, 255, 255, 255 );
$blk_color = imagecolorallocate($new_image, 0,0,0);
$pngTransparency = imagecolorallocatealpha($new_image , 0, 0, 0, 127);
$rand_R = rand(0,255);
$rand_G = rand(0,255);
$rand_B = rand(0,255);
$iris_color = imagecolorallocate($new_image, $rand_R,$rand_G,$rand_B);
imagefill($new_image, 0, 0, $pngTransparency);

$size = 256;
$draw_x = $fixed_x/2;
$draw_y = $fixed_y/2;

imagefilledarc($new_image, $draw_x, $draw_y, $size, $size, 0, 360, $blk_color, IMG_ARC_PIE);
imagefilledarc($new_image, $draw_x, $draw_y, $size-20, $size-20, 0, 360, $white_color, IMG_ARC_PIE);
imagefilledarc($new_image, $draw_x, $draw_y, $size-100, $size-100, 0, 360, $iris_color, IMG_ARC_PIE);
imagefilledarc($new_image, $draw_x, $draw_y, $size-170, $size-170, 0, 360, $blk_color, IMG_ARC_PIE);
imagefilledellipse($new_image, $draw_x, $draw_y-50, $size-180, $size-220, $white_color);
imagefilledellipse($new_image, $draw_x, $draw_y+50, $size-200, $size-225, $white_color);


// Cut top part of Eye off
$partial_eye = partialEye($new_image, 192, $fixed_x, $fixed_y, 0);

// Cut top part of eye and add lid
$left_eye_add_lid_eye = partialEye($new_image, 192, $fixed_x, $fixed_y, 1);

// Rotate and Preserve Size
$left_eye = rotatePreserve($left_eye_add_lid_eye, $fixed_x, $fixed_y, -13);


imagesavealpha($left_eye, true);
imagepng($left_eye, $output_dir.'/left_eye.png', 0, 9);

$first_rotate = rotatePreserve($new_image, $fixed_x, $fixed_y, 26);


// FAILS!! 
$right_eye_add_lid_eye = partialEye($first_rotate, 192, $fixed_x, $fixed_y, 2);

//Testing shows the fail with any color choice becoming transparent
imagesavealpha($right_eye_add_lid_eye, true);
imagepng($right_eye_add_lid_eye, $output_dir.'/'.$output_filename, 0, 9);

// Then rotate and flip for final position this would finish the right eye in the correct position
// this would give the expected result if the "eye lid" wasn't transparent

// $right_eye_add_lid_eye = rotatePreserve($right_eye_add_lid_eye, $fixed_x, $fixed_y, -13);
// imageflip($right_eye_add_lid_eye, IMG_FLIP_HORIZONTAL);
// imagesavealpha($right_eye_add_lid_eye, true);
// imagepng($right_eye_add_lid_eye, $output_dir.'/right_eye.png', 0, 9);

results

0

There are 0 best solutions below