imagecopyresampled_variation1.phpt 1.8 KB

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