Thursday, January 13, 2011

ImageCache Service version 1.0

<?php

// Sample call http://172.26.50.202/imagecache.php?scan_date=20101203&item_key=IMPORT_7cb88ca5813f4e8c97a8cb797d5291cf2&image_name=00000001.tif&format=png&width=0&height=600&orientation=180

// This vlaue should point the the home of the image cache.  The user IUSR must have read access to this directory.
$image_cache_home = "F:\Capture\ImageCache";

// parse url
$scan_date = $_GET['scan_date'];
$item_key = $_GET['item_key'];
$image_name = $_GET['image_name'];
$format = $_GET['format'];
$width = $_GET['width'];
$height = $_GET['height'];
$orientation = $_GET['orientation'];

// assemble full path to image
$image_path = $image_cache_home . "\\" . $scan_date . "\\" . $item_key . "\\" . $image_name;

// load the image
$im = new Imagick( $image_path );

// rotate the image.
if( $orientation > 0 ) {
    $im->rotateImage(new ImagickPixel(), $orientation);
}

// adjust the size ( a value of 0, will retain the aspect ratio)
if( $width > 0 || $height > 0 ) {
    $im->resizeImage($width, $height, Imagick::FILTER_POINT, 1);
}

// change format to png
$im->setImageFormat( $format );

// output the image to the browser as a png
if( $format == "pdf" )
{
    header( "Content-Type: application/pdf" );
}
elseif( $format == "jpg" )
{
    header( "Content-Type: image/jpg" );
}
elseif( $format == "png" )
{
    header( "Content-Type: image/png" );
}
elseif( $format == "tif" )
{
    header( "Content-Type: image/tiff" );
}
elseif( $format == "gif" )
{
    header( "Content-Type: image/gif" );
}

// send the image
echo $im;

// cleanup
$im->clear();
$im->destroy();

?>

How to host php on IIS 7.5

Php application can be hosted on IIS web server by using FastCGI Module that can be found in IIS. Here I am using Windows 7 as my IIS web server. Same is for server 2008 R2. Remember R2 has IIS ver7.5.

First you  need to install IIS in your server, make sure that you have installed CGI feature while installation.

addorremoveCGI
Now you need to Install and Configure the php to run for IIS.
You may go to http://windows.php.net/download/ and download the php msi file for windows (VC9 x86 Non Thread Safe).


At the time of writing, Version 5.3.5 is the latest.  I have copied the files to the network, you can download them from file://bpsnt04/transfer/Setups/PHP/php-5.3.5-nts-Win32-VC9-x86.msi.

Run the installer, when ask for the destination folder change it to c:\php














Select IIS FastCGI from the Web Server Setup screen.

















Make the following changes to c:\php\php.ini
set fastcgi.impersonate = 1
set cgi.force_redirect=0
set cgi.fix=pathinfo=0

Create a new file named c:\inetpub\wwwroot\phpinfo.php with the contents as:
<?php
    phpinfo();
?>

If the php is installed correctly the url http://localhost/phpinfo.php will show the page below.











Additional Resources:
Phil's Blog
Microsoft Technet

Monday, January 10, 2011

Install PHP ImageMagick and imagick on IIS 7.5

Install IIS 7.5 and PHP

Download and install ImageMagick.  Not all versions will work, at the time of writing the current version for windows was 6.6.7-0, however this version does not work, version 6.6.3-0 is the latest version that would work for me.  You can find version 6.6.3-0 at file://bpsnt04/transfer/Setups/ImageMagick/ImageMagick-6.6.3-0-Q8-windows-dll.exe.

You can find all windows version at this mirror http://image_magick.veidrodis.com/image_magick/binaries/

When installing the applications change the path to c:\ImageMagick.  Having spaces in the path seems to be a problem with PHP.
















Do not select any additional tasks from the "Select Additional Tasks" screen, the default is fine.

















To test the installation select Command Prompt from the Windows Start menu. Within the window type:
    convert logo: logo.miff
    imdisplay logo.miff
and the ImageMagick logo should be displayed in a window.


















Imagick is a PHP extension for ImageMagick.  The latest version of this file works fine and can be found at file://bpsnt04/transfer/Setups/ImageMagick/php_imagick.dll.

You can find the latest version at 
http://valokuva.org/builds/.  Be sure to download the x86 / VC9 / non-thread-safe version.

Download the file into the extensions folder for PHP, most likely c:\php\ext.  See the php,ini entry extension_dir for the location on your machine.

You must now configure PHP to know about the new extension.  Edit php.ini and add or uncomment the PHP_IMAGICK entry.

[PHP_IMAGICK]
extension=php_imagick.dll

Reboot the machine then check the phpinfo screen http://localhost/phpinfo.php.  There should now be an IMAGICK section.  Refer to the end of the blog How to host php on IIS 7.5 for help creating the phpinfo.php file.

























The user IUSR must be granted certain privledges. 
    C:\ImageMagick -> Read & execute, List folder contents and Read
    C:\Windows\Temp -> Modify, Read & execute, List folder contents, Read and Write

In addition, you may need to grant permissions to other folders used by PHP and ImageMagick

Some image formats require addional packages, PDF is one of those.  Since it is so common, we are going to install Ghostscript. The latest version at the time of writing works just fine, version 9.00, and be found at \\bpsnt04\transfer\Setups\ImageMagick\gs900w32.zip
All available files can be found at http://pages.cs.wisc.edu/~ghost/.


When installing the package accept all defaults.