Mariph

Mariph is a collection of a few classes that can read a subset of Ruby’s Marshal format. I wrote it back when I worked with RPG Maker XP, and wanted to convert the marshalled data from that program into something I could read. In order to do it in Ruby, I would have had to recreate most of the module and class structure defined in RPG Maker, so I instead opted to write a reader in the language most familiar to me – PHP.

It does not support every feature built in to the marshal format, only the ones that I required at the time. This includes: nil, true, false, int, float, string, symbol, regexp (string), array, hash, object, and user. Hash types are returned as a Mariph_Hash, which is a simple representation of a hash table with arbitrary data for keys. Objects are returned as stdClass, with the property __mariphName set to the name of its Ruby counterpart class. User types look for a PHP class defined with the same name as the Ruby class, with any double colon replaced by a double underscore. It then statically calls the class’s rubyLoad() method, which should accept the raw data string as its only parameter, and return an instance of the class. See the Table class in the file for an example.

The simplest way to parse in the data is by calling the mariph_load() shortcut function, which saves you the trouble of initializing both the tokenizer and interpreter classes. Each top-level element in the marshalled string becomes an element of the returned array.

$data = mariph_load($marshalString);

The Mariph_Hash class has your standard get() and set() methods, along with a few convenience methods such as iterate(), count() (implementing Countable), each(), and reset().

$hash = new Mariph_Hash(array('initial' => 'data'));
$hash->set(1, 'one');
$hash->set(2.5, 'two point five');
$hash->set('str', 'string');
$hash->set(array(1, 2, 3), 'one two three');

echo $hash->get(2.5); // "two point five"

// should work in PHP 5.3
$hash->iterate(function($value, $key) {
    echo 'Key: ';
    var_dump($key);
    echo 'Value: ';
    var_dump($value);
});

// Alternatively iterate with each()
while (list($key, $value) = $hash->each()) {
    // do whatever
}

// reset iteration position after using each()
$hash->reset();

I’m sure there’s much better ways for designing a custom hash class than what I wrote, but for me it gets the job done. Hopefully this small library can prove useful to some people. :)

Posted in PHP | Tagged , | Leave a comment

Obfuscated Cake

Almost forgot about this one. I wrote it back in June 2008 for Jeremy’s birthday since he was having a rough day. Like the heart, this is code in the shape of a birthday cake (well, as long as you use some imagination :-P ) that draws a birthday cake. A chocolate one.

The code first draws the main cake rectangle. After that, it loops three times to draw the rectangles for the candlesticks, polygons for the flames, then orange circles with an alpha channel for the light. It finishes off with a series of lines whose lengths are determined by the sine function, used for the frosting.

Here’s the output.

<?php               header             ('Content'.
'-type:'.          'image/png'         );$f = 'im'.
'agecre'.          'atetrueco'.        'lor';$im=$f
(250,250);         $g='imagef'.        'illedrect'.
'angle';$g         ($im,10,120,        240,240,(630*
10000)+ 37         *100+44);for        ($i=0;$i<3;$i
++){$x=60+         $i*60;$g($im        ,$x+2,50,$x+8,
120,0xeeeeee);$h=str_rot13('vzntrsvyyrqcbyltba');$h(
$im,array($x,50,$x+5,30,$x+10,50),3,0xc00000);$h($im,
array($x+2,50,$x+ 5,40,$x+8,50),3, 0xff8000);$h ($im,
array($x+3,50,$x+5,45,$x+7,50),3,0x0000ff);$el='ima'.
'gefilledellipse';$el($im,$x+5,40,40,40,0x48ffa000);}
$l='i'.'m'.'a'. 'g'.'e'.'l'.'in'.'e';$c=0xd0d0ff; for
($i=10;$i<=240;$i++){$y=130+3* sin(($i-10)/2);$l($im,
$i,120,$i,$y,$c);}$p=strrev('egami').'png';$d='imag';
$d.='ede';$de='str';$d.=$de;$d.='oy';$p($im);$d($im);

Unobfuscated version:

<?php

header('Content-type: image/png');
$img = imageCreateTrueColor(250, 250);
imageFilledRectangle($img, 10, 120, 240, 240, 0x603000);

// loop through the 3 candles
for ($i = 0; $i < 3; $i++) {
	$x = 60 + $i * 60;
	// candlestick, red flame, orange flame, blue flame, light
	imageFilledRectangle($img, $x + 2, 50, $x + 8, 120, 0xEEEEEE);
	imageFilledPolygon($img, array($x, 50, $x + 5, 30, $x + 10, 50), 3, 0xC00000);
	imageFilledPolygon($img, array($x + 2, 50, $x + 5, 40, $x + 8, 50), 3, 0xFF8000);
	imageFilledPolygon($img, array($x + 3, 50, $x + 5, 45, $x + 7, 50), 3, 0x0000FF);
	imageFilledEllipse($img, $x + 5, 40, 40, 40, 0x48FFA000);
}

// frosting
for ($i = 10; $i <= 240; $i++) {
	$y = 130 + 3 * sin(($i - 10) / 2);
	imageLine($img, $i, 120, $i, $y, 0xD0D0FF);
}

imagePNG($img);
imageDestroy($img);
Posted in PHP | Tagged , | 1 Comment

Voronoi Diagram

Here’s another imaging example done with PHP – a Voronoi Diagram. I don’t exactly remember when I wrote this, but it was some time this year. The code doesn’t use any fancy, ultra-efficient algorithm, so it takes several seconds to make a medium-size image on my (old) computer.

The algorithm used is very simple: Iterate through each pixel of the image, and find which reference point it is closest to. Then, set the color of the pixel to the color of the reference point.

Here’s the code.

Voronoi Diagram

Voronoi Diagram with 50 Colors

Posted in PHP | Tagged | Leave a comment

Random Line Generator

Another repost, though a bit more fresh – it was originally posted in September 2007.

JS Random Line Generator

The random line generator uses the HTML5 canvas element to draw a series of connected lines, with a large variety of options. You can choose where to start drawing, the initial angle, the line lengths, how far the lines can turn left or right, the number of lines, the line colors, the line width, and the background color. In addition, if you are using a modern browser that supports canvas’s toDataURL() method, you can save the image to your computer (otherwise you’ll likely have to Print Screen it).

One nifty thing you can do with the line colors is cycle through a gradient. By using a color fade generator and selecting “show HTML codes” then the color sequence, you can copy and paste the list into the line colors box. Additionally, it accepts code in HTML (font/color, and span/style), so if somebody has a nice gradient on a website, chances are you can copy/paste the source and use it.

Here’s an example image that I created:

Random Line Example

Random line with black-white gradient

For people with older browsers, there is a PHP version with slightly reduced functionality. It has a max color limit of 30, and no line width setting. It does allow you to turn anti-aliasing on and off, though. The source for the PHP version can be found here.

Posted in JavaScript | Tagged , | Leave a comment

Obfuscated Heart

I originally wrote this in July 2006 for PHP image generation practice, but decided to repost it. Basically, it’s code written in the shape of a heart, that draws a heart.

Going more in-depth, the code uses polar coordinates to draw a cardioid-like figure, which are converted into cartesian coordinates via the cfp_x() and cfp_y() functions. The new coordinates are then used in PHP’s imageFilledPolygon() function to draw the shape.

The formula to create the heart is simply done by setting the radius equal to the angle, rotated 90 degrees so that the shape is right-side up. So, at 0, the coordinates are at the origin – the sharp valley of the heart. At 180, the coordinates are at the bottom point of the heart.

View the output here.

           function                cfp_x($ox,
     $a,$d) {return ($ox+    round((cos (deg2rad($a
   ))*$d)));}function cfp_y($oy, $a, $d){return ($oy+
  round((sin(deg2rad($a)) * $d)));} $crd=array();$ct=0;
 $im=imageCreateTrueColor(250,250);$c=imageColorAllocate
 ($im,255,0,0);header("Content-type: image/png");for($i
  =-180;$i<180;$i++){$crd[]=cfp_x(125,$i -90,abs($i));
    $crd[] = cfp_y(50, $i - 90, abs($i) ); $ct++;}
        imageFilledPolygon($im, $crd,$ct,$c);
            imagePNG($im);imageDestroy
                      (($im));
                         ?>

Unobfuscated version:

function cfp_x($origin_x, $angle, $radius) {
    return $origin_x + round(cos(deg2rad($angle)) * $radius);
}
function cfp_y($origin_y, $angle, $radius) {
    return $origin_y + round(sin(deg2rad($angle)) * $radius);
}

$coords = array();
$num_pts = 0;
$img = imageCreateTrueColor(250, 250);

for ($i = -180; $i < 180; $i++) {
    $coords[] = cfp_x(125, $i - 90, abs($i));
    $coords[] = cfp_y(50, $i - 90, abs($i));
    $num_pts++;
}

imageFilledPolygon($img, $coords, $num_pts, 0xFF0000);
header('Content-type: image/png');
imagePNG($img);
imageDestroy($img);
Posted in PHP | Tagged , | Leave a comment

New Blog

My old blog was scarcely updated, using outdated software, and had a mixed bag of unorganized content. As a result, I decided to start over, and create a blog solely devoted to technical subjects and my programming projects.

Posted in Miscellaneous | Leave a comment