1

I'm running php on an intranet-server w/o www-connectivity and without any graphics-libraries installed. Now I need to create a color-block of 16x16 pixels (coloured #86D0FF) and was wondering if there was a way to return the required sequence of bytes for such a simple thing without having to install the libraries?

In other words: I'd like to achieve the following without having GD installed:

<?php 
    header("Content-Disposition: Attachment;filename=image.png"); // so that it can be saved...
    header("Content-type: image/png");

    $img = imagecreate(16,16);
    $color = imagecolorallocate( $image , 134 , 208 , 255 );
    imagefilledrectangle( $image , 0 , 0 , 16 , 16 , $color );
    imagepng( $image );
    imagedestroy( $image );
Jongware
  • 22,200
  • 8
  • 54
  • 100
MBaas
  • 7,248
  • 6
  • 44
  • 61

1 Answers1

1

Ok, I found a solution. Created samples and saved as JPG, ICO, PNG and GIF and looked at them with an hex-editor, decided GIF looked like the easiest one. So I'm now reading an existing GIF (as a template) and just manipulate bytes[37..39] which (in that specific file) hold the colour-value.

The colour-values I am taking from the CSS-File where they are defined, so there also is a bit of css-parsing. Maybe may code can help to inspire you ;-)

<?php

   $css = file_get_contents('my.css');
   $col = $_GET["col"];   // file is referenced using /coloursample.php?col=#
   $patt = '/\.colGroup' . $col . '.*?background-color:.*?\#([A-F0-9]{3,6};/six';
   $bin = file_get_contents('sample.gif');
   if (preg_match($patt,$css,$regs))
   {
      $bin[37] = chr(hexdec(substr($regs[1],0,2)));
      $bin[38] = chr(hexdec(substr($regs[1],2,2)));
      $bin[39] = chr(hexdec(substr($regs[1],4,2)));
   }


  header("Content-Disposition: Attachment; filename=color" . $regs[1] . "gif");
  header("Cpntent-type: image/gif");
  echo $bin;
MBaas
  • 7,248
  • 6
  • 44
  • 61
  • I still don't understand why it needs to be an image.. but since it was only 16x16 I was going to recommend looking up bitmap specification. From what I saw of it it ought to be pretty easy to build a bitmap file with PHP if you're doing 24bit color depth. The hard part is constructing the header. After that it's just writing byte by byte RGBRGBRGBRGB. – fie Sep 17 '14 at 05:14
  • I'm building a contextmenu, the library I'm using requires an image to be used when one wants to attach an image to a menu-option. And btw, with my solution I am following your suggestion - I found GIF was easiest to manipulate (I'm using a "template"-GIF and just inject the colour at the appropriate positions - only need to change 3 bytes :) – MBaas Sep 17 '14 at 07:13