Quercus: Image Library example

From Resin 3.0

(Difference between revisions)
Jump to: navigation, search
 
 
Line 1: Line 1:
Here's a short example of how to use the Quercus image library.  This example fetches a [http://www.realultimatepower.net/ninja/kidgarbage.jpg fine image] of a young man demonstrating his martial arts skills in an unlikely environment.  It then applies a convolution to the image, and adds a much-needed embellishment to the scene and displays the [http://www.megacz.com/caucho/images/kidgarbage-after.png resulting image].
+
Here's a short example of how to use the Quercus image library.  This example fetches a [http://www.realultimatepower.net/ninja/kidgarbage.jpg 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 [http://www.megacz.com/caucho/images/kidgarbage-resized.png 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 <em>always</em> have access to <em>all</em> of the functionality <em>all</em> of the time -- there is no compile-time configuration to worry about.
 
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 <em>always</em> have access to <em>all</em> of the functionality <em>all</em> of the time -- there is no compile-time configuration to worry about.
Line 6: Line 6:
  
 
   <?php
 
   <?php
 
+
   
   $image = imagecreatetruecolor(576, 480);
+
   $image = imagecreatetruecolor(270, 240);
 
    
 
    
 
   # fetch the image of a young man with REAL ULTIMATE POWER
 
   # fetch the image of a young man with REAL ULTIMATE POWER
   $picture = imagecreatefromjpeg("ht tp://www.realultimatepower.net/ninja/kidgarbage.jpg");
+
   $picture = imagecreatefromjpeg("h ttp://www.realultimatepower.net/ninja/kidgarbage.jpg");
   imagecopy($image, $picture, 0, 0, 0, 0, 576, 480);
+
   imagecopyresized($image, $picture, 0, 0, 0, 0, 270, 240, 576, 480);
 
    
 
    
 
   # apply a convolution
 
   # apply a convolution
Line 24: Line 24:
 
   $red = imagecolorallocate($image, 255, 0, 0);
 
   $red = imagecolorallocate($image, 255, 0, 0);
 
   imagesetthickness($image, 3);
 
   imagesetthickness($image, 3);
   imageline($image, 334, 161, 0, 300, $red);
+
   imageline($image, 158, 81, 0, 300, $red);
   imageline($image, 351, 159, 0, 300, $red);
+
   imageline($image, 165, 81, 0, 300, $red);
 
    
 
    
 
   # set the output type and send the image to the client.
 
   # set the output type and send the image to the client.
Line 32: Line 32:
 
   ?>
 
   ?>
  
Note that you'll need Resin 3.0.20 or the 2006/06/02 snapshot in order to run this properly.
+
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]

Latest revision as of 06:46, 13 June 2006

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