PHP and .NET - Image Rotation Example using C# and PHP : COM (Windows) : COM and .Net (Windows) PHP Source Code

PHP Source Code » COM and .Net (Windows) » COM (Windows) »


PHP and .NET - Image Rotation Example using C# and PHP

We want to extend PHP using a language he had been already using for some of his back-end work, in this case, Microsoft's C# language which is part of .NET.

I quickly launched my VMWare Fusion into Vista, gulp, and proceeded to run Microsoft's Visual Studio 2005. This posting won't be a tutorial on how to write C#, but i'll just go over some of the basics of setting up a simple C#-based COM object that can be used from within PHP.

The first thing to note is that to register COM objects in Vista, you need to be Administrator, so just launch Visual Studio as Administrator using the right-click menu 'Run as Administrator'. Once you have Visual Studio running, start by creating a new 'Class Library' project.

Once you've created the project, right-click on the project and go to the Properties menu, we'll want to make this object a COM-visible one.

In the 'Build' section of the properties, scroll to the bottom and check the 'Register for COM Interop' checkbox. This will make your object visible to other languages as a COM object.

The next thing you'll need to do is open your 'AssemblyInfo.cs' file, which is usually in your Properties folder of your project. Be sure to change the 'false' value to 'true' for the 'ComVisible' setting:

Now that you've done that, you can just create a COM object the 'usual' way. The complete project is attached, so you can take a look there.

Once you've got your object compiled, using it from within PHP is super-easy! You can just use the COM object function to create a wrapped instance of the native object and then just use functions and properties on it just as if it were native PHP.

So here is the very short PHP Script code running server-side that creates the COM object, loads our image, rotates it via the input field in the HTML form, and then saves it back as a new image. The communications from PHP to .NET happens very quickly and as you can see, was super-simple to set up.

Here's a screenshot of the image being rotated:

What's really cool is that you can write anything you like in C#/VB.NET and easily access it via PHP.

MyImageProcessor.zip15.48 KB

HTML code for linking to this page:

Follow Navioo On Twitter

PHP Source Code

 Navioo COM and .Net (Windows)
» COM (Windows)