Click here to Skip to main content
15,891,033 members
Articles / Programming Languages / PHP

Phalanger, PHP for .NET: Introduction for .NET developers

Rate me:
Please Sign up or sign in to vote.
5.00/5 (41 votes)
24 Jan 2007CPOL29 min read 247.4K   4.1K   140  
Phalanger is a PHP language compiler for the .NET Framework which introduces PHP as a first-class .NET citizen.
<%@ WebHandler Language="PHP" Class="Handler" %><?
    import namespace System;
    import namespace System:::IO;
    import namespace System:::Web;

	// The class won't expose the needed parameterless constructor if it's not exported.
	[Export]
    class Handler implements IHttpHandler {

	    public $IsReusable = true;
    	
	    function ProcessRequest ($context) {
		    // Set up the response settings
		    $context->Response->ContentType = "image/jpeg";
		    $context->Response->Cache->SetCacheability(HttpCacheability::i'Public');
		    $context->Response->BufferOutput = false;
		    
		    $query = $context->Request->QueryString;
		    $photo_id = $context->Request->QueryString->get_Item("PhotoID");
		    $album_id = $context->Request->QueryString->get_Item("AlbumID");
		    $qs_size = $context->Request->QueryString->get_Item("Size");
		    
		    // Setup the Size Parameter
		    switch ($qs_size) {
			    case "S":
				    $size = PhotoSize::Small;
				    break;
			    case "M":
				    $size = PhotoSize::Medium;
				    break;
			    case "L":
				    $size = PhotoSize::Large;
				    break;
			    default:
				    $size = PhotoSize::Original;
				    break;
		    } 
		    // Setup the PhotoID Parameter
		    $id = -1;
		    if (isset($photo_id)) {
			    $id = Convert::ToInt32($photo_id);
			    $stream = PhotoManager::GetPhoto($id, $size);
		    } else {
			    $id = Convert::ToInt32($album_id);
			    $stream = PhotoManager::GetFirstPhoto($id, $size);
		    }
		    // Get the photo from the database, if nothing is returned, get the default "placeholder" photo
		    if ($stream == NULL) $stream = PhotoManager::GetPhotoPlaceholder($size);
		    // Write image stream to the response stream
		    $buffersize = 1024 * 16;
		    $buffer = System:::Array::CreateInstance(Type::GetType("System.Byte"), $buffersize);
		    $count = $stream->Read($buffer, 0, $buffersize);
		    while ($count > 0) {
			    $context->Response->OutputStream->Write($buffer, 0, $count);
			    $count = $stream->Read($buffer, 0, $buffersize);
		    }
	    }

    }
?>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Czech Republic Czech Republic
I live in Prague, the capital city of Czech republic (most of the time Smile | :) ). I've been very interested in functional programming recently and I have a passion for the new Microsoft F# language. I'm writing a book about Functional Programming in the Real World that shows the ideas using examples in C# 3.0 and F#.

I've been Microsoft MVP (for C#) since 2004 and I'm one of the most active members of the F# community. I'm a computer science student at Charles University of Prague. My hobbies include photography, fractals and of course many things related to computers (except fixing them). My favorite book writers are Terry Pratchett and Philip K Dick and I like paintings by M. C. Escher.

PS: My favorite codeproject icon is Sheep | [baah] .

Comments and Discussions