1--TEST-- 2Test for correct colors of imagecopyresampled() wrt. alpha 3--SKIPIF-- 4<?php 5if (!extension_loaded('gd')) die('skip ext/gd required'); 6?> 7--FILE-- 8<?php 9 10const EXP_RED = 66; 11const EXP_GREEN = 66; 12const EXP_BLUE = 133; 13const EXP_ALPHA = 32; 14 15/* create the source image */ 16$im = imagecreatetruecolor(10, 10); 17imagealphablending($im, false); 18$solid = imagecolorallocate($im, 0, 100, 150); 19$transparent = imagecolorallocatealpha($im, 200, 0, 100, 64); 20 21/* draw a checker pattern */ 22for ($i = 0; $i < imagesx($im); $i++) { 23 for ($j = 0; $j < imagesy($im); $j++) { 24 imagesetpixel($im, $i, $j, ($i%2 != $j%2 ? $solid : $transparent)); 25 } 26} 27 28/* create the destination image */ 29$copy = imagecreatetruecolor(5, 5); 30imagealphablending($copy, false); 31imagesavealpha($copy, true); 32imagecopyresampled($copy, $im, 0,0, 0,0, 5,5, 10, 10); 33 34/* assert all pixels have the same color */ 35$color = imagecolorat($copy, 3, 3); 36for ($i = 0; $i < imagesx($copy); $i++) { 37 for ($j = 0; $j < imagesy($copy); $j++) { 38 if (imagecolorat($copy, $i, $j) != $color) { 39 echo 'different pixel values', PHP_EOL; 40 } 41 } 42} 43 44/* assign actual component values */ 45$red = ($color & 0xFF0000) >> 16; 46$green = ($color & 0x00FF00) >> 8; 47$blue = ($color & 0x0000FF); 48$alpha = ($color & 0x7F000000) >> 24; 49 50/* test for expected component values */ 51if (!($red >= EXP_RED - 1 && $red <= EXP_RED + 1)) { 52 printf("red: expected roughly %d, got %d\n", EXP_RED, $red); 53} 54if (!($green >= EXP_GREEN - 1 && $green <= EXP_GREEN + 1)) { 55 printf("green: expected roughly %d, got %d\n", EXP_GREEN, $green); 56} 57if (!($blue >= EXP_BLUE - 1 && $blue <= EXP_BLUE + 1)) { 58 printf("blue: expected roughly %d, got %d\n", EXP_BLUE, $blue); 59} 60if (!($alpha >= EXP_ALPHA - 1 && $alpha <= EXP_ALPHA + 1)) { 61 printf("alpha: expected roughly %d, got %d\n", EXP_ALPHA, $alpha); 62} 63 64imagedestroy($copy); 65imagedestroy($im); 66 67echo 'DONE'; 68?> 69--EXPECT-- 70DONE 71