Quercus: Image Library example

From Resin 3.0

Jump to: navigation, search

Here's a short example of how to use the Quercus image library. This example fetches a fine image of a young man demonstrating his martial arts skills in an unlikely environment. It then shrinks the image, applies a convolution to it, and adds a much-needed embellishment to the scene and displays the resulting image.

Note that this example uses imageconvolution(), which is one of a number of functions which mod_php will not make available unless compiled with special flags. Many prebuilt binary packages are not compiled this way, so most php installations can't use these functions. With Quercus, you always have access to all of the functionality all of the time -- there is no compile-time configuration to worry about.

Here's the code; it downloads the images across the network (using Quercus' equivalent of "fopen wrappers"), so the script is self-contained:

 <?php
   
  $image = imagecreatetruecolor(270, 240);
 
  # fetch the image of a young man with REAL ULTIMATE POWER
  $picture = imagecreatefromjpeg("h ttp://www.realultimatepower.net/ninja/kidgarbage.jpg");
  imagecopyresized($image, $picture, 0, 0, 0, 0, 270, 240, 576, 480);
 
  # apply a convolution
  $matrix = array(    array(  1, -1, -1 ),
                      array( -1,  5, -1 ),
                      array( -1, -1,  1 ) );
  $divisor = 1;
  $offset = 0;
  imageconvolution($image, $matrix, $divisor, $offset);
 
  # laser beams.  everybody needs laser beams.
  $red = imagecolorallocate($image, 255, 0, 0);
  imagesetthickness($image, 3);
  imageline($image, 158, 81, 0, 300, $red);
  imageline($image, 165, 81, 0, 300, $red);
 
  # set the output type and send the image to the client.
  header("Content-type: image/png");
  imagepng($image);
 ?>

Note that due to a parameter-ordering bug you will need a snapshot dated at least 13-Jun-06 or later. Or you can grab a copy from [subversion http://forum.caucho.com/node/23]

Personal tools