Source for file functions.inc.php

Documentation is available at functions.inc.php

  1. <?php
  2. /**
  3.  * Aptana Cloud Sample Application - Image Manipulation Demo Functions
  4.  * 
  5.  * This file contains all the function definitions for the sample application.
  6.  * 
  7.  * @author Ian Selby
  8.  * @version 1.0
  9.  * @license http://www.opensource.org/licenses/mit-license.php MIT License
  10.  * @package ImageManipulationDemo
  11.  * @filesource
  12.  */
  13.  
  14. /**
  15.  * The document site root
  16.  */
  17. define('SITE_ROOT'dirname(__FILE__'/');
  18.  
  19. /**
  20.  * Valid file extensions for uploaded images
  21.  *
  22.  * @global array $GLOBALS['valid_files'] 
  23.  * @name $valid_files
  24.  */
  25. $GLOBALS['valid_files'array('jpg''jpeg''gif''png');
  26.  
  27.  
  28. /**
  29.  * Uploads an Image to the Uploads Folder
  30.  *
  31.  * This function is pretty straight-forward.  It does a few simple things:
  32.  *
  33.  * - Attempts to chmod the uploads directory to make sure it is writeable
  34.  * - Sanitizes the uploaded file name
  35.  * - Uploads the file
  36.  * - Makes sure that the file is a valid image type
  37.  *
  38.  * It will return an array with the following fields:
  39.  *
  40.  * - filename: the sanitized name of the uploaded file
  41.  * - success: true / false
  42.  * - error: the reason the upload failed (if success == false)
  43.  * 
  44.  * @param string $key The form uploader element to use for the upload
  45.  * @return array The result of the upload
  46.  */
  47. function uploadImage($key)
  48. {
  49.     $return_array array();
  50.  
  51.     // make sure the uploads directory is writeable
  52.     @chmod(SITE_ROOT 'uploads'0777);
  53.     
  54.     // clean up the filename
  55.     $original_file basename($_FILES[$key]['name']);
  56.     $new_file preg_replace('/[^a-zA-Z0-9._-]/'''$original_file);
  57.     // where we'll put the new file
  58.     $target SITE_ROOT 'uploads/' $new_file;
  59.     
  60.     // try to upload the file
  61.     if(!move_uploaded_file($_FILES[$key]['tmp_name']$target))
  62.     {
  63.         $return_array['success'false;
  64.         $return_array['filename'$new_file;
  65.         $return_array['error''Could not upload file';
  66.         
  67.         return $return_array;
  68.     }
  69.     
  70.     // assume everything is ok
  71.     $return_array['success'true;
  72.     $return_array['filename'$new_file;
  73.     $return_array['error''';
  74.     
  75.     // get the extension, and make sure it's valid
  76.     $extension getFileExtension($target);
  77.     
  78.     // granted, it would be wiser to check the actual file header to verify it's an image,
  79.     // but this is a sample so this should suffice :)
  80.     if(!in_array($extension$GLOBALS['valid_files']))
  81.     {
  82.         $return_array['success'false;
  83.         $return_array['error''The uploaded file type is not valid';
  84.     }
  85.         
  86.     return $return_array;
  87. }
  88.  
  89. /**
  90.  * Generates a "Polaroid" of an Image
  91.  *
  92.  * This function will take an image file, scale it down to no wider than
  93.  * 450 pixels, rotate it randomly, and then apply a "polaroid" type effect
  94.  * to it with a drop-shadow.  It then returns the manipulated file name.
  95.  *
  96.  * Because of the effects applied during the manipulation, the returned file is
  97.  * always a PNG image (for transparency support).
  98.  *
  99.  * @param string $filename Full filesystem path to the image to manipulate
  100.  * @return string The name of the resultant file
  101.  */
  102. function generatePolaroidImage($filename)
  103. {
  104.     // build the new filename
  105.     $extension getFileExtension($filename);
  106.     $new_file str_ireplace($extension'png'$filename);
  107.  
  108.     // do all the magic ;)
  109.     $bg new ImagickDraw();
  110.     $image new Imagick($filename);
  111.     $image->setImageFormat('png');
  112.     $image->thumbnailImage(450null);
  113.     $image->setImageBackgroundColor(new ImagickPixel('black'));
  114.     
  115.     // come up with a random angle between 1 and 45 degrees
  116.     $angle mt_rand145 );
  117.     if mt_rand1=== 
  118.     {   
  119.         $angle $angle * -1
  120.     }   
  121.  
  122.     $image->polaroidImage($bg$angle);
  123.     $image->writeImage($new_file);
  124.     
  125.     return basename($new_file);
  126. }
  127.  
  128. /**
  129.  * Returns the Extension of a File
  130.  *
  131.  * @param string $filename The full path to the file to get the extension of
  132.  * @return string The extension of the file
  133.  */
  134. function getFileExtension($filename)
  135. {
  136.     $path_info pathinfo($filename);
  137.     return strtolower($path_info['extension']);
  138. }
  139.  
  140. ?>

Documentation generated on Tue, 07 Oct 2008 15:23:02 -0700 by phpDocumentor 1.4.2