[Challenge] Images the Hard Way

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

[Challenge] Images the Hard Way

Post by McInfo »

Create an image while following these rules:
  • The image can be in any image format (PNG, JPEG, etc.).*
  • You cannot use GD functions, existing classes like ImageMagick, or existing command line tools.
  • The image must have more than one color.
  • All original code must be PHP.
  • All PHP code must be original.
*If you're desperate, ASCII art is acceptable.

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 11:43 am, edited 1 time in total.
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: [Challenge] Images the Hard Way

Post by Weiry »

Finally completed, had a lot of problems with saving as an image file. But saving the image was a part of the other code i wrote, this code simply output's the image to the browser rather than saving it.
There is still one thing i didnt figure out, and thats how to dynamically generate a bmp header, so i stored a default 20x20 header.
The code itself has potential to input many more different colour codes, i just simply took the basic colours:
Black, Grey, White, Red, Green, Blue.

Click the link for a working example:
Create an image using php

Code: Select all

<?php
# create an image using php only
# no GD functions or command line.
#
#   idea:
#       take an array input,
#           convert w : ff ff ff : white
#           convert B : 00 00 00 : black
#           convert G : 55 55 55 : grey
#           convert r : ff 00 00 : red
#           convert g : 00 ff 00 : green
#           convert b : 00 00 ff : blue
$fromChr = array("w","B","G","r","g","b");
$toHex = array("ff ff ff","00 00 00","55 55 55", "ff 00 00", "00 ff 00", "00 00 ff");
 
#   store the bmp header (20x20)
$header = "42 4d 00 00 00 00 00 00 00 00 36 00 00 00 28 00 00 00 14 00 00 00 14 00 00 00 01 00 18 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ";
 
#   create the image string (20x20)
$imageArray = array(
"b b b b b b b b b b b b b b b b b b b b ",
"b B B B B B B B B B B B B B B B B B B b ",
"b B r r r r r r r r r r r r r r r r B b ",
"b B r g w w w w w w w w w w w w g r B b ",
"b B r g w G w w w w G w w w G w g r B b ",
"b B r g w G w w w w G w w w G w g r B b ",
"b B r g w G w w w w G w w w w w g r B b ",
"b B r g w G w w w w G w w w G w g r B b ",
"b B r g w G w w w w G w w w G w g r B b ",
"b B r g w G G G G G G w w w G w g r B b ",
"b B r g w G w w w w G w w w G w g r B b ",
"b B r g w G w w w w G w w w G w g r B b ",
"b B r g w G w w w w G w w w G w g r B b ",
"b B r g w G w w w w G w w w G w g r B b ",
"b B r g w G w w w w G w w w G w g r B b ",
"b B r g w G w w w w G w w w G w g r B b ",
"b B r g w w w w w w w w w w w w g r B b ",
"b B r r r r r r r r r r r r r r r r B b ",
"b B B B B B B B B B B B B B B B B B B b ",
"b b b b b b b b b b b b b b b b b b b b ");
#---------
#   Reverse the string to match bmp storage format
    $hexString = "";
    foreach($imageArray as $string){
        $hexString .= str_replace($fromChr,$toHex,implode(array_reverse(str_split($string))));
    }
    $imgArr = array_reverse(str_split($hexString));
#---------
 
#   Add the BMP header for a (20x20) image.
    $bmpHex = $header.implode($imgArr);
 
//header('Last-Modified: '.date('r'));
header('Content-Length: '.strlen($bmpHex));
header('Content-Type: image/bmp');
print pack('H*',implode(explode(" ",$bmpHex)));
?>
Edit:
Updated some depreciated variable names
Post Reply