fork download
  1. <?php
  2. header('Content-Type: image/png');
  3. function rgbFromHex($hexValue) {
  4.  
  5. //$hexValue = str_replace('#', '', $hexValue);
  6. //SILLY ME WAS EXPECTING # TO BE PASSED. BUT USING $_GET[] VARIABLES THAT ISN'T POSSIBLE... DAMMIT.
  7.  
  8. if (strlen($hexValue) == 3) { //CHECK FOR SHORTHAND
  9. $r = hexdec(substr($hexValue, 0, 1) . substr($hexValue, 0, 1));
  10. $g = hexdec(substr($hexValue, 1, 1) . substr($hexValue, 1, 1));
  11. $b = hexdec(substr($hexValue, 2, 1) . substr($hexValue, 2, 1));
  12. } else { //FUNCTION FOR NORMAL 6 DIGIT VALUES
  13. $r = hexdec(substr($hexValue, 0, 2));
  14. $g = hexdec(substr($hexValue, 2, 2));
  15. $b = hexdec(substr($hexValue, 4, 2));
  16. }
  17. $rgb = array($r,$g,$b);
  18.  
  19. return $rgb;
  20. }
  21.  
  22. function createSwatch($colors){
  23. $colArray = explode(",", $colors);
  24. $colWidth = 200 / count($colArray);
  25. $im = imagecreate(200, 40);
  26. $x = 0;
  27.  
  28. foreach($colArray as $col){
  29. $rgb = rgbFromHex($col); //DON'T DO ANYTHING IF A COLOR ISN'T PROVIDED
  30. $color = imagecolorallocate($im, $rgb[0], $rgb[1], $rgb[2]); //FIND A BETTER WAY TO DO THIS.
  31.  
  32. imagefilledrectangle($im, $x, 0, $x + $colWidth, 40, $color);
  33. $x += $colWidth;
  34. }
  35.  
  36. return imagepng($im);
  37. }
  38.  
  39. createSwatch('ff0000,00ff00,00f,ff0,0ff')
  40. ?>
Success #stdin #stdout 0.01s 20600KB
stdin
Standard input is empty
stdout