Source for file class.upload.php 0.25

Documentation is available at class.upload.php

Webpage is available at http://www.verot.net/php_class_upload.htm

  1. <?php
  2. // +------------------------------------------------------------------------+
  3. // | class.upload.php                                                       |
  4. // +------------------------------------------------------------------------+
  5. // | Copyright (c) Colin Verot 2003-2007. All rights reserved.              |
  6. // | Version       0.25                                                     |
  7. // | Last modified 17/11/2007                                               |
  8. // | Email         colin@verot.net                                          |
  9. // | Web           http://www.verot.net                                     |
  10. // +------------------------------------------------------------------------+
  11. // | This program is free software; you can redistribute it and/or modify   |
  12. // | it under the terms of the GNU General Public License version 2 as      |
  13. // | published by the Free Software Foundation.                             |
  14. // |                                                                        |
  15. // | This program is distributed in the hope that it will be useful,        |
  16. // | but WITHOUT ANY WARRANTY; without even the implied warranty of         |
  17. // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          |
  18. // | GNU General Public License for more details.                           |
  19. // |                                                                        |
  20. // | You should have received a copy of the GNU General Public License      |
  21. // | along with this program; if not, write to the                          |
  22. // |   Free Software Foundation, Inc., 59 Temple Place, Suite 330,          |
  23. // |   Boston, MA 02111-1307 USA                                            |
  24. // |                                                                        |
  25. // | Please give credit on sites that use class.upload and submit changes   |
  26. // | of the script so other people can use them as well.                    |
  27. // | This script is free to use, don't abuse.                               |
  28. // +------------------------------------------------------------------------+
  29. //
  30.  
  31. /**
  32.  * Class upload
  33.  *
  34.  * @version   0.25
  35.  * @author    Colin Verot <colin@verot.net>
  36.  * @license   http://opensource.org/licenses/gpl-license.php GNU Public License
  37.  * @copyright Colin Verot
  38.  * @package   cmf
  39.  * @subpackage external
  40.  */
  41.  
  42. /**
  43.  * Class upload
  44.  *
  45.  * <b>What does it do?</b>
  46.  *
  47.  * It manages file uploads for you. In short, it manages the uploaded file,
  48.  * and allows you to do whatever you want with the file, especially if it
  49.  * is an image, and as many times as you want.
  50.  *
  51.  * It is the ideal class to quickly integrate file upload in your site.
  52.  * If the file is an image, you can convert, resize, crop it in many ways.
  53.  * You can also apply filters, add borders, text, watermarks, etc...
  54.  * That's all you need for a gallery script for instance. Supported formats
  55.  * are PNG, JPG, GIF and BMP.
  56.  *
  57.  * You can also use the class to work on local files, which is especially
  58.  * useful to use the image manipulation features.
  59.  *
  60.  * The class works with PHP 4 and 5, and its error messages can
  61.  * be localized at will.
  62.  *
  63.  * <b>How does it work?</b>
  64.  *
  65.  * You instanciate the class with the $_FILES['my_field'] array
  66.  * where my_field is the field name from your upload form.
  67.  * The class will check if the original file has been uploaded
  68.  * to its temporary location (alternatively, you can instanciate
  69.  * the class with a local filename).
  70.  *
  71.  * You can then set a number of processing variables to act on the file.
  72.  * For instance, you can rename the file, and if it is an image,
  73.  * convert and resize it in many ways.
  74.  * You can also set what will the class do if the file already exists.
  75.  *
  76.  * Then you call the function {@link process} to actually perform the actions
  77.  * according to the processing parameters you set above.
  78.  * It will create new instances of the original file,
  79.  * so the original file remains the same between each process.
  80.  * The file will be manipulated, and copied to the given location.
  81.  * The processing variables will be reseted once it is done.
  82.  *
  83.  * You can repeat setting up a new set of processing variables,
  84.  * and calling {@link process} again as many times as you want.
  85.  * When you have finished, you can call {@link clean} to delete
  86.  * the original uploaded file.
  87.  *
  88.  * If you don't set any processing parameters and call {@link process}
  89.  * just after instanciating the class. The uploaded file will be simply
  90.  * copied to the given location without any alteration or checks.
  91.  *
  92.  * Don't forget to add <i>enctype="multipart/form-data"</i> in your form
  93.  * tag <form> if you want your form to upload the file.
  94.  *
  95.  * <b>How to use it?</b><br>
  96.  * Create a simple HTML file, with a form such as:
  97.  * <pre>
  98.  * <form enctype="multipart/form-data" method="post" action="upload.php">
  99.  *   <input type="file" size="32" name="image_field" value="">
  100.  *   <input type="submit" name="Submit" value="upload">
  101.  * </form>
  102.  * </pre>
  103.  * Create a file called upload.php:
  104.  * <pre>
  105.  *  $handle = new upload($_FILES['image_field']);
  106.  *  if ($handle->uploaded) {
  107.  *      $handle->file_new_name_body   = 'image_resized';
  108.  *      $handle->image_resize         = true;
  109.  *      $handle->image_x              = 100;
  110.  *      $handle->image_ratio_y        = true;
  111.  *      $handle->process('/home/user/files/');
  112.  *      if ($handle->processed) {
  113.  *          echo 'image resized';
  114.  *          $handle->clean();
  115.  *      } else {
  116.  *          echo 'error : ' . $handle->error;
  117.  *      }
  118.  *  }
  119.  * </pre>
  120.  *
  121.  * <b>How to process local files?</b><br>
  122.  * Use the class as following, the rest being the same as above:
  123.  * <pre>
  124.  *  $handle = new upload('/home/user/myfile.jpg');
  125.  * </pre>
  126.  * 
  127.  * <b>How to set the language?</b><br>
  128.  * Instantiate the class with a second argument being the language code:
  129.  * <pre>
  130.  *  $handle = new upload($_FILES['image_field'], 'fr_FR');
  131.  *  $handle = new upload('/home/user/myfile.jpg', 'fr_FR');
  132.  * </pre>
  133.  * 
  134.  * <b>How to output the resulting file or picture directly to the browser?</b><br>
  135.  * Simply call {@link process}() without an argument (or with null as first argument):
  136.  * <pre>
  137.  *  $handle = new upload($_FILES['image_field']);
  138.  *  header('Content-type: ' . $handle->file_src_mime);
  139.  *  echo $handle->Process();
  140.  *  die();
  141.  * </pre>
  142.  * Or if you want to force the download of the file:
  143.  * <pre>
  144.  *  $handle = new upload($_FILES['image_field']);
  145.  *  header('Content-type: ' . $handle->file_src_mime);
  146.  *  header("Content-Disposition: attachment; filename=".rawurlencode($handle->file_src_name).";");
  147.  *  echo $handle->Process();
  148.  *  die();
  149.  * </pre>
  150.  *
  151.  * <b>Processing parameters</b> (reseted after each process)
  152.  * <ul>
  153.  *  <li><b>file_new_name_body</b> replaces the name body (default: '')<br>
  154.  *  <pre>$handle->file_new_name_body = 'new name';</pre></li>
  155.  *  <li><b>file_name_body_add</b> appends to the name body (default: '')<br>
  156.  *  <pre>$handle->file_name_body_add = '_uploaded';</pre></li>
  157.  *  <li><b>file_new_name_ext</b> replaces the file extension (default: '')<br>
  158.  *  <pre>$handle->file_new_name_ext = 'txt';</pre></li>
  159.  *  <li><b>file_safe_name</b> formats the filename (spaces changed to _) (default: true)<br>
  160.  *  <pre>$handle->file_safe_name = true;</pre></li>
  161.  *  <li><b>file_overwrite</b> sets behaviour if file already exists (default: false)<br>
  162.  *  <pre>$handle->file_overwrite = true;</pre></li>
  163.  *  <li><b>file_auto_rename</b> automatically renames file if it already exists (default: true)<br>
  164.  *  <pre>$handle->file_auto_rename = true;</pre></li>
  165.  *  <li><b>auto_create_dir</b> automatically creates destination directory if missing (default: true)<br>
  166.  *  <pre>$handle->auto_create_dir = true;</pre></li>
  167.  *  <li><b>dir_auto_chmod</b> automatically attempts to chmod the destination directory if not writeable (default: true)<br>
  168.  *  <pre>$handle->dir_auto_chmod = true;</pre></li>
  169.  *  <li><b>dir_chmod</b> chmod used when creating directory or if directory not writeable (default: 0777)<br>
  170.  *  <pre>$handle->dir_chmod = 0777;</pre></li>
  171.  *  <li><b>file_max_size</b> sets maximum upload size (default: upload_max_filesize from php.ini)<br>
  172.  *  <pre>$handle->file_max_size = '1024'; // 1KB</pre></li>
  173.  *  <li><b>mime_check</b> sets if the class check the MIME against the {@link allowed} list (default: true)<br>
  174.  *  <pre>$handle->mime_check = false;</pre></li>
  175.  *  <li><b>mime_magic_check</b> sets if the class uses mime_magic (default: false)<br>
  176.  *  <pre>$handle->mime_magic_check = true;</pre></li>
  177.  *  <li><b>no_script</b> sets if the class turns scripts into text files (default: true)<br>
  178.  *  <pre>$handle->no_script = false;</pre></li>
  179.  *  <li><b>allowed</b> array of allowed mime-types. wildcard accepted, as in image/* (default: check {@link Init})<br>
  180.  *  <pre>$handle->allowed = array('application/pdf','application/msword', 'image/*');</pre></li>
  181.  *  <li><b>forbidden</b> array of forbidden mime-types. wildcard accepted, as in image/*  (default: check {@link Init})<br>
  182.  *  <pre>$handle->forbidden = array('application/*');</pre></li>
  183.  * </ul>
  184.  * <ul>
  185.  *  <li><b>image_convert</b> if set, image will be converted (possible values : ''|'png'|'jpeg'|'gif'|'bmp'; default: '')<br>
  186.  *  <pre>$handle->image_convert = 'jpg';</pre></li>
  187.  *  <li><b>image_background_color</b> if set, will forcibly fill transparent areas with the color, in hexadecimal (default: null)<br>
  188.  *  <pre>$handle->image_background_color = '#FF00FF';</pre></li>
  189.  *  <li><b>image_default_color</b> fallback color background color for non alpha-transparent output formats, such as JPEG or BMP, in hexadecimal (default: #FFFFFF)<br>
  190.  *  <pre>$handle->image_default_color = '#FF00FF';</pre></li>
  191.  *  <li><b>jpeg_quality</b> sets the compression quality for JPEG images (default: 85)<br>
  192.  *  <pre>$handle->jpeg_quality = 50;</pre></li>
  193.  *  <li><b>jpeg_size</b> if set to a size in bytes, will approximate {@link jpeg_quality} so the output image fits within the size (default: null)<br>
  194.  *  <pre>$handle->jpeg_size = 3072;</pre></li>
  195.  * </ul>
  196.  * The following eight settings can be used to invalidate an upload if the file is an image (note that <i>open_basedir</i> restrictions prevent the use of these settings)
  197.  * <ul>
  198.  *  <li><b>image_max_width</b> if set to a dimension in pixels, the upload will be invalid if the image width is greater (default: null)<br>
  199.  *  <pre>$handle->image_max_width = 200;</pre></li>
  200.  *  <li><b>image_max_height</b> if set to a dimension in pixels, the upload will be invalid if the image height is greater (default: null)<br>
  201.  *  <pre>$handle->image_max_height = 100;</pre></li>
  202.  *  <li><b>image_max_pixels</b> if set to a number of pixels, the upload will be invalid if the image number of pixels is greater (default: null)<br>
  203.  *  <pre>$handle->image_max_pixels = 50000;</pre></li>
  204.  *  <li><b>image_max_ratio</b> if set to a aspect ratio (width/height), the upload will be invalid if the image apect ratio is greater (default: null)<br>
  205.  *  <pre>$handle->image_max_ratio = 1.5;</pre></li>
  206.  *  <li><b>image_min_width</b> if set to a dimension in pixels, the upload will be invalid if the image width is lower (default: null)<br>
  207.  *  <pre>$handle->image_min_width = 100;</pre></li>
  208.  *  <li><b>image_min_height</b> if set to a dimension in pixels, the upload will be invalid if the image height is lower (default: null)<br>
  209.  *  <pre>$handle->image_min_height = 500;</pre></li>
  210.  *  <li><b>image_min_pixels</b> if set to a number of pixels, the upload will be invalid if the image number of pixels is lower (default: null)<br>
  211.  *  <pre>$handle->image_min_pixels = 20000;</pre></li>
  212.  *  <li><b>image_min_ratio</b> if set to a aspect ratio (width/height), the upload will be invalid if the image apect ratio is lower (default: null)<br>
  213.  *  <pre>$handle->image_min_ratio = 0.5;</pre></li>
  214.  * </ul>
  215.  * <ul>
  216.  *  <li><b>image_resize</b> determines is an image will be resized (default: false)<br>
  217.  *  <pre>$handle->image_resize = true;</pre></li>
  218.  * </ul>
  219.  *  The following variables are used only if {@link image_resize} == true
  220.  * <ul>
  221.  *  <li><b>image_x</b> destination image width (default: 150)<br>
  222.  *  <pre>$handle->image_x = 100;</pre></li>
  223.  *  <li><b>image_y</b> destination image height (default: 150)<br>
  224.  *  <pre>$handle->image_y = 200;</pre></li>
  225.  * </ul>
  226.  *  Use either one of the following
  227.  * <ul>
  228.  *  <li><b>image_ratio</b> if true, resize image conserving the original sizes ratio, using {@link image_x} AND {@link image_y} as max sizes if true (default: false)<br>
  229.  *  <pre>$handle->image_ratio = true;</pre></li>
  230.  *  <li><b>image_ratio_crop</b> if true, resize image conserving the original sizes ratio, using {@link image_x} AND {@link image_y} as max sizes, and cropping excedent to fill the space. setting can also be a string, with one or more from 'TBLR', indicating which side of the image will be kept while cropping (default: false)<br>
  231.  *  <pre>$handle->image_ratio_crop = true;</pre></li>
  232.  *  <li><b>image_ratio_fill</b> if true, resize image conserving the original sizes ratio, using {@link image_x} AND {@link image_y} as max sizes, fitting the image in the space and coloring the remaining space. setting can also be a string, with one or more from 'TBLR', indicating which side of the space the image will be in (default: false)<br>
  233.  *  <pre>$handle->image_ratio_fill = true;</pre></li>
  234.  *  <li><b>image_ratio_no_zoom_in</b> same as {@link image_ratio}, but won't resize if the source image is smaller than {@link image_x} x {@link image_y} (default: false)<br>
  235.  *  <pre>$handle->image_ratio_no_zoom_in = true;</pre></li>
  236.  *  <li><b>image_ratio_no_zoom_out</b> same as {@link image_ratio}, but won't resize if the source image is bigger than {@link image_x} x {@link image_y} (default: false)<br>
  237.  *  <pre>$handle->image_ratio_no_zoom_out = true;</pre></li>
  238.  *  <li><b>image_ratio_x</b> if true, resize image, calculating {@link image_x} from {@link image_y} and conserving the original sizes ratio (default: false)<br>
  239.  *  <pre>$handle->image_ratio_x = true;</pre></li>
  240.  *  <li><b>image_ratio_y</b> if true, resize image, calculating {@link image_y} from {@link image_x} and conserving the original sizes ratio (default: false)<br>
  241.  *  <pre>$handle->image_ratio_y = true;</pre></li>
  242.  *  <li><b>image_ratio_pixels</b> if set to a long integer, resize image, calculating {@link image_y} and {@link image_x} to match a the number of pixels (default: false)<br>
  243.  *  <pre>$handle->image_ratio_pixels = 25000;</pre></li>
  244.  * </ul>
  245.  *  The following image manipulations require GD2+
  246.  * <ul>
  247.  *  <li><b>image_brightness</b> if set, corrects the brightness. value between -127 and 127 (default: null)<br>
  248.  *  <pre>$handle->image_brightness = 40;</pre></li>
  249.  *  <li><b>image_contrast</b> if set, corrects the contrast. value between -127 and 127 (default: null)<br>
  250.  *  <pre>$handle->image_contrast = 50;</pre></li>
  251.  *  <li><b>image_tint_color</b> if set, will tint the image with a color, value as hexadecimal #FFFFFF (default: null)<br>
  252.  *  <pre>$handle->image_tint_color = '#FF0000';</pre></li>
  253.  *  <li><b>image_overlay_color</b> if set, will add a colored overlay, value as hexadecimal #FFFFFF (default: null)<br>
  254.  *  <pre>$handle->image_overlay_color = '#FF0000';</pre></li>
  255.  *  <li><b>image_overlay_percent</b> used when {@link image_overlay_color} is set, determines the opacity (default: 50)<br>
  256.  *  <pre>$handle->image_overlay_percent = 20;</pre></li>
  257.  *  <li><b>image_negative</b> inverts the colors in the image (default: false)<br>
  258.  *  <pre>$handle->image_negative = true;</pre></li>
  259.  *  <li><b>image_greyscale</b> transforms an image into greyscale (default: false)<br>
  260.  *  <pre>$handle->image_greyscale = true;</pre></li>
  261.  *  <li><b>image_threshold</b> applies a threshold filter. value between -127 and 127 (default: null)<br>
  262.  *  <pre>$handle->image_threshold = 20;</pre></li>
  263.  * </ul>
  264.  * <ul>
  265.  *  <li><b>image_text</b> creates a text label on the image, value is a string, with eventual replacement tokens (default: null)<br>
  266.  *  <pre>$handle->image_text = 'test';</pre></li>
  267.  *  <li><b>image_text_direction</b> text label direction, either 'h' horizontal or 'v' vertical (default: 'h')<br>
  268.  *  <pre>$handle->image_text_direction = 'v';</pre></li>
  269.  *  <li><b>image_text_color</b> text color for the text label, in hexadecimal (default: #FFFFFF)<br>
  270.  *  <pre>$handle->image_text_color = '#FF0000';</pre></li>
  271.  *  <li><b>image_text_percent</b> text opacity on the text label, integer between 0 and 100 (default: 100)<br>
  272.  *  <pre>$handle->image_text_percent = 50;</pre></li>
  273.  *  <li><b>image_text_background</b> text label background color, in hexadecimal (default: null)<br>
  274.  *  <pre>$handle->image_text_background = '#FFFFFF';</pre></li>
  275.  *  <li><b>image_text_background_percent</b> text label background opacity, integer between 0 and 100 (default: 100)<br>
  276.  *  <pre>$handle->image_text_background_percent = 50;</pre></li>
  277.  *  <li><b>image_text_font</b> built-in font for the text label, from 1 to 5. 1 is the smallest (default: 5)<br>
  278.  *  <pre>$handle->image_text_font = 4;</pre></li>
  279.  *  <li><b>image_text_x</b> absolute text label position, in pixels from the left border. can be negative (default: null)<br>
  280.  *  <pre>$handle->image_text_x = 5;</pre></li>
  281.  *  <li><b>image_text_y</b> absolute text label position, in pixels from the top border. can be negative (default: null)<br>
  282.  *  <pre>$handle->image_text_y = 5;</pre></li>
  283.  *  <li><b>image_text_position</b> text label position withing the image, a combination of one or two from 'TBLR': top, bottom, left, right (default: null)<br>
  284.  *  <pre>$handle->image_text_position = 'LR';</pre></li>
  285.  *  <li><b>image_text_padding</b> text label padding, in pixels. can be overridden by {@link image_text_padding_x} and {@link image_text_padding_y} (default: 0)<br>
  286.  *  <pre>$handle->image_text_padding = 5;</pre></li>
  287.  *  <li><b>image_text_padding_x</b> text label horizontal padding (default: null)<br>
  288.  *  <pre>$handle->image_text_padding_x = 2;</pre></li>
  289.  *  <li><b>image_text_padding_y</b> text label vertical padding (default: null)<br>
  290.  *  <pre>$handle->image_text_padding_y = 10;</pre></li>
  291.  *  <li><b>image_text_alignment</b> text alignment when text has multiple lines, either 'L', 'C' or 'R' (default: 'C')<br>
  292.  *  <pre>$handle->image_text_alignment = 'R';</pre></li>
  293.  *  <li><b>image_text_line_spacing</b> space between lines in pixels, when text has multiple lines (default: 0)<br>
  294.  *  <pre>$handle->image_text_line_spacing = 3;</pre></li>
  295.  * </ul>
  296.  * <ul>
  297.  *  <li><b>image_flip</b> flips image, wither 'h' horizontal or 'v' vertical (default: null)<br>
  298.  *  <pre>$handle->image_flip = 'h';</pre></li>
  299.  *  <li><b>image_rotate</b> rotates image. possible values are 90, 180 and 270 (default: null)<br>
  300.  *  <pre>$handle->image_rotate = 90;</pre></li>
  301.  *  <li><b>image_crop</b> crops image. accepts 4, 2 or 1 values as 'T R B L' or 'TB LR' or 'TBLR'. dimension can be 20, or 20px or 20% (default: null)<br>
  302.  *  <pre>$handle->image_crop = array(50,40,30,20); OR '-20 20%'...</pre></li>
  303.  * </ul>
  304.  * <ul>
  305.  *  <li><b>image_bevel</b> adds a bevel border to the image. value is thickness in pixels (default: null)<br>
  306.  *  <pre>$handle->image_bevel = 20;</pre></li>
  307.  *  <li><b>image_bevel_color1</b> top and left bevel color, in hexadecimal (default: #FFFFFF)<br>
  308.  *  <pre>$handle->image_bevel_color1 = '#FFFFFF';</pre></li>
  309.  *  <li><b>image_bevel_color2</b> bottom and right bevel color, in hexadecimal (default: #000000)<br>
  310.  *  <pre>$handle->image_bevel_color2 = '#000000';</pre></li>
  311.  *  <li><b>image_border</b> adds a unicolor border to the image. accepts 4, 2 or 1 values as 'T R B L' or 'TB LR' or 'TBLR'. dimension can be 20, or 20px or 20% (default: null)<br>
  312.  *  <pre>$handle->image_border = '3px'; OR '-20 20%' OR array(3,2)...</pre></li>
  313.  *  <li><b>image_border_color</b> border color, in hexadecimal (default: #FFFFFF)<br>
  314.  *  <pre>$handle->image_border_color = '#FFFFFF';</pre></li>
  315.  *  <li><b>image_frame</b> type of frame: 1=flat 2=crossed (default: null)<br>
  316.  *  <pre>$handle->image_frame = 2;</pre></li>
  317.  *  <li><b>image_frame_colors</b> list of hex colors, in an array or a space separated string (default: '#FFFFFF #999999 #666666 #000000')<br>
  318.  *  <pre>$handle->image_frame_colors = array('#999999',  '#FF0000', '#666666', '#333333', '#000000');</pre></li>
  319.  * </ul>
  320.  * <ul>
  321.  *  <li><b>image_watermark</b> adds a watermark on the image, value is a local filename. accepted files are GIF, JPG, BMP, PNG and PNG alpha (default: null)<br>
  322.  *  <pre>$handle->image_watermark = 'watermark.png';</pre></li>
  323.  *  <li><b>image_watermark_x</b> absolute watermark position, in pixels from the left border. can be negative (default: null)<br>
  324.  *  <pre>$handle->image_watermark_x = 5;</pre></li>
  325.  *  <li><b>image_watermark_y</b> absolute watermark position, in pixels from the top border. can be negative (default: null)<br>
  326.  *  <pre>$handle->image_watermark_y = 5;</pre></li>
  327.  *  <li><b>image_watermark_position</b> watermark position withing the image, a combination of one or two from 'TBLR': top, bottom, left, right (default: null)<br>
  328.  *  <pre>$handle->image_watermark_position = 'LR';</pre></li>
  329.  * </ul>
  330.  * <ul>
  331.  *  <li><b>image_reflection_height</b> if set, a reflection will be added. Format is either in pixels or percentage, such as 40, '40', '40px' or '40%' (default: null)<br>
  332.  *  <pre>$handle->image_reflection_height = '25%';</pre></li>
  333.  *  <li><b>image_reflection_space</b> space in pixels between the source image and the reflection, can be negative (default: null)<br>
  334.  *  <pre>$handle->image_reflection_space = 3;</pre></li>
  335.  *  <li><b>image_reflection_color</b> reflection background color, in hexadecimal. Now deprecated in favor of {@link image_default_color} (default: #FFFFFF)<br>
  336.  *  <pre>$handle->image_default_color = '#000000';</pre></li>
  337.  *  <li><b>image_reflection_opacity</b> opacity level at which the reflection starts, integer between 0 and 100 (default: 60)<br>
  338.  *  <pre>$handle->image_reflection_opacity = 60;</pre></li>
  339.  * </ul>
  340.  *
  341.  * <b>Values that can be read before calling {@link process}()</b>
  342.  * <ul>
  343.  *  <li><b>file_src_name</b> Source file name</li>
  344.  *  <li><b>file_src_name_body</b> Source file name body</li>
  345.  *  <li><b>file_src_name_ext</b> Source file extension</li>
  346.  *  <li><b>file_src_pathname</b> Source file complete path and name</li>
  347.  *  <li><b>file_src_mime</b> Source file mime type</li>
  348.  *  <li><b>file_src_size</b> Source file size in bytes</li>
  349.  *  <li><b>file_src_error</b> Upload error code</li>
  350.  *  <li><b>file_is_image</b> Boolean flag, true if the file is a supported image type</li>
  351.  * </ul>
  352.  * If the file is a supported image type (and <i>open_basedir</i> restrictions allow it)
  353.  * <ul>
  354.  *  <li><b>image_src_x</b> Source file width in pixels</li>
  355.  *  <li><b>image_src_y</b> Source file height in pixels</li>
  356.  *  <li><b>image_src_pixels</b> Source file number of pixels</li>
  357.  *  <li><b>image_src_type</b> Source file type (png, jpg, gif or bmp)</li>
  358.  *  <li><b>image_src_bits</b> Source file color depth</li>
  359.  * </ul>
  360.  *
  361.  * <b>Values that can be read before after {@link process}()</b>
  362.  * <ul>
  363.  *  <li><b>file_dst_path</b> Destination file path</li>
  364.  *  <li><b>file_dst_name_body</b> Destination file name body</li>
  365.  *  <li><b>file_dst_name_ext</b> Destination file extension</li>
  366.  *  <li><b>file_dst_name</b> Destination file name</li>
  367.  *  <li><b>file_dst_pathname</b> Destination file complete path and name</li>
  368.  * </ul>
  369.  * If the file is a supported image type
  370.  * <ul>
  371.  *  <li><b>image_dst_x</b> Destination file width</li>
  372.  *  <li><b>image_dst_y</b> Destination file height</li>
  373.  *  <li><b>image_convert</b> Destination file format</li>
  374.  * </ul>
  375.  *
  376.  * <b>Requirements</b>
  377.  *
  378.  * Most of the image operations require GD. GD2 is greatly recommended
  379.  *
  380.  * The class is compatible with PHP 4.3+, and compatible with PHP5
  381.  *
  382.  * <b>Changelog</b>
  383.  * <ul>
  384.  *  <li><b>v 0.25</b> 17/11/2007<br>
  385.  *   - added translation files and mechanism to instantiate the class with a language different from English<br>
  386.  *   - added {@link forbidden} to set an array of forbidden MIME types<br>
  387.  *   - implemented support for simple wildcards in {@link allowed} and {@link forbidden}, such as image/*<br>
  388.  *   - preset the file extension to the desired conversion format when converting an image<br>
  389.  *   - added read and write support for BMP images<br>
  390.  *   - added a flag {@link file_is_image} to determine if the file is a supported image type<br>
  391.  *   - the class now provides some information about the image, before calling {@link process}(). Available are {@link image_src_x}{@link image_src_y} and the newly introduced {@link image_src_bits}{@link image_src_pixels} and {@link image_src_type}. Note that this will not work if <i>open_basedir</i> restrictions are in place<br>
  392.  *   - improved logging; now provides useful system information<br>
  393.  *   - added some more pre-processing checks for files that are images: {@link image_max_width}{@link image_max_height}{@link image_max_pixels}{@link image_max_ratio}{@link image_min_width}{@link image_min_height}{@link image_min_pixels} and {@link image_min_ratio}<br>
  394.  *   - added {@link image_ratio_pixels} to resize an image to a number of pixels, keeping aspect ratio<br>
  395.  *   - added {@link image_is_palette} and {@link image_is_transparent} and {@link image_transparent_color} for GIF images<br>
  396.  *   - added {@link image_default_color} to define a fallback color for non alpha-transparent output formats, such as JPEG or BMP<br>
  397.  *   - changed {@link image_background_color}, which now forces transparent areas to be painted<br>
  398.  *   - improved reflections and color overlays so that it works with alpha transparent images<br>
  399.  *   - {@link image_reflection_color} is now deprecated in favour of {@link image_default_color}<br />
  400.  *   - transparent PNGs are now processed in true color, and fully preserving the alpha channel when doing merges<br>
  401.  *   - transparent GIFs are now automatically detected. {@link preserve_transparency} is deprecated<br>
  402.  *   - transparent true color images can be saved as GIF while retaining transparency, semi transparent areas being merged with {@link image_default_color}<br>
  403.  *   - transparent true color images can be saved as JPG/BMP with the semi transparent areas being merged with {@link image_default_color}<br>
  404.  *   - fixed conversion of images to true color<br>
  405.  *   - the class can now output the uploaded files content as the return value of process() if the function is called with an empty or null argumenti, or no argument</li>
  406.  *  <li><b>v 0.24</b> 25/05/2007<br>
  407.  *   - added {@link image_background_color}, to set the default background color of an image<br>
  408.  *   - added possibility of using replacement tokens in text labels<br>
  409.  *   - changed default JPEG quality to 85<br>
  410.  *   - fixed a small bug when using greyscale filter and associated filters<br>
  411.  *   - added {@link image_ratio_fill} in order to fit an image within some dimensions and color the remaining space. Very similar to {@link image_ratio_crop}<br>
  412.  *   - improved the recursive creation of directories<br>
  413.  *   - the class now converts palette based images to true colors before doing graphic manipulations</li>
  414.  *  <li><b>v 0.23</b> 23/12/2006<br>
  415.  *   - fixed a bug when processing more than once the same uploaded file. If there is an open_basedir restriction, the class now creates a temporary file for the first call to process(). This file will be used for subsequent processes, and will be deleted upon calling clean()</li>
  416.  *  <li><b>v 0.22</b> 16/12/2006<br>
  417.  *   - added automatic creation of a temporary file if the upload directory is not within open_basedir<br>
  418.  *   - fixed a bug which was preventing to work on a local file by overwriting it with its processed copy<br>
  419.  *   - added MIME types video/x-ms-wmv and image/x-png and fixed PNG support for IE weird MIME types<br>
  420.  *   - modified {@link image_ratio_crop} so it can accept one or more from string 'TBLR', determining which side of the image is kept while cropping<br>
  421.  *   - added support for multiple lines in the text, using "\n" as a line break<br>
  422.  *   - added {@link image_text_line_spacing} which allow to set the space between several lines of text<br>
  423.  *   - added {@link image_text_alignment} which allow to set the alignment when text has several lines<br>
  424.  *   - {@link image_text_font} can now be set to the path of a GDF font to load external fonts<br>
  425.  *   - added {@link image_reflection_height} to create a reflection of the source image, which height is in pixels or percentage<br>
  426.  *   - added {@link image_reflection_space} to set the space in pixels between the source image and the reflection<br>
  427.  *   - added {@link image_reflection_color} to set the reflection background color<br>
  428.  *   - added {@link image_reflection_opacity} to set the initial level of opacity of the reflection</li>
  429.  *  <li><b>v 0.21</b> 30/09/2006<br>
  430.  *   - added {@link image_ratio_crop} which resizes within {@link image_x} and {@link image_y}, keeping ratio, but filling the space by cropping excedent of image<br>
  431.  *   - added {@link mime_check}, which default is true, to set checks against {@link allowed} MIME list<br>
  432.  *   - if MIME is empty, the class now triggers an error<br>
  433.  *   - color #000000 is OK for {@link image_text_color}, and related text transparency bug fixed<br>
  434.  *   - {@link gd_version}() now uses gd_info(), or else phpinfo()<br>
  435.  *   - fixed path issue when the destination path has no trailing slash on Windows systems <br>
  436.  *   - removed inline functions to be fully PHP5 compatible </li>
  437.  *  <li><b>v 0.20</b> 11/08/2006<br>
  438.  *   - added some more error checking and messages (GD presence, permissions...)<br>
  439.  *   - fix when uploading files without extension<br>
  440.  *   - changed values for {@link image_brightness} and {@link image_contrast} to be between -127 and 127<br>
  441.  *   - added {@link dir_auto_create} to automatically and recursively create destination directory if missing.<br>
  442.  *   - added {@link dir_auto_chmod} to automatically chmod the destination directory if not writeable.<br>
  443.  *   - added {@link dir_chmod} to set the default chmod to use.<br>
  444.  *   - added {@link image_crop} to crop images<br>
  445.  *   - added {@link image_negative} to invert the colors on the image<br>
  446.  *   - added {@link image_greyscale} to turn the image into greyscale<br>
  447.  *   - added {@link image_threshold} to apply a threshold filter on the image<br>
  448.  *   - added {@link image_bevel}{@link image_bevel_color1} and {@link image_bevel_color2} to add a bevel border<br>
  449.  *   - added {@link image_border} and {@link image_border_color} to add a single color border<br>
  450.  *   - added {@link image_frame} and {@link image_frame_colors} to add a multicolored frame</li>
  451.  *  <li><b>v 0.19</b> 29/03/2006<br>
  452.  *   - class is now compatible i18n (thanks Sylwester).<br>
  453.  *   - the class can mow manipulate local files, not only uploaded files (instanciate the class with a local filename).<br>
  454.  *   - {@link file_safe_name} has been improved a bit.<br>
  455.  *   - added {@link image_brightness}{@link image_contrast}{@link image_tint_color}{@link image_overlay_color} and {@link image_overlay_percent} to do color manipulation on the images.<br>
  456.  *   - added {@link image_text} and all derivated settings to add a text label on the image.<br>
  457.  *   - added {@link image_watermark} and all derivated settings to add a watermark image on the image.<br>
  458.  *   - added {@link image_flip} and {@link image_rotate} for more image manipulations<br>
  459.  *   - added {@link jpeg_size} to calculate the JPG compression quality in order to fit within one filesize.</li>
  460.  *  <li><b>v 0.18</b> 02/02/2006<br>
  461.  *   - added {@link no_script} to turn dangerous scripts into text files.<br>
  462.  *   - added {@link mime_magic_check} to set the class to use mime_magic.<br>
  463.  *   - added {@link preserve_transparency} *experimental*. Thanks Gregor.<br>
  464.  *   - fixed size and mime checking, wasn't working :/ Thanks Willem.<br>
  465.  *   - fixed memory leak when resizing images.<br>
  466.  *   - when resizing, it is not necessary anymore to set {@link image_convert}.<br>
  467.  *   - il is now possible to simply convert an image, with no resizing.<br>
  468.  *   - sets the default {@link file_max_size} to upload_max_filesize from php.ini. Thanks Edward</li>
  469.  *  <li><b>v 0.17</b> 28/05/2005<br>
  470.  *   - the class can be used with any version of GD.<br>
  471.  *   - added security check on the file with a list of mime-types.<br>
  472.  *   - changed the license to GPL v2 only</li>
  473.  *  <li><b>v 0.16</b> 19/05/2005<br>
  474.  *   - added {@link file_auto_rename} automatic file renaming if the same filename already exists.<br>
  475.  *   - added {@link file_safe_name} safe formatting of the filename (spaces to _underscores so far).<br>
  476.  *   - added some more error reporting to avoid crash if GD is not present</li>
  477.  *  <li><b>v 0.15</b> 16/04/2005<br>
  478.  *   - added JPEG compression quality setting. Thanks Vad</li>
  479.  *  <li><b>v 0.14</b> 14/03/2005<br>
  480.  *   - reworked the class file to allow parsing with phpDocumentor</li>
  481.  *  <li><b>v 0.13</b> 07/03/2005<br>
  482.  *   - fixed a bug with {@link image_ratio}. Thanks Justin.<br>
  483.  *   - added {@link image_ratio_no_zoom_in} and {@link image_ratio_no_zoom_out}</li>
  484.  *  <li><b>v 0.12</b> 21/01/2005<br>
  485.  *   - added {@link image_ratio} to resize within max values, keeping image ratio</li>
  486.  *  <li><b>v 0.11</b> 22/08/2003<br>
  487.  *   - update for GD2 (changed imageresized() into imagecopyresampled() and imagecreate() into imagecreatetruecolor())</li>
  488.  * </ul>
  489.  *
  490.  * @package   cmf
  491.  * @subpackage external
  492.  */
  493. class upload {
  494.  
  495.     
  496.     /**
  497.      * Uploaded file name
  498.      *
  499.      * @access public
  500.      * @var string 
  501.      */
  502.     var $file_src_name;
  503.  
  504.     /**
  505.      * Uploaded file name body (i.e. without extension)
  506.      *
  507.      * @access public
  508.      * @var string 
  509.      */
  510.     var $file_src_name_body;
  511.  
  512.     /**
  513.      * Uploaded file name extension
  514.      *
  515.      * @access public
  516.      * @var string 
  517.      */
  518.     var $file_src_name_ext;
  519.  
  520.     /**
  521.      * Uploaded file MIME type
  522.      *
  523.      * @access public
  524.      * @var string 
  525.      */
  526.     var $file_src_mime;
  527.  
  528.     /**
  529.      * Uploaded file size, in bytes
  530.      *
  531.      * @access public
  532.      * @var double 
  533.      */
  534.     var $file_src_size;
  535.  
  536.     /**
  537.      * Holds eventual PHP error code from $_FILES
  538.      *
  539.      * @access public
  540.      * @var string 
  541.      */
  542.     var $file_src_error;
  543.  
  544.     /**
  545.      * Uloaded file name, including server path
  546.      *
  547.      * @access private
  548.      * @var string 
  549.      */
  550.     var $file_src_pathname;
  551.  
  552.     /**
  553.      * Uloaded file name temporary copy
  554.      *
  555.      * @access private
  556.      * @var string 
  557.      */
  558.     var $file_src_temp;
  559.  
  560.     /**
  561.      * Destination file name
  562.      *
  563.      * @access private
  564.      * @var string 
  565.      */
  566.     var $file_dst_path;
  567.  
  568.     /**
  569.      * Destination file name
  570.      *
  571.      * @access public
  572.      * @var string 
  573.      */
  574.     var $file_dst_name;
  575.  
  576.     /**
  577.      * Destination file name body (i.e. without extension)
  578.      *
  579.     &