Source for file class.upload.php 0.28

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-2009. All rights reserved.              |
  6. // | Version       0.28                                                     |
  7. // | Last modified 10/08/2009                                               |
  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.28
  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. The class also supports
  59.  * Flash uploaders.
  60.  *
  61.  * The class works with PHP 4 and 5, and its error messages can
  62.  * be localized at will.
  63.  *
  64.  * <b>How does it work?</b>
  65.  *
  66.  * You instanciate the class with the $_FILES['my_field'] array
  67.  * where my_field is the field name from your upload form.
  68.  * The class will check if the original file has been uploaded
  69.  * to its temporary location (alternatively, you can instanciate
  70.  * the class with a local filename).
  71.  *
  72.  * You can then set a number of processing variables to act on the file.
  73.  * For instance, you can rename the file, and if it is an image,
  74.  * convert and resize it in many ways.
  75.  * You can also set what will the class do if the file already exists.
  76.  *
  77.  * Then you call the function {@link process} to actually perform the actions
  78.  * according to the processing parameters you set above.
  79.  * It will create new instances of the original file,
  80.  * so the original file remains the same between each process.
  81.  * The file will be manipulated, and copied to the given location.
  82.  * The processing variables will be reset once it is done.
  83.  *
  84.  * You can repeat setting up a new set of processing variables,
  85.  * and calling {@link process} again as many times as you want.
  86.  * When you have finished, you can call {@link clean} to delete
  87.  * the original uploaded file.
  88.  *
  89.  * If you don't set any processing parameters and call {@link process}
  90.  * just after instanciating the class. The uploaded file will be simply
  91.  * copied to the given location without any alteration or checks.
  92.  *
  93.  * Don't forget to add <i>enctype="multipart/form-data"</i> in your form
  94.  * tag <form> if you want your form to upload the file.
  95.  *
  96.  * <b>How to use it?</b><br>
  97.  * Create a simple HTML file, with a form such as:
  98.  * <pre>
  99.  * <form enctype="multipart/form-data" method="post" action="upload.php">
  100.  *   <input type="file" size="32" name="image_field" value="">
  101.  *   <input type="submit" name="Submit" value="upload">
  102.  * </form>
  103.  * </pre>
  104.  * Create a file called upload.php:
  105.  * <pre>
  106.  *  $handle = new upload($_FILES['image_field']);
  107.  *  if ($handle->uploaded) {
  108.  *      $handle->file_new_name_body   = 'image_resized';
  109.  *      $handle->image_resize         = true;
  110.  *      $handle->image_x              = 100;
  111.  *      $handle->image_ratio_y        = true;
  112.  *      $handle->process('/home/user/files/');
  113.  *      if ($handle->processed) {
  114.  *          echo 'image resized';
  115.  *          $handle->clean();
  116.  *      } else {
  117.  *          echo 'error : ' . $handle->error;
  118.  *      }
  119.  *  }
  120.  * </pre>
  121.  *
  122.  * <b>How to process local files?</b><br>
  123.  * Use the class as following, the rest being the same as above:
  124.  * <pre>
  125.  *  $handle = new upload('/home/user/myfile.jpg');
  126.  * </pre>
  127.  *
  128.  * <b>How to set the language?</b><br>
  129.  * Instantiate the class with a second argument being the language code:
  130.  * <pre>
  131.  *  $handle = new upload($_FILES['image_field'], 'fr_FR');
  132.  *  $handle = new upload('/home/user/myfile.jpg', 'fr_FR');
  133.  * </pre>
  134.  *
  135.  * <b>How to output the resulting file or picture directly to the browser?</b><br>
  136.  * Simply call {@link process}() without an argument (or with null as first argument):
  137.  * <pre>
  138.  *  $handle = new upload($_FILES['image_field']);
  139.  *  header('Content-type: ' . $handle->file_src_mime);
  140.  *  echo $handle->Process();
  141.  *  die();
  142.  * </pre>
  143.  * Or if you want to force the download of the file:
  144.  * <pre>
  145.  *  $handle = new upload($_FILES['image_field']);
  146.  *  header('Content-type: ' . $handle->file_src_mime);
  147.  *  header("Content-Disposition: attachment; filename=".rawurlencode($handle->file_src_name).";");
  148.  *  echo $handle->Process();
  149.  *  die();
  150.  * </pre>
  151.  *
  152.  * <b>Processing parameters</b> (reset after each process)
  153.  * <ul>
  154.  *  <li><b>file_new_name_body</b> replaces the name body (default: '')<br>
  155.  *  <pre>$handle->file_new_name_body = 'new name';</pre></li>
  156.  *  <li><b>file_name_body_add</b> appends to the name body (default: '')<br>
  157.  *  <pre>$handle->file_name_body_add = '_uploaded';</pre></li>
  158.  *  <li><b>file_name_body_pre</b> prepends to the name body (default: '')<br>
  159.  *  <pre>$handle->file_name_body_pre = 'thumb_';</pre></li>
  160.  *  <li><b>file_new_name_ext</b> replaces the file extension (default: '')<br>
  161.  *  <pre>$handle->file_new_name_ext = 'txt';</pre></li>
  162.  *  <li><b>file_safe_name</b> formats the filename (spaces changed to _) (default: true)<br>
  163.  *  <pre>$handle->file_safe_name = true;</pre></li>
  164.  *  <li><b>file_overwrite</b> sets behaviour if file already exists (default: false)<br>
  165.  *  <pre>$handle->file_overwrite = true;</pre></li>
  166.  *  <li><b>file_auto_rename</b> automatically renames file if it already exists (default: true)<br>
  167.  *  <pre>$handle->file_auto_rename = true;</pre></li>
  168.  *  <li><b>auto_create_dir</b> automatically creates destination directory if missing (default: true)<br>
  169.  *  <pre>$handle->auto_create_dir = true;</pre></li>
  170.  *  <li><b>dir_auto_chmod</b> automatically attempts to chmod the destination directory if not writeable (default: true)<br>
  171.  *  <pre>$handle->dir_auto_chmod = true;</pre></li>
  172.  *  <li><b>dir_chmod</b> chmod used when creating directory or if directory not writeable (default: 0777)<br>
  173.  *  <pre>$handle->dir_chmod = 0777;</pre></li>
  174.  *  <li><b>file_max_size</b> sets maximum upload size (default: upload_max_filesize from php.ini)<br>
  175.  *  <pre>$handle->file_max_size = '1024'; // 1KB</pre></li>
  176.  *  <li><b>mime_check</b> sets if the class check the MIME against the {@link allowed} list (default: true)<br>
  177.  *  <pre>$handle->mime_check = true;</pre></li>
  178.  *  <li><b>mime_fileinfo</b> activates MIME detection with Fileinfo PECL extension if true (or a string to set the path to the magic database file); false to deactivate (default: true)<br>
  179.  *  <pre>$handle->mime_fileinfo = '/usr/share/file/magic';</pre></li>
  180.  *  <li><b>mime_file</b> activates MIME detection with UNIX file() command if true; false to deactivate (default: true)<br>
  181.  *  <pre>$handle->mime_file = false;</pre></li>
  182.  *  <li><b>mime_magic</b> activates MIME detection with mime_magic (mime_content_type()) if true; false to deactivate (default: true)<br>
  183.  *  <pre>$handle->mime_magic = false;</pre></li>
  184.  *  <li><b>mime_getimagesize</b> activates MIME detection with getimagesize() if true; false to deactivate (default: true)<br>
  185.  *  <pre>$handle->mime_getimagesize = false;</pre></li>
  186.  *  <li><b>no_script</b> sets if the class turns scripts into text files (default: true)<br>
  187.  *  <pre>$handle->no_script = false;</pre></li>
  188.  *  <li><b>allowed</b> array of allowed mime-types. wildcard accepted, as in image/* (default: check {@link Init})<br>
  189.  *  <pre>$handle->allowed = array('application/pdf','application/msword', 'image/*');</pre></li>
  190.  *  <li><b>forbidden</b> array of forbidden mime-types. wildcard accepted, as in image/*  (default: check {@link Init})<br>
  191.  *  <pre>$handle->forbidden = array('application/*');</pre></li>
  192.  * </ul>
  193.  * <ul>
  194.  *  <li><b>image_convert</b> if set, image will be converted (possible values : ''|'png'|'jpeg'|'gif'|'bmp'; default: '')<br>
  195.  *  <pre>$handle->image_convert = 'jpg';</pre></li>
  196.  *  <li><b>image_background_color</b> if set, will forcibly fill transparent areas with the color, in hexadecimal (default: null)<br>
  197.  *  <pre>$handle->image_background_color = '#FF00FF';</pre></li>
  198.  *  <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>
  199.  *  <pre>$handle->image_default_color = '#FF00FF';</pre></li>
  200.  *  <li><b>jpeg_quality</b> sets the compression quality for JPEG images (default: 85)<br>
  201.  *  <pre>$handle->jpeg_quality = 50;</pre></li>
  202.  *  <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>
  203.  *  <pre>$handle->jpeg_size = 3072;</pre></li>
  204.  * </ul>
  205.  * 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)
  206.  * <ul>
  207.  *  <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>
  208.  *  <pre>$handle->image_max_width = 200;</pre></li>
  209.  *  <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>
  210.  *  <pre>$handle->image_max_height = 100;</pre></li>
  211.  *  <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>
  212.  *  <pre>$handle->image_max_pixels = 50000;</pre></li>
  213.  *  <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>
  214.  *  <pre>$handle->image_max_ratio = 1.5;</pre></li>
  215.  *  <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>
  216.  *  <pre>$handle->image_min_width = 100;</pre></li>
  217.  *  <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>
  218.  *  <pre>$handle->image_min_height = 500;</pre></li>
  219.  *  <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>
  220.  *  <pre>$handle->image_min_pixels = 20000;</pre></li>
  221.  *  <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>
  222.  *  <pre>$handle->image_min_ratio = 0.5;</pre></li>
  223.  * </ul>
  224.  * <ul>
  225.  *  <li><b>image_resize</b> determines is an image will be resized (default: false)<br>
  226.  *  <pre>$handle->image_resize = true;</pre></li>
  227.  * </ul>
  228.  *  The following variables are used only if {@link image_resize} == true
  229.  * <ul>
  230.  *  <li><b>image_x</b> destination image width (default: 150)<br>
  231.  *  <pre>$handle->image_x = 100;</pre></li>
  232.  *  <li><b>image_y</b> destination image height (default: 150)<br>
  233.  *  <pre>$handle->image_y = 200;</pre></li>
  234.  * </ul>
  235.  *  Use either one of the following
  236.  * <ul>
  237.  *  <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>
  238.  *  <pre>$handle->image_ratio = true;</pre></li>
  239.  *  <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>
  240.  *  <pre>$handle->image_ratio_crop = true;</pre></li>
  241.  *  <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>
  242.  *  <pre>$handle->image_ratio_fill = true;</pre></li>
  243.  *  <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>
  244.  *  <pre>$handle->image_ratio_no_zoom_in = true;</pre></li>
  245.  *  <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>
  246.  *  <pre>$handle->image_ratio_no_zoom_out = true;</pre></li>
  247.  *  <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>
  248.  *  <pre>$handle->image_ratio_x = true;</pre></li>
  249.  *  <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>
  250.  *  <pre>$handle->image_ratio_y = true;</pre></li>
  251.  *  <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>
  252.  *  <pre>$handle->image_ratio_pixels = 25000;</pre></li>
  253.  * </ul>
  254.  *  The following image manipulations require GD2+
  255.  * <ul>
  256.  *  <li><b>image_brightness</b> if set, corrects the brightness. value between -127 and 127 (default: null)<br>
  257.  *  <pre>$handle->image_brightness = 40;</pre></li>
  258.  *  <li><b>image_contrast</b> if set, corrects the contrast. value between -127 and 127 (default: null)<br>
  259.  *  <pre>$handle->image_contrast = 50;</pre></li>
  260.  *  <li><b>image_tint_color</b> if set, will tint the image with a color, value as hexadecimal #FFFFFF (default: null)<br>
  261.  *  <pre>$handle->image_tint_color = '#FF0000';</pre></li>
  262.  *  <li><b>image_overlay_color</b> if set, will add a colored overlay, value as hexadecimal #FFFFFF (default: null)<br>
  263.  *  <pre>$handle->image_overlay_color = '#FF0000';</pre></li>
  264.  *  <li><b>image_overlay_percent</b> used when {@link image_overlay_color} is set, determines the opacity (default: 50)<br>
  265.  *  <pre>$handle->image_overlay_percent = 20;</pre></li>
  266.  *  <li><b>image_negative</b> inverts the colors in the image (default: false)<br>
  267.  *  <pre>$handle->image_negative = true;</pre></li>
  268.  *  <li><b>image_greyscale</b> transforms an image into greyscale (default: false)<br>
  269.  *  <pre>$handle->image_greyscale = true;</pre></li>
  270.  *  <li><b>image_threshold</b> applies a threshold filter. value between -127 and 127 (default: null)<br>
  271.  *  <pre>$handle->image_threshold = 20;</pre></li>
  272.  * </ul>
  273.  * <ul>
  274.  *  <li><b>image_text</b> creates a text label on the image, value is a string, with eventual replacement tokens (default: null)<br>
  275.  *  <pre>$handle->image_text = 'test';</pre></li>
  276.  *  <li><b>image_text_direction</b> text label direction, either 'h' horizontal or 'v' vertical (default: 'h')<br>
  277.  *  <pre>$handle->image_text_direction = 'v';</pre></li>
  278.  *  <li><b>image_text_color</b> text color for the text label, in hexadecimal (default: #FFFFFF)<br>
  279.  *  <pre>$handle->image_text_color = '#FF0000';</pre></li>
  280.  *  <li><b>image_text_percent</b> text opacity on the text label, integer between 0 and 100 (default: 100)<br>
  281.  *  <pre>$handle->image_text_percent = 50;</pre></li>
  282.  *  <li><b>image_text_background</b> text label background color, in hexadecimal (default: null)<br>
  283.  *  <pre>$handle->image_text_background = '#FFFFFF';</pre></li>
  284.  *  <li><b>image_text_background_percent</b> text label background opacity, integer between 0 and 100 (default: 100)<br>
  285.  *  <pre>$handle->image_text_background_percent = 50;</pre></li>
  286.  *  <li><b>image_text_font</b> built-in font for the text label, from 1 to 5. 1 is the smallest (default: 5)<br>
  287.  *  <pre>$handle->image_text_font = 4;</pre></li>
  288.  *  <li><b>image_text_x</b> absolute text label position, in pixels from the left border. can be negative (default: null)<br>
  289.  *  <pre>$handle->image_text_x = 5;</pre></li>
  290.  *  <li><b>image_text_y</b> absolute text label position, in pixels from the top border. can be negative (default: null)<br>
  291.  *  <pre>$handle->image_text_y = 5;</pre></li>
  292.  *  <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>
  293.  *  <pre>$handle->image_text_position = 'LR';</pre></li>
  294.  *  <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>
  295.  *  <pre>$handle->image_text_padding = 5;</pre></li>
  296.  *  <li><b>image_text_padding_x</b> text label horizontal padding (default: null)<br>
  297.  *  <pre>$handle->image_text_padding_x = 2;</pre></li>
  298.  *  <li><b>image_text_padding_y</b> text label vertical padding (default: null)<br>
  299.  *  <pre>$handle->image_text_padding_y = 10;</pre></li>
  300.  *  <li><b>image_text_alignment</b> text alignment when text has multiple lines, either 'L', 'C' or 'R' (default: 'C')<br>
  301.  *  <pre>$handle->image_text_alignment = 'R';</pre></li>
  302.  *  <li><b>image_text_line_spacing</b> space between lines in pixels, when text has multiple lines (default: 0)<br>
  303.  *  <pre>$handle->image_text_line_spacing = 3;</pre></li>
  304.  * </ul>
  305.  * <ul>
  306.  *  <li><b>image_flip</b> flips image, wither 'h' horizontal or 'v' vertical (default: null)<br>
  307.  *  <pre>$handle->image_flip = 'h';</pre></li>
  308.  *  <li><b>image_rotate</b> rotates image. possible values are 90, 180 and 270 (default: null)<br>
  309.  *  <pre>$handle->image_rotate = 90;</pre></li>
  310.  *  <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>
  311.  *  <pre>$handle->image_crop = array(50,40,30,20); OR '-20 20%'...</pre></li>
  312.  *  <li><b>image_precrop</b> crops image, before an eventual resizing. 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>
  313.  *  <pre>$handle->image_precrop = array(50,40,30,20); OR '-20 20%'...</pre></li>
  314.  * </ul>
  315.  * <ul>
  316.  *  <li><b>image_bevel</b> adds a bevel border to the image. value is thickness in pixels (default: null)<br>
  317.  *  <pre>$handle->image_bevel = 20;</pre></li>
  318.  *  <li><b>image_bevel_color1</b> top and left bevel color, in hexadecimal (default: #FFFFFF)<br>
  319.  *  <pre>$handle->image_bevel_color1 = '#FFFFFF';</pre></li>
  320.  *  <li><b>image_bevel_color2</b> bottom and right bevel color, in hexadecimal (default: #000000)<br>
  321.  *  <pre>$handle->image_bevel_color2 = '#000000';</pre></li>
  322.  *  <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>
  323.  *  <pre>$handle->image_border = '3px'; OR '-20 20%' OR array(3,2)...</pre></li>
  324.  *  <li><b>image_border_color</b> border color, in hexadecimal (default: #FFFFFF)<br>
  325.  *  <pre>$handle->image_border_color = '#FFFFFF';</pre></li>
  326.  *  <li><b>image_frame</b> type of frame: 1=flat 2=crossed (default: null)<br>
  327.  *  <pre>$handle->image_frame = 2;</pre></li>
  328.  *  <li><b>image_frame_colors</b> list of hex colors, in an array or a space separated string (default: '#FFFFFF #999999 #666666 #000000')<br>
  329.  *  <pre>$handle->image_frame_colors = array('#999999',  '#FF0000', '#666666', '#333333', '#000000');</pre></li>
  330.  * </ul>
  331.  * <ul>
  332.  *  <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>
  333.  *  <pre>$handle->image_watermark = 'watermark.png';</pre></li>
  334.  *  <li><b>image_watermark_x</b> absolute watermark position, in pixels from the left border. can be negative (default: null)<br>
  335.  *  <pre>$handle->image_watermark_x = 5;</pre></li>
  336.  *  <li><b>image_watermark_y</b> absolute watermark position, in pixels from the top border. can be negative (default: null)<br>
  337.  *  <pre>$handle->image_watermark_y = 5;</pre></li>
  338.  *  <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>
  339.  *  <pre>$handle->image_watermark_position = 'LR';</pre></li>
  340.  * </ul>
  341.  * <ul>
  342.  *  <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>
  343.  *  <pre>$handle->image_reflection_height = '25%';</pre></li>
  344.  *  <li><b>image_reflection_space</b> space in pixels between the source image and the reflection, can be negative (default: null)<br>
  345.  *  <pre>$handle->image_reflection_space = 3;</pre></li>
  346.  *  <li><b>image_reflection_color</b> reflection background color, in hexadecimal. Now deprecated in favor of {@link image_default_color} (default: #FFFFFF)<br>
  347.  *  <pre>$handle->image_default_color = '#000000';</pre></li>
  348.  *  <li><b>image_reflection_opacity</b> opacity level at which the reflection starts, integer between 0 and 100 (default: 60)<br>
  349.  *  <pre>$handle->image_reflection_opacity = 60;</pre></li>
  350.  * </ul>
  351.  *
  352.  * <b>Values that can be read before calling {@link process}()</b>
  353.  * <ul>
  354.  *  <li><b>file_src_name</b> Source file name</li>
  355.  *  <li><b>file_src_name_body</b> Source file name body</li>
  356.  *  <li><b>file_src_name_ext</b> Source file extension</li>
  357.  *  <li><b>file_src_pathname</b> Source file complete path and name</li>
  358.  *  <li><b>file_src_mime</b> Source file mime type</li>
  359.  *  <li><b>file_src_size</b> Source file size in bytes</li>
  360.  *  <li><b>file_src_error</b> Upload error code</li>
  361.  *  <li><b>file_is_image</b> Boolean flag, true if the file is a supported image type</li>
  362.  * </ul>
  363.  * If the file is a supported image type (and <i>open_basedir</i> restrictions allow it)
  364.  * <ul>
  365.  *  <li><b>image_src_x</b> Source file width in pixels</li>
  366.  *  <li><b>image_src_y</b> Source file height in pixels</li>
  367.  *  <li><b>image_src_pixels</b> Source file number of pixels</li>
  368.  *  <li><b>image_src_type</b> Source file type (png, jpg, gif or bmp)</li>
  369.  *  <li><b>image_src_bits</b> Source file color depth</li>
  370.  * </ul>
  371.  *
  372.  * <b>Values that can be read after calling {@link process}()</b>
  373.  * <ul>
  374.  *  <li><b>file_dst_path</b> Destination file path</li>
  375.  *  <li><b>file_dst_name_body</b> Destination file name body</li>
  376.  *  <li><b>file_dst_name_ext</b> Destination file extension</li>
  377.  *  <li><b>file_dst_name</b> Destination file name</li>
  378.  *  <li><b>file_dst_pathname</b> Destination file complete path and name</li>
  379.  * </ul>
  380.  * If the file is a supported image type
  381.  * <ul>
  382.  *  <li><b>image_dst_x</b> Destination file width</li>
  383.  *  <li><b>image_dst_y</b> Destination file height</li>
  384.  *  <li><b>image_convert</b> Destination file format</li>
  385.  * </ul>
  386.  *
  387.  * <b>Requirements</b>
  388.  *
  389.  * Most of the image operations require GD. GD2 is greatly recommended
  390.  *
  391.  * The class is compatible with PHP 4.3+, and compatible with PHP5
  392.  *
  393.  * <b>Changelog</b>
  394.  * <ul>
  395.  *  <li><b>v 0.28</b> 10/08/2009<br>
  396.  *   - replaced ereg functions to be compatible with PHP 5.3<br>
  397.  *   - added flv MIME type<br>
  398.  *   - improved MIME type detection<br>
  399.  *   - added {@link file_name_body_pre} to prepend a string to the file name<br>
  400.  *   - added {@link mime_fileinfo}{@link mime_file}{@link mime_magic} and {@link mime_getimagesize} so that it is possible to deactivate some MIME type checking method<br>
  401.  *   - use exec() rather than shell_exec(), to play better with safe mode <br>
  402.  *   - added some error messages<br>
  403.  *   - fix bug when checking on conditions, {@link processed} wasn't propagated properly</li>
  404.  *  <li><b>v 0.27</b> 14/05/2009<br>
  405.  *   - look for the language files directory from __FILE__<br>
  406.  *   - deactivate {@link file_auto_rename} if {@link file_overwrite} is set<br>
  407.  *   - improved transparency replacement for true color images<br>
  408.  *   - fixed calls to newer version of UNIX file utility<br>
  409.  *   - fixed error when using PECL Fileinfo extension in SAFE MODE, and when using the finfo class<br>
  410.  *   - added {@link image_precrop} to crop the image before an eventual resizing</li>
  411.  *  <li><b>v 0.26</b> 13/11/2008<br>
  412.  *   - rewrote conversion from palette to true color to handle transparency better<br>
  413.  *   - fixed imagecopymergealpha() when the overlayed image is of wrong dimensions<br>
  414.  *   - fixed imagecreatenew() when the image to create have less than 1 pixels width or height<br>
  415.  *   - rewrote MIME type detection to be more secure and not rely on browser information; now using Fileinfo PECL extension, UNIX file() command, MIME magic, and getimagesize(), in that order<br>
  416.  *   - added support for Flash uploaders<br>
  417.  *   - some bug fixing and error handling</li>
  418.  *  <li><b>v 0.25</b> 17/11/2007<br>
  419.  *   - added translation files and mechanism to instantiate the class with a language different from English<br>
  420.  *   - added {@link forbidden} to set an array of forbidden MIME types<br>
  421.  *   - implemented support for simple wildcards in {@link allowed} and {@link forbidden}, such as image/*<br>
  422.  *   - preset the file extension to the desired conversion format when converting an image<br>
  423.  *   - added read and write support for BMP images<br>
  424.  *   - added a flag {@link file_is_image} to determine if the file is a supported image type<br>
  425.  *   - 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>
  426.  *   - improved logging; now provides useful system information<br>
  427.  *   - 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>
  428.  *   - added {@link image_ratio_pixels} to resize an image to a number of pixels, keeping aspect ratio<br>
  429.  *   - added {@link image_is_palette} and {@link image_is_transparent} and {@link image_transparent_color} for GIF images<br>
  430.  *   - added {@link image_default_color} to define a fallback color for non alpha-transparent output formats, such as JPEG or BMP<br>
  431.  *   - changed {@link image_background_color}, which now forces transparent areas to be painted<br>
  432.  *   - improved reflections and color overlays so that it works with alpha transparent images<br>
  433.  *   - {@link image_reflection_color} is now deprecated in favour of {@link image_default_color}<br />
  434.  *   - transparent PNGs are now processed in true color, and fully preserving the alpha channel when doing merges<br>
  435.  *   - transparent GIFs are now automatically detected. {@link preserve_transparency} is deprecated<br>
  436.  *   - transparent true color images can be saved as GIF while retaining transparency, semi transparent areas being merged with {@link image_default_color}<br>
  437.  *   - transparent true color images can be saved as JPG/BMP with the semi transparent areas being merged with {@link image_default_color}<br>
  438.  *   - fixed conversion of images to true color<br>
  439.  *   - 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>
  440.  *  <li><b>v 0.24</b> 25/05/2007<br>
  441.  *   - added {@link image_background_color}, to set the default background color of an image<br>
  442.  *   - added possibility of using replacement tokens in text labels<br>
  443.  *   - changed default JPEG quality to 85<br>
  444.  *   - fixed a small bug when using greyscale filter and associated filters<br>
  445.  *   - 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>
  446.  *   - improved the recursive creation of directories<br>
  447.  *   - the class now converts palette based images to true colors before doing graphic manipulations</li>
  448.  *  <li><b>v 0.23</b> 23/12/2006<br>
  449.  *   - 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>
  450.  *  <li><b>v 0.22</b> 16/12/2006<br>
  451.  *   - added automatic creation of a temporary file if the upload directory is not within open_basedir<br>
  452.  *   - fixed a bug which was preventing to work on a local file by overwriting it with its processed copy<br>
  453.  *   - added MIME types video/x-ms-wmv and image/x-png and fixed PNG support for IE weird MIME types<br>
  454.  *   - 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>
  455.  *   - added support for multiple lines in the text, using "\n" as a line break<br>
  456.  *   - added {@link image_text_line_spacing} which allow to set the space between several lines of text<br>
  457.  *   - added {@link image_text_alignment} which allow to set the alignment when text has several lines<br>
  458.  *   - {@link image_text_font} can now be set to the path of a GDF font to load external fonts<br>
  459.  *   - added {@link image_reflection_height} to create a reflection of the source image, which height is in pixels or percentage<br>
  460.  *   - added {@link image_reflection_space} to set the space in pixels between the source image and the reflection<br>
  461.  *   - added {@link image_reflection_color} to set the reflection background color<br>
  462.  *   - added {@link image_reflection_opacity} to set the initial level of opacity of the reflection</li>
  463.  *  <li><b>v 0.21</b> 30/09/2006<br>
  464.  *   - 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>
  465.  *   - added {@link mime_check}, which default is true, to set checks against {@link allowed} MIME list<br>
  466.  *   - if MIME is empty, the class now triggers an error<br>
  467.  *   - color #000000 is OK for {@link image_text_color}, and related text transparency bug fixed<br>
  468.  *   - {@link gd_version}() now uses gd_info(), or else phpinfo()<br>
  469.  *   - fixed path issue when the destination path has no trailing slash on Windows systems <br>
  470.  *   - removed inline functions to be fully PHP5 compatible </li>
  471.  *  <li><b>v 0.20</b> 11/08/2006<br>
  472.  *   - added some more error checking and messages (GD presence, permissions...)<br>
  473.  *   - fix when uploading files without extension<br>
  474.  *   - changed values for {@link image_brightness} and {@link image_contrast} to be between -127 and 127<br>
  475.  *   - added {@link dir_auto_create} to automatically and recursively create destination directory if missing.<br>
  476.  *   - added {@link dir_auto_chmod} to automatically chmod the destination directory if not writeable.<br>
  477.  *   - added {@link dir_chmod} to set the default chmod to use.<br>
  478.  *   - added {@link image_crop} to crop images<br>
  479.  *   - added {@link image_negative} to invert the colors on the image<br>
  480.  *   - added {@link image_greyscale} to turn the image into greyscale<br>
  481.  *   - added {@link image_threshold} to apply a threshold filter on the image<br>
  482.  *   - added {@link image_bevel}{@link image_bevel_color1} and {@link image_bevel_color2} to add a bevel border<br>
  483.  *   - added {@link image_border} and {@link image_border_color} to add a single color border<br>
  484.  *   - added {@link image_frame} and {@link image_frame_colors} to add a multicolored frame</li>
  485.  *  <li><b>v 0.19</b> 29/03/2006<br>
  486.  *   - class is now compatible i18n (thanks Sylwester).<br>
  487.  *   - the class can mow manipulate local files, not only uploaded files (instanciate the class with a local filename).<br>
  488.  *   - {@link file_safe_name} has been improved a bit.<br>
  489.  *   - 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>
  490.  *   - added {@link image_text} and all derivated settings to add a text label on the image.<br>
  491.  *   - added {@link image_watermark} and all derivated settings to add a watermark image on the image.<br>
  492.  *   - added {@link image_flip} and {@link image_rotate} for more image manipulations<br>
  493.  *   - added {@link jpeg_size} to calculate the JPG compression quality in order to fit within one filesize.</li>
  494.  *  <li><b>v 0.18</b> 02/02/2006<br>
  495.  *   - added {@link no_script} to turn dangerous scripts into text files.<br>
  496.  *   - added {@link mime_magic_check} to set the class to use mime_magic.<br>
  497.  *   - added {@link preserve_transparency} *experimental*. Thanks Gregor.<br>
  498.  *   - fixed size and mime checking, wasn't working :/ Thanks Willem.<br>
  499.  *   - fixed memory leak when resizing images.<br>
  500.  *   - when resizing, it is not necessary anymore to set {@link image_convert}.<br>
  501.  *   - il is now possible to simply convert an image, with no resizing.<br>
  502.  *   - sets the default {@link file_max_size} to upload_max_filesize from php.ini. Thanks Edward</li>
  503.  *  <li><b>v 0.17</b> 28/05/2005<br>
  504.  *   - the class can be used with any version of GD.<br>
  505.  *   - added security check on the file with a list of mime-types.<br>
  506.  *   - changed the license to GPL v2 only</li>
  507.  *  <li><b>v 0.16</b> 19/05/2005<br>
  508.  *   - added {@link file_auto_rename} automatic file renaming if the same filename already exists.<br>
  509.  *   - added {@link file_safe_name} safe formatting of the filename (spaces to _underscores so far).<br>
  510.  *   - added some more error reporting to avoid crash if GD is not present</li>
  511.  *  <li><b>v 0.15</b> 16/04/2005<br>
  512.  *   - added JPEG compression quality setting. Thanks Vad</li>
  513.  *  <li><b>v 0.14</b> 14/03/2005<br>
  514.  *   - reworked the class file to allow parsing with phpDocumentor</li>
  515.  *  <li><b>v 0.13</b> 07/03/2005<br>
  516.  *   - fixed a bug with {@link image_ratio}. Thanks Justin.<br>
  517.  *   - added {@link image_ratio_no_zoom_in} and {@link image_ratio_no_zoom_out} </li>
  518.  *  <li><b>v 0.12</b> 21/01/2005<br>
  519.  *   - added {@link image_ratio} to resize within max values, keeping image ratio</li>
  520.  *  <li><b>v 0.11</b> 22/08/2003<br>
  521.  *   - update for GD2 (changed imageresized() into imagecopyresampled() and imagecreate() into imagecreatetruecolor())</li>
  522.  * </ul>
  523.  *
  524.  * @package   cmf
  525.  * @subpackage external
  526.  */
  527. class upload {
  528.  
  529.  
  530.     /**
  531.      * Class version
  532.      *
  533.      * @access public
  534.      * @var string 
  535.      */
  536.     var $version;
  537.  
  538.     /**
  539.      * Uploaded file name
  540.      *
  541.      * @access public
  542.      * @var string 
  543.      */
  544.     var $file_src_name;
  545.  
  546.     /**
  547.      * Uploaded file name body (i.e. without extension)
  548.      *
  549.      * @access public
  550.      * @var string 
  551.      */
  552.     var $file_src_name_body;
  553.  
  554.     /**
  555.      * Uploaded file name extension
  556.      *
  557.      * @access public
  558.      * @var string 
  559.      */
  560.     var $file_src_name_ext;
  561.  
  562.     /**
  563.      * Uploaded file MIME type
  564.      *
  565.      * @access public
  566.      * @var string 
  567.      */
  568.     var $file_src_mime;
  569.  
  570.     /**
  571.      * Uploaded file size, in bytes
  572.      *
  573.      * @access public
  574.      * @var double 
  575.      */
  576.     var $file_src_size;
  577.  
  578.     /**
  579.      * Holds eventual PHP error code from $_FILES
  580.      *
  581.      * @access public
  582.      * @var string 
  583.      */
  584.     var $file_src_error;
  585.  
  586.     /**
  587.      * Uloaded file name, including server path
  588.      *
  589.      * @access private
  590.      * @var string 
  591.      */
  592.     var $file_src_pathname;
  593.  
  594.     /**
  595.      * Uloaded file name temporary copy
  596.      *
  597.      * @access private
  598.      * @var string 
  599.      */
  600.     var $file_src_temp;
  601.  
  602.     /**
  603.      * Destination file name
  604.      *
  605.      * @access private
  606.      * @var string 
  607.      */
  608.     var $file_dst_path;
  609.  
  610.     /**
  611.      * Destination file name
  612.      *
  613.      * @access public
  614.      * @var string 
  615.      */
  616.     var $file_dst_name;
  617.  
  618.     /**
  619.      * Destination file name body (i.e. without extension)
  620.      *
  621.      * @access public
  622.      * @var string 
  623.      */
  624.     var $file_dst_name_body;
  625.  
  626.     /**
  627.      * Destination file extension
  628.      *
  629.      * @access public
  630.      * @var string 
  631.      */
  632.     var $file_dst_name_ext;
  633.  
  634.     /**
  635.      * Destination file name, including path
  636.      *
  637.      * @access private
  638.      * @var string 
  639.      */
  640.     var $file_dst_pathname;
  641.  
  642.     /**
  643.      * Source image width
  644.      *
  645.      * @access private
  646.      * @var integer 
  647.      */
  648.     var $image_src_x;
  649.  
  650.     /**
  651.      * Source image height
  652.      *
  653.      * @access private
  654.      * @var integer 
  655.      */
  656.     var $image_src_y;
  657.  
  658.     /**
  659.      * Source image color depth
  660.      *
  661.      * @access private
  662.      * @var integer 
  663.      */
  664.     var $image_src_bits;
  665.  
  666.     /**
  667.      * Number of pixels
  668.      *
  669.      * @access private
  670.      * @var long 
  671.      */
  672.     var $image_src_pixels;
  673.  
  674.     /**
  675.      * Type of image (png, gif, jpg or bmp)
  676.      *
  677.      * @access private
  678.      * @var string 
  679.      */
  680.     var $image_src_type;
  681.  
  682.     /**
  683.      * Destination image width
  684.      *
  685.      * @access private
  686.      * @var integer 
  687.      */
  688.     var $image_dst_x;
  689.  
  690.     /**
  691.      * Destination image height
  692.      *
  693.      * @access private
  694.      * @var integer 
  695.      */
  696.     var $image_dst_y;
  697.  
  698.     /**
  699.      * Supported image formats
  700.      *
  701.      * @access private
  702.      * @var array 
  703.      */
  704.     var $image_supported;
  705.  
  706.     /**
  707.      * Flag to determine if the source file is an image
  708.      *
  709.      * @access private
  710.      * @var boolean 
  711.      */
  712.     var $file_is_image;
  713.  
  714.     /**
  715.      * Flag set after instanciating the class
  716.      *
  717.      * Indicates if the file has been uploaded properly
  718.      *
  719.      * @access public
  720.      * @var bool 
  721.      */
  722.     var $uploaded;
  723.  
  724.     /**
  725.      * Flag stopping PHP upload checks
  726.      *
  727.      * Indicates whether we instanciated the class with a filename, in which case
  728.      * we will not check on the validity of the PHP *upload*
  729.      *
  730.      * This flag is automatically set to true when working on a local file
  731.      *
  732.      * Warning: for uploads, this flag MUST be set to false for security reason
  733.      *
  734.      * @access public
  735.      * @var bool 
  736.      */
  737.     var $no_upload_check;
  738.  
  739.     /**
  740.      * Flag set after calling a process
  741.      *
  742.      * Indicates if the processing, and copy of the resulting file went OK
  743.      *
  744.      * @access public
  745.      * @var bool 
  746.      */
  747.     var $processed;
  748.  
  749.     /**
  750.      * Holds eventual error message in plain english
  751.      *
  752.      * @access public
  753.      * @var string 
  754.      */
  755.     var $error;
  756.  
  757.     /**
  758.      * Holds an HTML formatted log
  759.      *
  760.      * @access public
  761.      * @var string 
  762.      */
  763.     var $log;
  764.  
  765.  
  766.     // overiddable processing variables
  767.  
  768.  
  769.     /**
  770.      * Set this variable to replace the name body (i.e. without extension)
  771.      *
  772.      * @access public
  773.      * @var string 
  774.      */
  775.     var $file_new_name_body;
  776.  
  777.     /**
  778.      * Set this variable to append a string to the file name body
  779.      *
  780.      * @access public
  781.      * @var string 
  782.      */
  783.     var $file_name_body_add;
  784.  
  785.     /**
  786.      * Set this variable to prepend a string to the file name body
  787.      *
  788.      * @access public
  789.      * @var string 
  790.      */
  791.     var $file_name_body_pre;
  792.  
  793.     /**
  794.      * Set this variable to change the file extension
  795.      *
  796.      * @access public
  797.      * @var string 
  798.      */
  799.     var $file_new_name_ext;
  800.  
  801.     /**
  802.      * Set this variable to format the filename (spaces changed to _)
  803.      *
  804.      * @access public
  805.      * @var boolean 
  806.      */
  807.     var $file_safe_name;
  808.  
  809.     /**
  810.      * Set this variable to false if you don't want to check the MIME against the allowed list
  811.      *
  812.      * This variable is set to true by default for security reason
  813.      *
  814.      * @access public
  815.      * @var boolean 
  816.      */
  817.     var $mime_check;
  818.  
  819.     /**
  820.      * Set this variable to false if you don't want to check the MIME with Fileinfo PECL extension
  821.      *
  822.      * You can also set it with the path of the magic database file.
  823.      * If set to true, the class will try to read the MAGIC environment variable
  824.      *   and if it is empty, will default to '/usr/share/file/magic'
  825.      * If set to an empty string, it will call finfo_open without the path argument
  826.      *
  827.      * This variable is set to true by default for security reason
  828.      *
  829.      * @access public
  830.      * @var boolean 
  831.      */
  832.     var $mime_fileinfo;
  833.  
  834.     /**
  835.      * Set this variable to false if you don't want to check the MIME with UNIX file() command
  836.      *
  837.      * This variable is set to true by default for security reason
  838.      *
  839.      * @access public
  840.      * @var boolean 
  841.      */
  842.     var $mime_file;
  843.  
  844.     /**
  845.      * Set this variable to false if you don't want to check the MIME with the magic.mime file
  846.      *
  847.      * The function mime_content_type() will be deprecated,
  848.      * and this variable will be set to false in a future release
  849.      *
  850.      * This variable is set to true by default for security reason
  851.      *
  852.      * @access public
  853.      * @var boolean 
  854.      */
  855.     var $mime_magic;
  856.  
  857.     /**
  858.      * Set this variable to false if you don't want to check the MIME with getimagesize()
  859.      *
  860.      * The class tries to get a MIME type from getimagesize()
  861.      * If no MIME is returned, it tries to guess the MIME type from the file type
  862.      *
  863.      * This variable is set to true by default for security reason
  864.      *
  865.      * @access public
  866.      * @var boolean 
  867.      */
  868.     var $mime_getimagesize;
  869.  
  870.     /**
  871.      * Set this variable to false if you don't want to turn dangerous scripts into simple text files
  872.      *
  873.      * @access public
  874.      * @var boolean 
  875.      */
  876.     var $no_script;
  877.  
  878.     /**
  879.      * Set this variable to true to allow automatic renaming of the file
  880.      * if the file already exists
  881.      *
  882.      * Default value is true
  883.      *
  884.      * For instance, on uploading foo.ext,<br>
  885.      * if foo.ext already exists, upload will be renamed foo_1.ext<br>
  886.      * and if foo_1.ext already exists, upload will be renamed foo_2.ext<br>
  887.      *
  888.      * Note that this option doesn't have any effect if {@link file_overwrite} is true
  889.      *
  890.      * @access public
  891.      * @var bool 
  892.      */
  893.     var $file_auto_rename;
  894.  
  895.     /**
  896.      * Set this variable to true to allow automatic creation of the destination
  897.      * directory if it is missing (works recursively)
  898.      *
  899.      * Default value is true
  900.      *
  901.      * @access public
  902.      * @var bool 
  903.      */
  904.     var $dir_auto_create;
  905.  
  906.     /**
  907.      * Set this variable to true to allow automatic chmod of the destination
  908.      * directory if it is not writeable
  909.      *
  910.      * Default value is true
  911.      *
  912.      * @access public
  913.      * @var bool 
  914.      */
  915.     var $dir_auto_chmod;
  916.  
  917.     /**
  918.      * Set this variable to the default chmod you want the class to use
  919.      * when creating directories, or attempting to write in a directory
  920.      *
  921.      * Default value is 0777 (without quotes)
  922.      *
  923.      * @access public
  924.      * @var bool 
  925.      */
  926.     var $dir_chmod;
  927.  
  928.     /**
  929.      * Set this variable tu true to allow overwriting of an existing file
  930.      *
  931.      * Default value is false, so no files will be overwritten
  932.      *
  933.      * @access public
  934.      * @var bool 
  935.      */
  936.     var $file_overwrite;
  937.  
  938.     /**
  939.      * Set this variable to change the maximum size in bytes for an uploaded file
  940.      *
  941.      * Default value is the value <i>upload_max_filesize</i> from php.ini
  942.      *
  943.      * @access public
  944.      * @var double 
  945.      */
  946.     var $file_max_size;
  947.  
  948.     /**
  949.      * Set this variable to true to resize the file if it is an image
  950.      *
  951.      * You will probably want to set {@link image_x} and {@link image_y}, and maybe one of the ratio variables
  952.      *
  953.      * Default value is false (no resizing)
  954.      *
  955.      * @access public
  956.      * @var bool 
  957.      */
  958.     var $image_resize;
  959.  
  960.     /**
  961.      * Set this variable to convert the file if it is an image
  962.      *
  963.      * Possibles values are : ''; 'png'; 'jpeg'; 'gif'; 'bmp'
  964.      *
  965.      * Default value is '' (no conversion)<br>
  966.      * If {@link resize} is true, {@link convert} will be set to the source file extension
  967.      *
  968.      * @access public
  969.      * @var string 
  970.      */
  971.     var $image_convert;
  972.  
  973.     /**
  974.      * Set this variable to the wanted (or maximum/minimum) width for the processed image, in pixels
  975.      *
  976.      * Default value is 150
  977.      *
  978.      * @access public
  979.      * @var integer 
  980.      */
  981.     var $image_x;
  982.  
  983.     /**
  984.      * Set this variable to the wanted (or maximum/minimum) height for the processed image, in pixels
  985.      *
  986.      * Default value is 150
  987.      *
  988.      * @access public
  989.      * @var integer 
  990.      */
  991.     var $image_y;
  992.  
  993.     /**
  994.      * Set this variable to keep the original size ratio to fit within {@link image_x} x {@link image_y}
  995.      *
  996.      * Default value is false
  997.      *
  998.      * @access public
  999.      * @var bool 
  1000.      */
  1001.     var $image_ratio;
  1002.  
  1003.     /**
  1004.      * Set this variable to keep the original size ratio to fit within {@link image_x} x {@link image_y}
  1005.      *
  1006.      * The image will be resized as to fill the whole space, and excedent will be cropped
  1007.      *
  1008.      * Value can also be a string, one or more character from 'TBLR' (top, bottom, left and right)
  1009.      * If set as a string, it determines which side of the image is kept while cropping.
  1010.      * By default, the part of the image kept is in the center, i.e. it crops equally on both sides
  1011.      *
  1012.      * Default value is false
  1013.      *
  1014.      * @access public
  1015.      * @var mixed 
  1016.      */
  1017.     var $image_ratio_crop;
  1018.  
  1019.     /**
  1020.      * Set this variable to keep the original size ratio to fit within {@link image_x} x {@link image_y}
  1021.      *
  1022.      * The image will be resized to fit entirely in the space, and the rest will be colored.
  1023.      * The default color is white, but can be set with {@link image_default_color}
  1024.      *
  1025.      * Value can also be a string, one or more character from 'TBLR' (top, bottom, left and right)
  1026.      * If set as a string, it determines in which side of the space the image is displayed.
  1027.      * By default, the image is displayed in the center, i.e. it fills the remaining space equally on both sides
  1028.      *
  1029.      * Default value is false
  1030.      *
  1031.      * @access public
  1032.      * @var mixed 
  1033.      */
  1034.     var $image_ratio_fill;
  1035.  
  1036.     /**
  1037.      * Set this variable to a number of pixels so that {@link image_x} and {@link image_y} are the best match possible
  1038.      *
  1039.      * The image will be resized to have approximatively the number of pixels
  1040.      * The aspect ratio wil be conserved
  1041.      *
  1042.      * Default value is false
  1043.      *
  1044.      * @access public
  1045.      * @var mixed 
  1046.      */
  1047.     var $image_ratio_pixels;
  1048.  
  1049.     /**
  1050.      * Set this variable to keep the original size ratio to fit within {@link image_x} x {@link image_y},
  1051.      * but only if original image is bigger
  1052.      *
  1053.      * Default value is false
  1054.      *
  1055.      * @access public
  1056.      * @var bool 
  1057.      */
  1058.  
  1059.     /**
  1060.      * Set this variable to keep the original size ratio to fit within {@link image_x} x {@link image_y},
  1061.      * but only if original image is smaller
  1062.      *
  1063.      * Default value is false
  1064.      *
  1065.      * @access public
  1066.      * @var bool 
  1067.      */
  1068.  
  1069.     /**
  1070.      * Set this variable to calculate {@link image_x} automatically , using {@link image_y} and conserving ratio
  1071.      *
  1072.      * Default value is false
  1073.      *
  1074.      * @access public
  1075.      * @var bool 
  1076.      */
  1077.     var $image_ratio_x;
  1078.  
  1079.     /**
  1080.      * Set this variable to calculate {@link image_y} automatically , using {@link image_x} and conserving ratio
  1081.      *
  1082.      * Default value is false
  1083.      *
  1084.      * @access public
  1085.      * @var bool 
  1086.      */
  1087.     var $image_ratio_y;
  1088.  
  1089.     /**
  1090.      * Set this variable to set a maximum image width, above which the upload will be invalid
  1091.      *
  1092.      * Default value is null
  1093.      *
  1094.      * @access public
  1095.      * @var integer 
  1096.      */
  1097.     var $image_max_width;
  1098.  
  1099.     /**
  1100.      * Set this variable to set a maximum image height, above which the upload will be invalid
  1101.      *
  1102.      * Default value is null
  1103.      *
  1104.      * @access public
  1105.      * @var integer 
  1106.      */
  1107.     var $image_max_height;
  1108.  
  1109.     /**
  1110.      * Set this variable to set a maximum number of pixels for an image, above which the upload will be invalid
  1111.      *
  1112.      * Default value is null
  1113.      *
  1114.      * @access public
  1115.      * @var long 
  1116.      */
  1117.     var $image_max_pixels;
  1118.  
  1119.     /**
  1120.      * Set this variable to set a maximum image aspect ratio, above which the upload will be invalid
  1121.      *
  1122.      * Note that ratio = width / height
  1123.      *
  1124.      * Default value is null
  1125.      *
  1126.      * @access public
  1127.      * @var float 
  1128.      */
  1129.     var $image_max_ratio;
  1130.  
  1131.     /**
  1132.      * Set this variable to set a minimum image width, below which the upload will be invalid
  1133.      *
  1134.      * Default value is null
  1135.      *
  1136.      * @access public
  1137.      * @var integer 
  1138.      */
  1139.     var $image_min_width;
  1140.  
  1141.     /**
  1142.      * Set this variable to set a minimum image height, below which the upload will be invalid
  1143.      *
  1144.      * Default value is null
  1145.      *
  1146.      * @access public
  1147.      * @var integer 
  1148.      */
  1149.     var $image_min_height;
  1150.  
  1151.     /**
  1152.      * Set this variable to set a minimum number of pixels for an image, below which the upload will be invalid
  1153.      *
  1154.      * Default value is null
  1155.      *
  1156.      * @access public
  1157.      * @var long 
  1158.      */
  1159.     var $image_min_pixels;
  1160.  
  1161.     /**
  1162.      * Set this variable to set a minimum image aspect ratio, below which the upload will be invalid
  1163.      *
  1164.      * Note that ratio = width / height
  1165.      *
  1166.      * Default value is null
  1167.      *
  1168.      * @access public
  1169.      * @var float 
  1170.      */
  1171.     var $image_min_ratio;
  1172.  
  1173.     /**
  1174.      * Quality of JPEG created/converted destination image
  1175.      *
  1176.      * Default value is 85
  1177.      *
  1178.      * @access public
  1179.      * @var integer 
  1180.      */
  1181.     var $jpeg_quality;
  1182.  
  1183.     /**
  1184.      * Determines the quality of the JPG image to fit a desired file size
  1185.      *
  1186.      * Value is in bytes. The JPG quality will be set between 1 and 100%
  1187.      * The calculations are approximations.
  1188.      *
  1189.      * Default value is null (no calculations)
  1190.      *
  1191.      * @access public
  1192.      * @var integer 
  1193.      */
  1194.     var $jpeg_size;
  1195.  
  1196.     /**
  1197.      * Preserve transparency when resizing or converting an image (deprecated)
  1198.      *
  1199.      * Default value is automatically set to true for transparent GIFs
  1200.      * This setting is now deprecated
  1201.      *
  1202.      * @access public
  1203.      * @var integer 
  1204.      */
  1205.  
  1206.     /**
  1207.      * Flag set to true when the image is transparent
  1208.      *
  1209.      * This is actually used only for transparent GIFs
  1210.      *
  1211.      * @access public
  1212.      * @var boolean 
  1213.      */
  1214.     var $image_is_transparent;
  1215.  
  1216.     /**
  1217.      * Transparent color in a palette
  1218.      *
  1219.      * This is actually used only for transparent GIFs
  1220.      *
  1221.      * @access public
  1222.      * @var boolean 
  1223.      */
  1224.  
  1225.     /**
  1226.      * Background color, used to paint transparent areas with
  1227.      *
  1228.      * If set, it will forcibly remove transparency by painting transparent areas with the color
  1229.      * This setting will fill in all transparent areas in PNG and GIF, as opposed to {@link image_default_color}
  1230.      * which will do so only in BMP, JPEG, and alpha transparent areas in transparent GIFs
  1231.      * This setting overrides {@link image_default_color}
  1232.      *
  1233.      * Default value is null
  1234.      *
  1235.      * @access public
  1236.      * @var string 
  1237.      */
  1238.  
  1239.     /**
  1240.      * Default color for non alpha-transparent images
  1241.      *
  1242.      * This setting is to be used to define a background color for semi transparent areas
  1243.      * of an alpha transparent when the output format doesn't support alpha transparency
  1244.      * This is useful when, from an alpha transparent PNG image, or an image with alpha transparent features
  1245.      * if you want to output it as a transparent GIFs for instance, you can set a blending color for transparent areas
  1246.      * If you output in JPEG or BMP, this color will be used to fill in the previously transparent areas
  1247.      *
  1248.      * The default color white
  1249.      *
  1250.      * @access public
  1251.      * @var boolean 
  1252.      */
  1253.     var $image_default_color;
  1254.  
  1255.     /**
  1256.      * Flag set to true when the image is not true color
  1257.      *
  1258.      * @access public
  1259.      * @var boolean 
  1260.      */
  1261.     var $image_is_palette;
  1262.  
  1263.     /**
  1264.      * Corrects the image brightness
  1265.      *
  1266.      * Value can range between -127 and 127
  1267.      *
  1268.      * Default value is null
  1269.      *
  1270.      * @access public
  1271.      * @var integer 
  1272.      */
  1273.     var $image_brightness;
  1274.  
  1275.     /**
  1276.      * Corrects the image contrast
  1277.      *
  1278.      * Value can range between -127 and 127
  1279.      *
  1280.      * Default value is null
  1281.      *
  1282.      * @access public
  1283.      * @var integer 
  1284.      */
  1285.     var $image_contrast;
  1286.  
  1287.     /**
  1288.      * Applies threshold filter
  1289.      *
  1290.      * Value can range between -127 and 127
  1291.      *
  1292.      * Default value is null
  1293.      *
  1294.      * @access public
  1295.      * @var integer 
  1296.      */
  1297.     var $image_threshold;
  1298.  
  1299.     /**
  1300.      * Applies a tint on the image
  1301.      *
  1302.      * Value is an hexadecimal color, such as #FFFFFF
  1303.      *
  1304.      * Default value is null
  1305.      *
  1306.      * @access public
  1307.      * @var string; 
  1308.      */
  1309.     var $image_tint_color;
  1310.  
  1311.     /**
  1312.      * Applies a colored overlay on the image
  1313.      *
  1314.      * Value is an hexadecimal color, such as #FFFFFF
  1315.      *
  1316.      * To use with {@link image_overlay_percent}
  1317.      *
  1318.      * Default value is null
  1319.      *
  1320.      * @access public
  1321.      * @var string; 
  1322.      */
  1323.     var $image_overlay_color;
  1324.  
  1325.     /**
  1326.      * Sets the percentage for the colored overlay
  1327.      *
  1328.      * Value is a percentage, as an integer between 0 and 100
  1329.      *
  1330.      * Unless used with {@link image_overlay_color}, this setting has no effect
  1331.      *
  1332.      * Default value is 50
  1333.      *
  1334.      * @access public
  1335.      * @var integer 
  1336.      */
  1337.  
  1338.     /**
  1339.      * Inverts the color of an image
  1340.      *
  1341.      * Default value is FALSE
  1342.      *
  1343.      * @access public
  1344.      * @var boolean; 
  1345.      */
  1346.     var $image_negative;
  1347.  
  1348.     /**
  1349.      * Turns the image into greyscale
  1350.      *
  1351.      * Default value is FALSE
  1352.      *
  1353.      * @access public
  1354.      * @var boolean; 
  1355.      */
  1356.     var $image_greyscale;
  1357.  
  1358.     /**
  1359.      * Adds a text label on the image
  1360.      *
  1361.      * Value is a string, any text. Text will not word-wrap, although you can use breaklines in your text "\n"
  1362.      *
  1363.      * If set, this setting allow the use of all other settings starting with image_text_
  1364.      *
  1365.      * Replacement tokens can be used in the string:
  1366.      * <pre>
  1367.      * gd_version    src_name       src_name_body src_name_ext
  1368.      * src_pathname  src_mime       src_x         src_y
  1369.      * src_type      src_bits       src_pixels
  1370.      * src_size      src_size_kb    src_size_mb   src_size_human
  1371.      * dst_path      dst_name_body  dst_pathname
  1372.      * dst_name      dst_name_ext   dst_x         dst_y
  1373.      * date          time           host          server        ip
  1374.      * </pre>
  1375.      * The tokens must be enclosed in square brackets: [dst_x] will be replaced by the width of the picture
  1376.      *
  1377.      * Default value is null
  1378.      *
  1379.      * @access public
  1380.      * @var string; 
  1381.      */
  1382.     var $image_text;
  1383.  
  1384.     /**
  1385.      * Sets the text direction for the text label
  1386.      *
  1387.      * Value is either 'h' or 'v', as in horizontal and vertical
  1388.      *
  1389.      * Default value is h (horizontal)
  1390.      *
  1391.      * @access public
  1392.      * @var string; 
  1393.      */
  1394.     var $image_text_direction;
  1395.  
  1396.     /**
  1397.      * Sets the text color for the text label
  1398.      *
  1399.      * Value is an hexadecimal color, such as #FFFFFF
  1400.      *
  1401.      * Default value is #FFFFFF (white)
  1402.      *
  1403.      * @access public
  1404.      * @var string; 
  1405.      */
  1406.     var $image_text_color;
  1407.  
  1408.     /**
  1409.      * Sets the text visibility in the text label
  1410.      *
  1411.      * Value is a percentage, as an integer between 0 and 100
  1412.      *
  1413.      * Default value is 100
  1414.      *
  1415.      * @access public
  1416.      * @var integer 
  1417.      */
  1418.     var $image_text_percent;
  1419.  
  1420.     /**
  1421.      * Sets the text background color for the text label
  1422.      *
  1423.      * Value is an hexadecimal color, such as #FFFFFF
  1424.      *
  1425.      * Default value is null (no background)
  1426.      *
  1427.      * @access public
  1428.      * @var string; 
  1429.      */
  1430.  
  1431.     /**
  1432.      * Sets the text background visibility in the text label
  1433.      *
  1434.      * Value is a percentage, as an integer between 0 and 100
  1435.      *
  1436.      * Default value is 100
  1437.      *
  1438.      * @access public
  1439.      * @var integer 
  1440.      */
  1441.  
  1442.     /**
  1443.      * Sets the text font in the text label
  1444.      *
  1445.      * Value is a an integer between 1 and 5 for GD built-in fonts. 1 is the smallest font, 5 the biggest
  1446.      * Value can also be a string, which represents the path to a GDF font. The font will be loaded into GD, and used as a built-in font.
  1447.      *
  1448.      * Default value is 5
  1449.      *
  1450.      * @access public
  1451.      * @var mixed; 
  1452.      */
  1453.     var $image_text_font;
  1454.  
  1455.     /**
  1456.      * Sets the text label position within the image
  1457.      *
  1458.      * Value is one or two out of 'TBLR' (top, bottom, left, right)
  1459.      *
  1460.      * The positions are as following:
  1461.      * <pre>
  1462.      *                        TL  T  TR
  1463.      *                        L       R
  1464.      *                        BL  B  BR
  1465.      * </pre>
  1466.      *
  1467.      * Default value is null (centered, horizontal and vertical)
  1468.      *
  1469.      * Note that is {@link image_text_x} and {@link image_text_y} are used, this setting has no effect
  1470.      *
  1471.      * @access public
  1472.      * @var string; 
  1473.      */
  1474.     var $image_text_position;
  1475.  
  1476.     /**
  1477.      * Sets the text label absolute X position within the image
  1478.      *
  1479.      * Value is in pixels, representing the distance between the left of the image and the label
  1480.      * If a negative value is used, it will represent the distance between the right of the image and the label
  1481.      *
  1482.      * Default value is null (so {@link image_text_position} is used)
  1483.      *
  1484.      * @access public
  1485.      * @var integer 
  1486.      */
  1487.     var $image_text_x;
  1488.  
  1489.     /**
  1490.      * Sets the text label absolute Y position within the image
  1491.      *
  1492.      * Value is in pixels, representing the distance between the top of the image and the label
  1493.      * If a negative value is used, it will represent the distance between the bottom of the image and the label
  1494.      *
  1495.      * Default value is null (so {@link image_text_position} is used)
  1496.      *
  1497.      * @access public
  1498.      * @var integer 
  1499.      */
  1500.     var $image_text_y;
  1501.  
  1502.     /**
  1503.      * Sets the text label padding
  1504.      *
  1505.      * Value is in pixels, representing the distance between the text and the label background border
  1506.      *
  1507.      * Default value is 0
  1508.      *
  1509.      * This setting can be overriden by {@link image_text_padding_x} and {@link image_text_padding_y}
  1510.      *
  1511.      * @access public
  1512.      * @var integer 
  1513.      */
  1514.     var $image_text_padding;
  1515.  
  1516.     /**
  1517.      * Sets the text label horizontal padding
  1518.      *
  1519.      * Value is in pixels, representing the distance between the text and the left and right label background borders
  1520.      *
  1521.      * Default value is null
  1522.      *
  1523.      * If set, this setting overrides the horizontal part of {@link image_text_padding}
  1524.      *
  1525.      * @access public
  1526.      * @var integer 
  1527.      */
  1528.     var $image_text_padding_x;
  1529.  
  1530.     /**
  1531.      * Sets the text label vertical padding
  1532.      *
  1533.      * Value is in pixels, representing the distance between the text and the top and bottom label background borders
  1534.      *
  1535.      * Default value is null
  1536.      *
  1537.      * If set, his setting overrides the vertical part of {@link image_text_padding}
  1538.      *
  1539.      * @access public
  1540.      * @var integer 
  1541.      */
  1542.     var $image_text_padding_y;
  1543.  
  1544.     /**
  1545.      * Sets the text alignment
  1546.      *
  1547.      * Value is a string, which can be either 'L', 'C' or 'R'
  1548.      *
  1549.      * Default value is 'C'
  1550.      *
  1551.      * This setting is relevant only if the text has several lines.
  1552.      *
  1553.      * @access public
  1554.      * @var string; 
  1555.      */
  1556.     var $image_text_alignment;
  1557.  
  1558.     /**
  1559.      * Sets the text line spacing
  1560.      *
  1561.      * Value is an integer, in pixels
  1562.      *
  1563.      * Default value is 0
  1564.      *
  1565.      * This setting is relevant only if the text has several lines.
  1566.      *
  1567.      * @access public
  1568.      * @var integer 
  1569.      */
  1570.  
  1571.     /**
  1572.      * Sets the height of the reflection
  1573.      *
  1574.      * Value is an integer in pixels, or a string which format can be in pixels or percentage.
  1575.      * For instance, values can be : 40, '40', '40px' or '40%'
  1576.      *
  1577.      * Default value is null, no reflection
  1578.      *
  1579.      * @access public
  1580.      * @var mixed; 
  1581.      */
  1582.  
  1583.     /**
  1584.      * Sets the space between the source image and its relection
  1585.      *
  1586.      * Value is an integer in pixels, which can be negative
  1587.      *
  1588.      * Default value is 2
  1589.      *
  1590.      * This setting is relevant only if {@link image_reflection_height} is set
  1591.      *
  1592.      * @access public
  1593.      * @var integer 
  1594.      */
  1595.  
  1596.     /**
  1597.      * Sets the color of the reflection background (deprecated)
  1598.      *
  1599.      * Value is an hexadecimal color, such as #FFFFFF
  1600.      *
  1601.      * Default value is #FFFFFF
  1602.      *
  1603.      * This setting is relevant only if {@link image_reflection_height} is set
  1604.      *
  1605.      * This setting is now deprecated in favor of {@link image_default_color}
  1606.      *
  1607.      * @access public
  1608.      * @var string; 
  1609.      */
  1610.  
  1611.     /**
  1612.      * Sets the initial opacity of the reflection
  1613.      *
  1614.      * Value is an integer between 0 (no opacity) and 100 (full opacity).
  1615.      * The reflection will start from {@link image_reflection_opacity} and end up at 0
  1616.      *
  1617.      * Default value is 60
  1618.      *
  1619.      * This setting is relevant only if {@link image_reflection_height} is set
  1620.      *
  1621.      * @access public
  1622.      * @var integer 
  1623.      */
  1624.  
  1625.     /**
  1626.      * Flips the image vertically or horizontally
  1627.      *
  1628.      * Value is either 'h' or 'v', as in horizontal and vertical
  1629.      *
  1630.      * Default value is null (no flip)
  1631.      *
  1632.      * @access public
  1633.      * @var string; 
  1634.      */
  1635.     var $image_flip;
  1636.  
  1637.     /**
  1638.      * Rotates the image by increments of 45 degrees
  1639.      *
  1640.      * Value is either 90, 180 or 270
  1641.      *
  1642.      * Default value is null (no rotation)
  1643.      *
  1644.      * @access public
  1645.      * @var string; 
  1646.      */
  1647.     var $image_rotate;
  1648.  
  1649.     /**
  1650.      * Crops an image
  1651.      *
  1652.      * Values are four dimensions, or two, or one (CSS style)
  1653.      * They represent the amount cropped top, right, bottom and left.
  1654.      * These values can either be in an array, or a space separated string.
  1655.      * Each value can be in pixels (with or without 'px'), or percentage (of the source image)
  1656.      *
  1657.      * For instance, are valid:
  1658.      * <pre>
  1659.      * $foo->image_crop = 20                  OR array(20);
  1660.      * $foo->image_crop = '20px'              OR array('20px');
  1661.      * $foo->image_crop = '20 40'             OR array('20', 40);
  1662.      * $foo->image_crop = '-20 25%'           OR array(-20, '25%');
  1663.      * $foo->image_crop = '20px 25%'          OR array('20px', '25%');
  1664.      * $foo->image_crop = '20% 25%'           OR array('20%', '25%');
  1665.      * $foo->image_crop = '20% 25% 10% 30%'   OR array('20%', '25%', '10%', '30%');
  1666.      * $foo->image_crop = '20px 25px 2px 2px' OR array('20px', '25%px', '2px', '2px');
  1667.      * $foo->image_crop = '20 25% 40px 10%'   OR array(20, '25%', '40px', '10%');
  1668.      * </pre>
  1669.      *
  1670.      * If a value is negative, the image will be expanded, and the extra parts will be filled with black
  1671.      *
  1672.      * Default value is null (no cropping)
  1673.      *
  1674.      * @access public
  1675.      * @var string OR array;
  1676.      */
  1677.     var $image_crop;
  1678.  
  1679.     /**
  1680.      * Crops an image, before an eventual resizing
  1681.      *
  1682.      * See {@link image_crop} for valid formats
  1683.      *
  1684.      * Default value is null (no cropping)
  1685.      *
  1686.      * @access public
  1687.      * @var string OR array;
  1688.      */
  1689.     var $image_precrop;
  1690.  
  1691.     /**
  1692.      * Adds a bevel border on the image
  1693.      *
  1694.      * Value is a positive integer, representing the thickness of the bevel
  1695.      *
  1696.      * If the bevel colors are the same as the background, it makes a fade out effect
  1697.      *
  1698.      * Default value is null (no bevel)
  1699.      *
  1700.      * @access public
  1701.      * @var integer 
  1702.      */
  1703.     var $image_bevel;
  1704.  
  1705.     /**
  1706.      * Top and left bevel color
  1707.      *
  1708.      * Value is a color, in hexadecimal format
  1709.      * This setting is used only if {@link image_bevel} is set
  1710.      *
  1711.      * Default value is #FFFFFF
  1712.      *
  1713.      * @access public
  1714.      * @var string; 
  1715.      */
  1716.     var $image_bevel_color1;
  1717.  
  1718.     /**
  1719.      * Right and bottom bevel color
  1720.      *
  1721.      * Value is a color, in hexadecimal format
  1722.      * This setting is used only if {@link image_bevel} is set
  1723.      *
  1724.      * Default value is #000000
  1725.      *
  1726.      * @access public
  1727.      * @var string; 
  1728.      */
  1729.     var $image_bevel_color2;
  1730.  
  1731.     /**
  1732.      * Adds a single-color border on the outer of the image
  1733.      *
  1734.      * Values are four dimensions, or two, or one (CSS style)
  1735.      * They represent the border thickness top, right, bottom and left.
  1736.      * These values can either be in an array, or a space separated string.
  1737.      * Each value can be in pixels (with or without 'px'), or percentage (of the source image)
  1738.      *
  1739.      * See {@link image_crop} for valid formats
  1740.      *
  1741.      * If a value is negative, the image will be cropped.
  1742.      * Note that the dimensions of the picture will be increased by the borders' thickness
  1743.      *
  1744.      * Default value is null (no border)
  1745.      *
  1746.      * @access public
  1747.      * @var integer 
  1748.      */
  1749.     var $image_border;
  1750.  
  1751.     /**
  1752.      * Border color
  1753.      *
  1754.      * Value is a color, in hexadecimal format.
  1755.      * This setting is used only if {@link image_border} is set
  1756.      *
  1757.      * Default value is #FFFFFF
  1758.      *
  1759.      * @access public
  1760.      * @var string; 
  1761.      */
  1762.     var $image_border_color;
  1763.  
  1764.     /**
  1765.      * Adds a multi-color frame on the outer of the image
  1766.      *
  1767.      * Value is an integer. Two values are possible for now:
  1768.      * 1 for flat border, meaning that the frame is mirrored horizontally and vertically
  1769.      * 2 for crossed border, meaning that the frame will be inversed, as in a bevel effect
  1770.      *
  1771.      * The frame will be composed of colored lines set in {@link image_frame_colors}
  1772.      *
  1773.      * Note that the dimensions of the picture will be increased by the borders' thickness
  1774.      *
  1775.      * Default value is null (no frame)
  1776.      *
  1777.      * @access public
  1778.      * @var integer 
  1779.      */
  1780.     var $image_frame;
  1781.  
  1782.     /**
  1783.      * Sets the colors used to draw a frame
  1784.      *
  1785.      * Values is a list of n colors in hexadecimal format.
  1786.      * These values can either be in an array, or a space separated string.
  1787.      *
  1788.      * The colors are listed in the following order: from the outset of the image to its center
  1789.      *
  1790.      * For instance, are valid:
  1791.      * <pre>
  1792.      * $foo->image_frame_colors = '#FFFFFF #999999 #666666 #000000';
  1793.      * $foo->image_frame_colors = array('#FFFFFF', '#999999', '#666666', '#000000');
  1794.      * </pre>
  1795.      *
  1796.      * This setting is used only if {@link image_frame} is set
  1797.      *
  1798.      * Default value is '#FFFFFF #999999 #666666 #000000'
  1799.      *
  1800.      * @access public
  1801.      * @var string OR array;
  1802.      */
  1803.     var $image_frame_colors;
  1804.  
  1805.     /**
  1806.      * Adds a watermark on the image
  1807.      *
  1808.      * Value is a local image filename, relative or absolute. GIF, JPG, BMP and PNG are supported, as well as PNG alpha.
  1809.      *
  1810.      * If set, this setting allow the use of all other settings starting with image_watermark_
  1811.      *
  1812.      * Default value is null
  1813.      *
  1814.      * @access public
  1815.      * @var string; 
  1816.      */
  1817.     var $image_watermark;
  1818.  
  1819.     /**
  1820.      * Sets the watermarkposition within the image
  1821.      *
  1822.      * Value is one or two out of 'TBLR' (top, bottom, left, right)
  1823.      *
  1824.      * The positions are as following:   TL  T  TR
  1825.      *                                   L       R
  1826.      *                                   BL  B  BR
  1827.      *
  1828.      * Default value is null (centered, horizontal and vertical)
  1829.      *
  1830.      * Note that is {@link image_watermark_x} and {@link image_watermark_y} are used, this setting has no effect
  1831.      *
  1832.      * @access public
  1833.      * @var string; 
  1834.      */
  1835.  
  1836.     /**
  1837.      * Sets the watermark absolute X position within the image
  1838.      *
  1839.      * Value is in pixels, representing the distance between the top of the image and the watermark
  1840.      * If a negative value is used, it will represent the distance between the bottom of the image and the watermark
  1841.      *
  1842.      * Default value is null (so {@link image_watermark_position} is used)
  1843.      *
  1844.      * @access public
  1845.      * @var integer 
  1846.      */
  1847.     var $image_watermark_x;
  1848.  
  1849.     /**
  1850.      * Sets the twatermark absolute Y position within the image
  1851.      *
  1852.      * Value is in pixels, representing the distance between the left of the image and the watermark
  1853.      * If a negative value is used, it will represent the distance between the right of the image and the watermark
  1854.      *
  1855.      * Default value is null (so {@link image_watermark_position} is used)
  1856.      *
  1857.      * @access public
  1858.      * @var integer 
  1859.      */
  1860.     var $image_watermark_y;
  1861.  
  1862.     /**
  1863.      * Allowed MIME types
  1864.      *
  1865.      * Default is a selection of safe mime-types, but you might want to change it
  1866.      *
  1867.      * Simple wildcards are allowed, such as image/* or application/*
  1868.      *
  1869.      * @access public
  1870.      * @var array 
  1871.      */
  1872.     var $allowed;
  1873.  
  1874.     /**
  1875.      * Forbidden MIME types
  1876.      *
  1877.      * Default is a selection of safe mime-types, but you might want to change it
  1878.      * To only check for forbidden MIME types, and allow everything else, set {@link allowed} to array('* / *') without the spaces
  1879.      *
  1880.      * Simple wildcards are allowed, such as image/* or application/*
  1881.      *
  1882.      * @access public
  1883.      * @var array 
  1884.      */
  1885.     var $forbidden;
  1886.  
  1887.     /**
  1888.      * Array of translated error messages
  1889.      *
  1890.      * By default, the language is english (en_GB)
  1891.      * Translations can be in separate files, in a lang/ subdirectory
  1892.      *
  1893.      * @access public
  1894.      * @var array 
  1895.      */
  1896.     var $translation;
  1897.  
  1898.     /**
  1899.      * Language selected for the translations
  1900.      *
  1901.      * By default, the language is english ("en_GB")
  1902.      *
  1903.      * @access public
  1904.      * @var array 
  1905.      */
  1906.     var $language;
  1907.  
  1908.     /**
  1909.      * Init or re-init all the processing variables to their default values
  1910.      *
  1911.      * This function is called in the constructor, and after each call of {@link process}
  1912.      *
  1913.      * @access private
  1914.      */
  1915.     function init({
  1916.  
  1917.         // overiddable variables
  1918.         $this->file_new_name_body       = '';       // replace the name body
  1919.         $this->file_name_body_add       = '';       // append to the name body
  1920.         $this->file_name_body_pre       = '';       // prepend to the name body
  1921.         $this->file_new_name_ext        = '';       // replace the file extension
  1922.         $this->file_safe_name           = true;     // format safely the filename
  1923.         $this->file_overwrite           = false;    // allows overwritting if the file already exists
  1924.         $this->file_auto_rename         = true;     // auto-rename if the file already exists
  1925.         $this->dir_auto_create          = true;     // auto-creates directory if missing
  1926.         $this->dir_auto_chmod           = true;     // auto-chmod directory if not writeable
  1927.         $this->dir_chmod                = 0777;     // default chmod to use
  1928.  
  1929.         $this->mime_check               = true;     // checks the mime type against the allowed list
  1930.         $this->mime_fileinfo            = true;     // MIME detection with Fileinfo PECL extension
  1931.         $this->mime_file                = true;     // MIME detection with UNIX file() command
  1932.         $this->mime_magic               = true;     // MIME detection with mime_magic (mime_content_type())
  1933.         $this->mime_getimagesize        = true;     // MIME detection with getimagesize()
  1934.         $this->no_script                = true;     // turns scripts into test files
  1935.  
  1936.         $val trim(ini_get('upload_max_filesize'));
  1937.         $last strtolower($val{strlen($val)-1});
  1938.         switch($last{
  1939.             case 'g':
  1940.                 $val *= 1024;
  1941.             case 'm':
  1942.                 $val *= 1024;
  1943.             case 'k':
  1944.                 $val *= 1024;
  1945.         }
  1946.         $this->file_max_size = $val;
  1947.  
  1948.         $this->image_resize             = false;    // resize the image
  1949.         $this->image_convert            = '';       // convert. values :''; 'png'; 'jpeg'; 'gif'; 'bmp'
  1950.  
  1951.         $this->image_x                  = 150;
  1952.         $this->image_y                  = 150;
  1953.         $this->image_ratio              = false;    // keeps aspect ratio with x and y dimensions
  1954.         $this->image_ratio_crop         = false;    // keeps aspect ratio with x and y dimensions, filling the space
  1955.         $this->image_ratio_fill         = false;    // keeps aspect ratio with x and y dimensions, fitting the image in the space, and coloring the rest
  1956.         $this->image_ratio_pixels       = false;    // keeps aspect ratio, calculating x and y so that the image is approx the set number of pixels
  1957.         $this->image_ratio_no_zoom_in   = false;
  1958.         $this->image_ratio_no_zoom_out  = false;
  1959.         $this->image_ratio_x            = false;    // calculate the $image_x if true
  1960.         $this->image_ratio_y            = false;    // calculate the $image_y if true
  1961.         $this->jpeg_quality             = 85;
  1962.         $this->jpeg_size                = null;
  1963.         $this->preserve_transparency    = false;
  1964.         $this->image_is_transparent     = false;
  1965.         $this->image_transparent_color  = null;
  1966.         $this->image_background_color   = null;
  1967.         $this->image_default_color      = '#ffffff';
  1968.         $this->image_is_palette         = false;
  1969.  
  1970.         $this->image_max_width          = null;
  1971.         $this->image_max_height         = null;
  1972.         $this->image_max_pixels         = null;
  1973.         $this->image_max_ratio          = null;
  1974.         $this->image_min_width          = null;
  1975.         $this->image_min_height         = null;
  1976.         $this->image_min_pixels         = null;
  1977.         $this->image_min_ratio          = null;
  1978.  
  1979.         $this->image_brightness         = null;
  1980.         $this->image_contrast           = null;
  1981.         $this->image_threshold          = null;
  1982.         $this->image_tint_color         = null;
  1983.         $this->image_overlay_color      = null;
  1984.         $this->image_overlay_percent    = null;
  1985.         $this->image_negative           = false;
  1986.         $this->image_greyscale          = false;
  1987.  
  1988.         $this->image_text               = null;
  1989.         $this->image_text_direction     = null;
  1990.         $this->image_text_color         = '#FFFFFF';
  1991.         $this->image_text_percent       = 100;
  1992.         $this->image_text_background    = null;
  1993.         $this->image_text_background_percent = 100;
  1994.         $this->image_text_font          = 5;
  1995.         $this->image_text_x             = null;
  1996.         $this->image_text_y             = null;
  1997.         $this->image_text_position      = null;
  1998.         $this->image_text_padding       = 0;
  1999.         $this->image_text_padding_x     = null;
  2000.         $this->image_text_padding_y     = null;
  2001.         $this->image_text_alignment     = 'C';
  2002.         $this->image_text_line_spacing  = 0;
  2003.  
  2004.         $this->image_reflection_height  = null;
  2005.         $this->image_reflection_space   = 2;
  2006.         $this->image_reflection_color   = '#ffffff';
  2007.         $this->image_reflection_opacity = 60;
  2008.  
  2009.         $this->image_watermark          = null;
  2010.         $this->image_watermark_x        = null;
  2011.         $this->image_watermark_y        = null;
  2012.         $this->image_watermark_position = null;
  2013.  
  2014.         $this->image_flip               = null;
  2015.         $this->image_rotate             = null;
  2016.         $this->image_crop               = null;
  2017.         $this->image_precrop            = null;
  2018.  
  2019.         $this->image_bevel              = null;
  2020.         $this->image_bevel_color1       = '#FFFFFF';
  2021.         $this->image_bevel_color2       = '#000000';
  2022.         $this->image_border             = null;
  2023.         $this->image_border_color       = '#FFFFFF';
  2024.         $this->image_frame              = null;
  2025.         $this->image_frame_colors       = '#FFFFFF #999999 #666666 #000000';
  2026.  
  2027.         $this->forbidden = array();
  2028.         $this->allowed = array("application/rar",
  2029.                                "application/x-rar-compressed",
  2030.                                "application/arj",
  2031.                                "application/excel",
  2032.                                "application/gnutar",
  2033.                                "application/octet-stream",
  2034.                                "application/pdf",
  2035.                                "application/powerpoint",
  2036.                                "application/postscript",
  2037.                                "application/plain",
  2038.                                "application/rtf",
  2039.                                "application/vocaltec-media-file",
  2040.                                "application/wordperfect",
  2041.                                "application/x-bzip",
  2042.                                "application/x-bzip2",
  2043.                                "application/x-compressed",
  2044.                                "application/x-excel",
  2045.                                "application/x-gzip",
  2046.                                "application/x-latex",
  2047.                                "application/x-midi",
  2048.                                "application/x-msexcel",
  2049.                                "application/x-rtf",
  2050.                                "application/x-sit",
  2051.                                "application/x-stuffit",
  2052.                                "application/x-shockwave-flash",
  2053.                                "application/x-troff-msvideo",
  2054.                                "application/x-zip-compressed",
  2055.                                "application/xml",
  2056.                                "application/zip",
  2057.                                "application/msword",
  2058.                                "application/mspowerpoint",
  2059.                                "application/vnd.ms-excel",
  2060.                                "application/vnd.ms-powerpoint",
  2061.                                "application/vnd.ms-word",
  2062.                                "application/vnd.ms-word.document.macroEnabled.12",
  2063.                                "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
  2064.                                "application/vnd.ms-word.template.macroEnabled.12",
  2065.                                "application/vnd.openxmlformats-officedocument.wordprocessingml.template",
  2066.                                "application/vnd.ms-powerpoint.template.macroEnabled.12",
  2067.                                "application/vnd.openxmlformats-officedocument.presentationml.template",
  2068.                                "application/vnd.ms-powerpoint.addin.macroEnabled.12",
  2069.                                "application/vnd.ms-powerpoint.slideshow.macroEnabled.12",
  2070.                                "application/vnd.openxmlformats-officedocument.presentationml.slideshow",
  2071.                                "application/vnd.ms-powerpoint.presentation.macroEnabled.12",
  2072.                                "application/vnd.openxmlformats-officedocument.presentationml.presentation",
  2073.                                "application/vnd.ms-excel.addin.macroEnabled.12",
  2074.                                "application/vnd.ms-excel.sheet.binary.macroEnabled.12",
  2075.                                "application/vnd.ms-excel.sheet.macroEnabled.12",
  2076.                                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
  2077.                                "application/vnd.ms-excel.template.macroEnabled.12",
  2078.                                "application/vnd.openxmlformats-officedocument.spreadsheetml.template",
  2079.                                "audio/*",
  2080.                                "image/*",
  2081.                                "video/*",
  2082.                                "multipart/x-zip",
  2083.                                "multipart/x-gzip",
  2084.                                "text/richtext",
  2085.                                "text/plain",
  2086.                                "text/xml");
  2087.  
  2088.     }
  2089.  
  2090.     /**
  2091.      * Constructor. Checks if the file has been uploaded
  2092.      *
  2093.      * The constructor takes $_FILES['form_field'] array as argument
  2094.      * where form_field is the form field name
  2095.      *
  2096.      * The constructor will check if the file has been uploaded in its temporary location, and
  2097.      * accordingly will set {@link uploaded} (and {@link error} is an error occurred)
  2098.      *
  2099.      * If the file has been uploaded, the constructor will populate all the variables holding the upload
  2100.      * information (none of the processing class variables are used here).
  2101.      * You can have access to information about the file (name, size, MIME type...).
  2102.      *
  2103.      *
  2104.      * Alternatively, you can set the first argument to be a local filename (string)
  2105.      * This allows processing of a local file, as if the file was uploaded
  2106.      *
  2107.      * The optional second argument allows you to set the language for the error messages
  2108.      *
  2109.      * @access private
  2110.      * @param  array  $file $_FILES['form_field']
  2111.      *     or   string $file Local filename
  2112.      * @param  string $lang Optional language code
  2113.      */
  2114.     function upload($file$lang 'en_GB'{
  2115.  
  2116.         $this->version            = '0.28';
  2117.  
  2118.         $this->file_src_name      = '';
  2119.         $this->file_src_name_body = '';
  2120.         $this->file_src_name_ext  = '';
  2121.         $this->file_src_mime      = '';
  2122.         $this->file_src_size      = '';
  2123.         $this->file_src_error     = '';
  2124.         $this->file_src_pathname  '';
  2125.         $this->file_src_temp      '';
  2126.  
  2127.         $this->file_dst_path      '';
  2128.         $this->file_dst_name      = '';
  2129.         $this->file_dst_name_body = '';
  2130.         $this->file_dst_name_ext  = '';
  2131.         $this->file_dst_pathname  '';
  2132.  
  2133.         $this->image_src_x        null;
  2134.         $this->image_src_y        null;
  2135.         $this->image_src_bits     null;
  2136.         $this->image_src_type     null;
  2137.         $this->image_src_pixels   null;
  2138.         $this->image_dst_x        0;
  2139.         $this->image_dst_y        0;
  2140.  
  2141.         $this->uploaded           = true;
  2142.         $this->no_upload_check    = false;
  2143.         $this->processed          = true;
  2144.         $this->error              = '';
  2145.         $this->log                = '';
  2146.         $this->allowed            = array();
  2147.         $this->forbidden          = array();
  2148.         $this->file_is_image      false;
  2149.         $this->init();
  2150.         $info                     null;
  2151.         $mime_from_browser        null;
  2152.  
  2153.         // sets default language
  2154.         $this->translation        = array();
  2155.         $this->translation['file_error']                  'File error. Please try again.';
  2156.         $this->translation['local_file_missing']          'Local file doesn\'t exist.';
  2157.         $this->translation['local_file_not_readable']     'Local file is not readable.';
  2158.         $this->translation['uploaded_too_big_ini']        'File upload error (the uploaded file exceeds the upload_max_filesize directive in php.ini).';
  2159.         $this->translation['uploaded_too_big_html']       'File upload error (the uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the html form).';
  2160.         $this->translation['uploaded_partial']            'File upload error (the uploaded file was only partially uploaded).';
  2161.         $this->translation['uploaded_missing']            'File upload error (no file was uploaded).';
  2162.         $this->translation['uploaded_no_tmp_dir']         'File upload error (missing a temporary folder).';
  2163.         $this->translation['uploaded_cant_write']         'File upload error (failed to write file to disk).';
  2164.         $this->translation['uploaded_err_extension']      'File upload error (file upload stopped by extension).';
  2165.         $this->translation['uploaded_unknown']            'File upload error (unknown error code).';
  2166.         $this->translation['try_again']                   'File upload error. Please try again.';
  2167.         $this->translation['file_too_big']                'File too big.';
  2168.         $this->translation['no_mime']                     'MIME type can\'t be detected.';
  2169.         $this->translation['incorrect_file']              'Incorrect type of file.';
  2170.         $this->translation['image_too_wide']              'Image too wide.';
  2171.         $this->translation['image_too_narrow']            'Image too narrow.';
  2172.         $this->translation['image_too_high']              'Image too high.';
  2173.         $this->translation['image_too_short']             'Image too short.';
  2174.         $this->translation['ratio_too_high']              'Image ratio too high (image too wide).';
  2175.         $this->translation['ratio_too_low']               'Image ratio too low (image too high).';
  2176.         $this->translation['too_many_pixels']             'Image has too many pixels.';
  2177.         $this->translation['not_enough_pixels']           'Image has not enough pixels.';
  2178.         $this->translation['file_not_uploaded']           'File not uploaded. Can\'t carry on a process.';
  2179.         $this->translation['already_exists']              '%s already exists. Please change the file name.';
  2180.         $this->translation['temp_file_missing']           'No correct temp source file. Can\'t carry on a process.';
  2181.         $this->translation['source_missing']              'No correct uploaded source file. Can\'t carry on a process.';
  2182.         $this->translation['destination_dir']             'Destination directory can\'t be created. Can\'t carry on a process.';
  2183.         $this->translation['destination_dir_missing']     'Destination directory doesn\'t exist. Can\'t carry on a process.';
  2184.         $this->translation['destination_path_not_dir']    'Destination path is not a directory. Can\'t carry on a process.';
  2185.         $this->translation['destination_dir_write']       'Destination directory can\'t be made writeable. Can\'t carry on a process.';
  2186.         $this->translation['destination_path_write']      'Destination path is not a writeable. Can\'t carry on a process.';
  2187.         $this->translation['temp_file']                   'Can\'t create the temporary file. Can\'t carry on a process.';
  2188.         $this->translation['source_not_readable']         'Source file is not readable. Can\'t carry on a process.';
  2189.         $this->translation['no_create_support']           'No create from %s support.';
  2190.         $this->translation['create_error']                'Error in creating %s image from source.';
  2191.         $this->translation['source_invalid']              'Can\'t read image source. Not an image?.';
  2192.         $this->translation['gd_missing']                  'GD doesn\'t seem to be present.';
  2193.         $this->translation['watermark_no_create_support''No create from %s support, can\'t read watermark.';
  2194.         $this->translation['watermark_create_error']      'No %s read support, can\'t create watermark.';
  2195.         $this->translation['watermark_invalid']           'Unknown image format, can\'t read watermark.';
  2196.         $this->translation['file_create']                 'No %s create support.';
  2197.         $this->translation['no_conversion_type']          'No conversion type defined.';
  2198.         $this->translation['copy_failed']                 'Error copying file on the server. copy() failed.';
  2199.         $this->translation['reading_failed']              'Error reading the file.';
  2200.  
  2201.         // determines the language
  2202.         $this->lang               $lang;
  2203.         if ($this->lang != 'en_GB' && file_exists(dirname(__FILE__).'/lang/class.upload.' $lang '.php')) {
  2204.             $translation null;
  2205.             include(dirname(__FILE__).'/lang/class.upload.' $lang '.php');
  2206.             if (is_array($translation)) {
  2207.                 $this->translation = array_merge($this->translation$translation);
  2208.             else {
  2209.                 $this->lang 'en_GB';
  2210.             }
  2211.         }
  2212.  
  2213.  
  2214.         // determines the supported MIME types, and matching image format
  2215.         $this->image_supported array();
  2216.         if ($this->gdversion()) {
  2217.             if (imagetypes(IMG_GIF{
  2218.                 $this->image_supported['image/gif''gif';
  2219.             }
  2220.             if (imagetypes(IMG_JPG{
  2221.                 $this->image_supported['image/jpg''jpg';
  2222.                 $this->image_supported['image/jpeg''jpg';
  2223.                 $this->image_supported['image/pjpeg''jpg';
  2224.             }
  2225.             if (imagetypes(IMG_PNG{
  2226.                 $this->image_supported['image/png''png';
  2227.                 $this->image_supported['image/x-png''png';
  2228.             }
  2229.             if (imagetypes(IMG_WBMP{
  2230.                 $this->image_supported['image/bmp''bmp';
  2231.                 $this->image_supported['image/x-ms-bmp''bmp';
  2232.                 $this->image_supported['image/x-windows-bmp''bmp';
  2233.             }
  2234.         }
  2235.  
  2236.         // display some system information
  2237.         if (empty($this->log)) {
  2238.             $this->log .= '<b>system information</b><br />';
  2239.             $inis ini_get_all();
  2240.             $open_basedir (array_key_exists('open_basedir'$inis&& array_key_exists('local_value'$inis['open_basedir']&& !empty($inis['open_basedir']['local_value'])) $inis['open_basedir']['local_value'false;
  2241.             $gd           $this->gdversion($this->gdversion(true'GD not present';
  2242.             $supported    trim((in_array('png'$this->image_supported'png' ''' ' (in_array('jpg'$this->image_supported'jpg' ''' ' (in_array('gif'$this->image_supported'gif' ''' ' (in_array('bmp'$this->image_supported'bmp' ''));
  2243.             $this->log .= '-&nbsp;class version           : ' $this->version . '<br />';
  2244.             $this->log .= '-&nbsp;GD version              : ' $gd '<br />';
  2245.             $this->log .= '-&nbsp;supported image types   : ' (!empty($supported$supported 'none''<br />';
  2246.             $this->log .= '-&nbsp;open_basedir            : ' (!empty($open_basedir$open_basedir 'no restriction''<br />';
  2247.             $this->log .= '-&nbsp;language                : ' $this->lang '<br />';
  2248.         }
  2249.  
  2250.         if (!$file{
  2251.             $this->uploaded = false;
  2252.             $this->error = $this->translate('file_error');
  2253.         }
  2254.  
  2255.         // check if we sent a local filename rather than a $_FILE element
  2256.         if (!is_array($file)) {
  2257.             if (empty($file)) {
  2258.                 $this->uploaded = false;
  2259.                 $this->error = $this->translate('file_error');
  2260.             else {
  2261.                 $this->no_upload_check = TRUE;
  2262.                 // this is a local filename, i.e.not uploaded
  2263.                 $this->log .= '<b>' $this->translate("source is a local file"' ' $file '</b><br />';
  2264.  
  2265.                 if ($this->uploaded && !file_exists($file)) {
  2266.                     $this->uploaded = false;
  2267.                     $this->error = $this->translate('local_file_missing');
  2268.                 }
  2269.  
  2270.                 if ($this->uploaded && !is_readable($file)) {
  2271.                     $this->uploaded = false;
  2272.                     $this->error = $this->translate('local_file_not_readable');
  2273.                 }
  2274.  
  2275.                 if ($this->uploaded{
  2276.                     $this->file_src_pathname   $file;
  2277.                     $this->file_src_name       = basename($file);
  2278.                     $this->log .= '- local file name OK<br />';
  2279.                     preg_match('/\.([^\.]*$)/'$this->file_src_name$extension);
  2280.                     if (is_array($extension)) {
  2281.                         $this->file_src_name_ext      = strtolower($extension[1]);
  2282.                         $this->file_src_name_body     = substr($this->file_src_name0((strlen($this->file_src_namestrlen($this->file_src_name_ext)))-1);
  2283.                     else {
  2284.                         $this->file_src_name_ext      = '';
  2285.                         $this->file_src_name_body     = $this->file_src_name;
  2286.                     }
  2287.                     $this->file_src_size = (file_exists($filefilesize($file0);
  2288.                 }
  2289.                 $this->file_src_error = 0;
  2290.             }
  2291.         else {
  2292.             // this is an element from $_FILE, i.e. an uploaded file
  2293.             $this->log .= '<b>source is an uploaded file</b><br />';
  2294.             if ($this->uploaded{
  2295.                 $this->file_src_error         = trim($file['error']);
  2296.                 switch($this->file_src_error{
  2297.                     case UPLOAD_ERR_OK:
  2298.                         // all is OK
  2299.                         $this->log .= '- upload OK<br />';
  2300.                         break;
  2301.                     case UPLOAD_ERR_INI_SIZE:
  2302.                         $this->uploaded = false;
  2303.                         $this->error = $this->translate('uploaded_too_big_ini');
  2304.                         break;
  2305.                     case UPLOAD_ERR_FORM_SIZE:
  2306.                         $this->uploaded = false;
  2307.                         $this->error = $this->translate('uploaded_too_big_html');
  2308.                         break;
  2309.                     case UPLOAD_ERR_PARTIAL:
  2310.                         $this->uploaded = false;
  2311.                         $this->error = $this->translate('uploaded_partial');
  2312.                         break;
  2313.                     case UPLOAD_ERR_NO_FILE:
  2314.                         $this->uploaded = false;
  2315.                         $this->error = $this->translate('uploaded_missing');
  2316.                         break;
  2317.                     case @UPLOAD_ERR_NO_TMP_DIR:
  2318.                         $this->uploaded = false;
  2319.                         $this->error = $this->translate('uploaded_no_tmp_dir');
  2320.                         break;
  2321.                     case @UPLOAD_ERR_CANT_WRITE:
  2322.                         $this->uploaded = false;
  2323.                         $this->error = $this->translate('uploaded_cant_write');
  2324.                         break;
  2325.                     case @UPLOAD_ERR_EXTENSION:
  2326.                         $this->uploaded = false;
  2327.                         $this->error = $this->translate('uploaded_err_extension');
  2328.                         break;
  2329.                     default:
  2330.                         $this->uploaded = false;
  2331.                         $this->error = $this->translate('uploaded_unknown'' ('.$this->file_src_error.')';
  2332.                 }
  2333.             }
  2334.  
  2335.             if ($this->uploaded{
  2336.                 $this->file_src_pathname   $file['tmp_name'];
  2337.                 $this->file_src_name       = $file['name'];
  2338.                 if ($this->file_src_name == ''{
  2339.                     $this->uploaded = false;
  2340.                     $this->error = $this->translate('try_again');
  2341.                 }
  2342.             }
  2343.  
  2344.             if ($this->uploaded{
  2345.                 $this->log .= '- file name OK<br />';
  2346.                 preg_match('/\.([^\.]*$)/'$this->file_src_name$extension);
  2347.                 if (is_array($extension)) {
  2348.                     $this->file_src_name_ext      = strtolower($extension[1]);
  2349.                     $this->file_src_name_body     = substr($this->file_src_name0((strlen($this->file_src_namestrlen($this->file_src_name_ext)))-1);
  2350.                 else {
  2351.                     $this->file_src_name_ext      = '';
  2352.                     $this->file_src_name_body     = $this->file_src_name;
  2353.                 }
  2354.                 $this->file_src_size = $file['size'];
  2355.                 $mime_from_browser $file['type'];
  2356.             }
  2357.         }
  2358.  
  2359.         if ($this->uploaded{
  2360.             $this->log .= '<b>determining MIME type</b><br />';
  2361.             $this->file_src_mime = null;
  2362.  
  2363.             // checks MIME type with Fileinfo PECL extension
  2364.             if (!$this->file_src_mime || !is_string($this->file_src_mime|| empty($this->file_src_mime|| strpos($this->file_src_mime'/'=== FALSE{
  2365.                 if ($this->mime_fileinfo{
  2366.                     $this->log .= '- Checking MIME type with Fileinfo PECL extension<br />';
  2367.                     if (function_exists('finfo_open')) {
  2368.                         if ($this->mime_fileinfo !== ''{
  2369.                             if ($this->mime_fileinfo === true{
  2370.                                 if (getenv('MAGIC'=== FALSE{
  2371.                                     if (substr(PHP_OS03== 'WIN'{
  2372.                                         $path realpath(ini_get('extension_dir''/../''extras/magic';
  2373.                                     else {
  2374.                                         $path '/usr/share/file/magic';
  2375.                                     }
  2376.                                     $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;MAGIC path defaults to ' $path '<br />';
  2377.                                 else {
  2378.                                     $path getenv('MAGIC');
  2379.                                     $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;MAGIC path is set to ' $path ' from MAGIC variable<br />';
  2380.                                 }
  2381.                             else {
  2382.                                 $path $this->mime_fileinfo;
  2383.                                 $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;MAGIC path is set to ' $path '<br />';
  2384.                             }
  2385.                             $f @finfo_open(FILEINFO_MIME$path);
  2386.                         else {
  2387.                             $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;MAGIC path will not be used<br />';
  2388.                             $f @finfo_open(FILEINFO_MIME);
  2389.                         }
  2390.                         if (is_resource($f)) {
  2391.                             $mime finfo_file($frealpath($this->file_src_pathname));
  2392.                             finfo_close($f);
  2393.                             $this->file_src_mime = $mime;
  2394.                             $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;MIME type detected as ' $this->file_src_mime . ' by Fileinfo PECL extension<br />';
  2395.                         else {
  2396.                             $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;Fileinfo PECL extension failed (finfo_open)<br />';
  2397.                         }
  2398.                     elseif (class_exists('finfo')) {
  2399.                         $f new finfoFILEINFO_MIME );
  2400.                         if ($f{
  2401.                             $this->file_src_mime = $f->file(realpath($this->file_src_pathname));
  2402.                             $this->log .= '- MIME type detected as ' $this->file_src_mime . ' by Fileinfo PECL extension<br />';
  2403.                         else {
  2404.                             $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;Fileinfo PECL extension failed (finfo)<br />';
  2405.                         }
  2406.                     else {
  2407.                         $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;Fileinfo PECL extension not available<br />';
  2408.                     }
  2409.                 else {
  2410.                     $this->log .= '- Fileinfo PECL extension deactivated<br />';
  2411.                 }
  2412.             }
  2413.  
  2414.             // checks MIME type with shell if unix access is authorized
  2415.             if (!$this->file_src_mime || !is_string($this->file_src_mime|| empty($this->file_src_mime|| strpos($this->file_src_mime'/'=== FALSE{
  2416.                 if ($this->mime_file{
  2417.                     $this->log .= '- Checking MIME type with UNIX file() command<br />';
  2418.                     if (substr(PHP_OS03!= 'WIN'{
  2419.                         if (strlen($mime @exec("file -bi ".escapeshellarg($this->file_src_pathname))) != 0{
  2420.                             $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;MIME returned as ' $mime '<br />';
  2421.                             if (strpos($mime';'!== FALSE{
  2422.                                 $temp split(';'$mime);
  2423.                                 $this->file_src_mime = $temp[0];
  2424.                             else if (strpos($mime' '!== FALSE{
  2425.                                 $temp split(' '$mime);
  2426.                                 $this->file_src_mime = $temp[0];
  2427.                             else {
  2428.                                 $this->file_src_mime = trim($mime);
  2429.                             }
  2430.                             $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;MIME type detected as ' $this->file_src_mime . ' by UNIX file() command<br />';
  2431.                         else {
  2432.                             $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;UNIX file() command failed<br />';
  2433.                         }
  2434.                     else {
  2435.                         $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;UNIX file() command not availabled<br />';
  2436.                     }
  2437.                 else {
  2438.                     $this->log .= '- UNIX file() command is deactivated<br />';
  2439.                 }
  2440.             }
  2441.  
  2442.             // checks MIME type with mime_magic
  2443.             if (!$this->file_src_mime || !is_string($this->file_src_mime|| empty($this->file_src_mime|| strpos($this->file_src_mime'/'=== FALSE{
  2444.                 if ($this->mime_magic{
  2445.                     $this->log .= '- Checking MIME type with mime.magic file (mime_content_type())<br />';
  2446.                     if (function_exists('mime_content_type')) {
  2447.                         $this->file_src_mime = mime_content_type($this->file_src_pathname);
  2448.                         $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;MIME type detected as ' $this->file_src_mime . ' by mime_content_type()<br />';
  2449.                     else {
  2450.                         $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;mime_content_type() is not available<br />';
  2451.                     }
  2452.                 else {
  2453.                     $this->log .= '- mime.magic file (mime_content_type()) is deactivated<br />';
  2454.                 }
  2455.             }
  2456.  
  2457.             // checks MIME type with getimagesize()
  2458.             if (!$this->file_src_mime || !is_string($this->file_src_mime|| empty($this->file_src_mime|| strpos($this->file_src_mime'/'=== FALSE{
  2459.                 if ($this->mime_getimagesize{
  2460.                     $this->log .= '- Checking MIME type with getimagesize()<br />';
  2461.                     $info getimagesize($this->file_src_pathname);
  2462.                     if (is_array($info&& array_key_exists('mime'$info)) {
  2463.                         $this->file_src_mime = trim($info['mime']);
  2464.                         if (empty($this->file_src_mime)) {
  2465.                             $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;MIME empty, guessing from type<br />';
  2466.                             $mime (is_array($info&& array_key_exists(2$info$info[2null)// 1 = GIF, 2 = JPG, 3 = PNG
  2467.                             $this->file_src_mime = ($mime==IMAGETYPE_GIF 'image/gif' ($mime==IMAGETYPE_JPEG 'image/jpeg' ($mime==IMAGETYPE_PNG 'image/png' ($mime==IMAGETYPE_BMP 'image/bmp' null))));
  2468.                         }
  2469.                         $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;MIME type detected as ' $this->file_src_mime . ' by PHP getimagesize() function<br />';
  2470.                     else {
  2471.                         $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;getimagesize() failed<br />';
  2472.                     }
  2473.                 else {
  2474.                     $this->log .= '- getimagesize() is deactivated<br />';
  2475.                 }
  2476.             }
  2477.  
  2478.             // default to MIME from browser (or Flash)
  2479.             if (!empty($mime_from_browser&& !$this->file_src_mime || !is_string($this->file_src_mime|| empty($this->file_src_mime)) {
  2480.                 $this->file_src_mime =$mime_from_browser;
  2481.                 $this->log .= '- MIME type detected as ' $this->file_src_mime . ' by browser<br />';
  2482.             }
  2483.  
  2484.             // we need to work some magic if we upload via Flash
  2485.             if ($this->file_src_mime == 'application/octet-stream' || !$this->file_src_mime || !is_string($this->file_src_mime|| empty($this->file_src_mime|| strpos($this->file_src_mime'/'=== FALSE{
  2486.                 if ($this->file_src_mime == 'application/octet-stream'$this->log .= '- Flash may be rewriting MIME as application/octet-stream<br />';
  2487.                 $this->log .= '- Try to guess MIME type from file extension (' $this->file_src_name_ext . '): ';
  2488.                 switch($this->file_src_name_ext{
  2489.                     case 'jpg':
  2490.                     case 'jpeg':
  2491.                     case 'jpe':
  2492.                         $this->file_src_mime = 'image/jpeg';
  2493.                         break;
  2494.                     case 'gif':
  2495.                         $this->file_src_mime = 'image/gif';
  2496.                         break;
  2497.                     case 'png':
  2498.                         $this->file_src_mime = 'image/png';
  2499.                         break;
  2500.                     case 'bmp':
  2501.                         $this->file_src_mime = 'image/bmp';
  2502.                         break;
  2503.                     case 'flv':
  2504.                         $this->file_src_mime = 'video/x-flv';
  2505.                         break;
  2506.                     case 'js' :
  2507.                         $this->file_src_mime = 'application/x-javascript';
  2508.                         break;
  2509.                     case 'json' :
  2510.                         $this->file_src_mime = 'application/json';
  2511.                         break;
  2512.                     case 'tiff' :
  2513.                         $this->file_src_mime = 'image/tiff';
  2514.                         break;
  2515.                     case 'css' :
  2516.                         $this->file_src_mime = 'text/css';
  2517.                         break;
  2518.                     case 'xml' :
  2519.                         $this->file_src_mime = 'application/xml';
  2520.                         break;
  2521.                     case 'doc' :
  2522.                     case 'docx' :
  2523.                         $this->file_src_mime = 'application/msword';
  2524.                         break;
  2525.                     case 'xls' :
  2526.                     case 'xlt' :
  2527.                     case 'xlm' :
  2528.                     case 'xld' :
  2529.                     case 'xla' :
  2530.                     case 'xlc' :
  2531.                     case 'xlw' :
  2532.                     case 'xll' :
  2533.                         $this->file_src_mime = 'application/vnd.ms-excel';
  2534.                         break;
  2535.                     case 'ppt' :
  2536.                     case 'pps' :
  2537.                         $this->file_src_mime = 'application/vnd.ms-powerpoint';
  2538.                         break;
  2539.                     case 'rtf' :
  2540.                         $this->file_src_mime = 'application/rtf';
  2541.                         break;
  2542.                     case 'pdf' :
  2543.                         $this->file_src_mime = 'application/pdf';
  2544.                         break;
  2545.                     case 'html' :
  2546.                     case 'htm' :
  2547.                     case 'php' :
  2548.                         $this->file_src_mime = 'text/html';
  2549.                         break;
  2550.                     case 'txt' :
  2551.                         $this->file_src_mime = 'text/plain';
  2552.                         break;
  2553.                     case 'mpeg' :
  2554.                     case 'mpg' :
  2555.                     case 'mpe' :
  2556.                         $this->file_src_mime = 'video/mpeg';
  2557.                         break;
  2558.                     case 'mp3' :
  2559.                         $this->file_src_mime = 'audio/mpeg3';
  2560.                         break;
  2561.                     case 'wav' :
  2562.                         $this->file_src_mime = 'audio/wav';
  2563.                         break;
  2564.                     case 'aiff' :
  2565.                     case 'aif' :
  2566.                         $this->file_src_mime = 'audio/aiff';
  2567.                         break;
  2568.                     case 'avi' :
  2569.                         $this->file_src_mime = 'video/msvideo';
  2570.                         break;
  2571.                     case 'wmv' :
  2572.                         $this->file_src_mime = 'video/x-ms-wmv';
  2573.                         break;
  2574.                     case 'mov' :
  2575.                         $this->file_src_mime = 'video/quicktime';
  2576.                         break;
  2577.                     case 'zip' :
  2578.                         $this->file_src_mime = 'application/zip';
  2579.                         break;
  2580.                     case 'tar' :
  2581.                         $this->file_src_mime = 'application/x-tar';
  2582.                         break;
  2583.                     case 'swf' :
  2584.                         $this->file_src_mime = 'application/x-shockwave-flash';
  2585.                         break;
  2586.                 }
  2587.                 if ($this->file_src_mime == 'application/octet-stream'{
  2588.                     $this->log .= 'doesn\'t look like anything known<br />';
  2589.                 else {
  2590.                     $this->log .= 'MIME type set to ' $this->file_src_mime . '<br />';
  2591.                 }
  2592.             }
  2593.  
  2594.             if (!$this->file_src_mime || !is_string($this->file_src_mime|| empty($this->file_src_mime|| strpos($this->file_src_mime'/'=== FALSE{
  2595.                 $this->log .= '- MIME type couldn\'t be detected! (' . (string) $this->file_src_mime . ')<br />';
  2596.             }
  2597.  
  2598.             // determine whether the file is an image
  2599.             if ($this->file_src_mime && is_string($this->file_src_mime&& !empty($this->file_src_mime&& array_key_exists($this->file_src_mime$this->image_supported)) {
  2600.                 $this->file_is_image true;
  2601.                 $this->image_src_type $this->image_supported[$this->file_src_mime];
  2602.             }
  2603.  
  2604.             // if the file is an image, we gather some useful data
  2605.             if ($this->file_is_image{
  2606.                 $info @getimagesize($this->file_src_pathname);
  2607.                 if (is_array($info)) {
  2608.                     $this->image_src_x    $info[0];
  2609.                     $this->image_src_y    $info[1];
  2610.                     $this->image_src_pixels $this->image_src_x $this->image_src_y;
  2611.                     $this->image_src_bits array_key_exists('bits'$info$info['bits'null;
  2612.                 else {
  2613.                     $this->log .= '- can\'t retrieve image information. open_basedir restriction in place?<br />';
  2614.                 }
  2615.             }
  2616.  
  2617.             $this->log .= '<b>source variables</b><br />';
  2618.             $this->log .= '- You can use all these before calling process()<br />';
  2619.             $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_src_name         : ' $this->file_src_name . '<br />';
  2620.             $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_src_name_body    : ' $this->file_src_name_body . '<br />';
  2621.             $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_src_name_ext     : ' $this->file_src_name_ext . '<br />';
  2622.             $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_src_pathname     : ' $this->file_src_pathname '<br />';
  2623.             $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_src_mime         : ' $this->file_src_mime . '<br />';
  2624.             $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_src_size         : ' $this->file_src_size . ' (max= ' $this->file_max_size . ')<br />';
  2625.             $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_src_error        : ' $this->file_src_error . '<br />';
  2626.  
  2627.             if ($this->file_is_image{
  2628.                 $this->log .= '- source file is an image<br />';
  2629.                 $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;image_src_x           : ' $this->image_src_x '<br />';
  2630.                 $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;image_src_y           : ' $this->image_src_y '<br />';
  2631.                 $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;image_src_pixels      : ' $this->image_src_pixels '<br />';
  2632.                 $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;image_src_type        : ' $this->image_src_type '<br />';
  2633.                 $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;image_src_bits        : ' $this->image_src_bits '<br />';
  2634.             }
  2635.         }
  2636.  
  2637.     }
  2638.  
  2639.     /**
  2640.      * Returns the version of GD
  2641.      *
  2642.      * @access public
  2643.      * @param  boolean  $full Optional flag to get precise version
  2644.      * @return float GD version
  2645.      */
  2646.     function gdversion($full false{
  2647.         static $gd_version null;
  2648.         static $gd_full_version null;
  2649.         if ($gd_version === null{
  2650.             if (function_exists('gd_info')) {
  2651.                 $gd gd_info();
  2652.                 $gd $gd["GD Version"];
  2653.                 $regex "/([\d\.]+)/i";
  2654.             else {
  2655.                 ob_start();
  2656.                 phpinfo(8);
  2657.                 $gd ob_get_contents();
  2658.                 ob_end_clean();
  2659.                 $regex "/\bgd\s+version\b[^\d\n\r]+?([\d\.]+)/i";
  2660.             }
  2661.             if (preg_match($regex$gd$m)) {
  2662.                 $gd_full_version = (string) $m[1];
  2663.                 $gd_version = (float) $m[1];
  2664.             else {
  2665.                 $gd_full_version 'none';
  2666.                 $gd_version 0;
  2667.             }
  2668.         }
  2669.         if ($full{
  2670.             return $gd_full_version;
  2671.         else {
  2672.             return $gd_version;
  2673.         }
  2674.     }
  2675.  
  2676.  
  2677.     /**
  2678.      * Creates directories recursively
  2679.      *
  2680.      * @access private
  2681.      * @param  string  $path Path to create
  2682.      * @param  integer $mode Optional permissions
  2683.      * @return boolean Success
  2684.      */
  2685.     function rmkdir($path$mode 0777{
  2686.         return is_dir($path|| $this->rmkdir(dirname($path)$mode&& $this->_mkdir($path$mode) );
  2687.     }
  2688.  
  2689.  
  2690.     /**
  2691.      * Creates directory
  2692.      *
  2693.      * @access private
  2694.      * @param  string  $path Path to create
  2695.      * @param  integer $mode Optional permissions
  2696.      * @return boolean Success
  2697.      */
  2698.     function _mkdir($path$mode 0777{
  2699.         $old umask(0);
  2700.         $res @mkdir($path$mode);
  2701.         umask($old);
  2702.         return $res;
  2703.     }
  2704.  
  2705.  
  2706.     /**
  2707.      * Translate error messages
  2708.      *
  2709.      * @access private
  2710.      * @param  string  $str    Message to translate
  2711.      * @param  array   $tokens Optional token values
  2712.      * @return string Translated string
  2713.      */
  2714.     function translate($str$tokens array()) {
  2715.         if (array_key_exists($str$this->translation)) $str $this->translation[$str];
  2716.         if (is_array($tokens&& sizeof($tokens0)   $str vsprintf($str$tokens);
  2717.         return $str;
  2718.     }
  2719.  
  2720.     /**
  2721.      * Decodes colors
  2722.      *
  2723.      * @access private
  2724.      * @param  string  $color  Color string
  2725.      * @return array RGB colors
  2726.      */
  2727.     function getcolors($color{
  2728.         $r sscanf($color"#%2x%2x%2x");
  2729.         $red   (array_key_exists(0$r&& is_numeric($r[0]$r[00);
  2730.         $green (array_key_exists(1$r&& is_numeric($r[1]$r[10);
  2731.         $blue  (array_key_exists(2$r&& is_numeric($r[2]$r[20);
  2732.         return array($red$green$blue);
  2733.     }
  2734.  
  2735.     /**
  2736.      * Creates a container image
  2737.      *
  2738.      * @access private
  2739.      * @param  integer  $x    Width
  2740.      * @param  integer  $y    Height
  2741.      * @param  boolean  $fill Optional flag to draw the background color or not
  2742.      * @param  boolean  $trsp Optional flag to set the background to be transparent
  2743.      * @return resource Container image
  2744.      */
  2745.     function imagecreatenew($x$y$fill true$trsp false{
  2746.         if ($x 1$x 1if ($y 1$y 1;
  2747.         if ($this->gdversion(>= && !$this->image_is_palette{
  2748.             // create a true color image
  2749.             $dst_im imagecreatetruecolor($x$y);
  2750.             // this preserves transparency in PNGs, in true color
  2751.             if (empty($this->image_background_color|| $trsp{
  2752.                 imagealphablending($dst_imfalse );
  2753.                 imagefilledrectangle($dst_im00$x$yimagecolorallocatealpha($dst_im000127));
  2754.             }
  2755.         else {
  2756.             // creates a palette image
  2757.             $dst_im imagecreate($x$y);
  2758.             // preserves transparency for palette images, if the original image has transparency
  2759.             if (($fill && $this->image_is_transparent && empty($this->image_background_color)) || $trsp{
  2760.                 imagefilledrectangle($dst_im00$x$y$this->image_transparent_color);
  2761.                 imagecolortransparent($dst_im$this->image_transparent_color);
  2762.             }
  2763.         }
  2764.         // fills with background color if any is set
  2765.         if ($fill && !empty($this->image_background_color&& !$trsp{
  2766.             list($red$green$blue$this->getcolors($this->image_background_color);
  2767.             $background_color imagecolorallocate($dst_im$red$green$blue);
  2768.             imagefilledrectangle($dst_im00$x$y$background_color);
  2769.         }
  2770.         return $dst_im;
  2771.     }
  2772.  
  2773.  
  2774.     /**
  2775.      * Transfers an image from the container to the destination image
  2776.      *
  2777.      * @access private
  2778.      * @param  resource $src_im Container image
  2779.      * @param  resource $dst_im Destination image
  2780.      * @return resource Destination image
  2781.      */
  2782.     function imagetransfer($src_im$dst_im{
  2783.         if (is_resource($dst_im)) imagedestroy($dst_im);
  2784.         $dst_im $src_im;
  2785.         return $dst_im;
  2786.     }
  2787.  
  2788.     /**
  2789.      * Merges two images
  2790.      *
  2791.      * If the output format is PNG, then we do it pixel per pixel to retain the alpha channel
  2792.      *
  2793.      * @access private
  2794.      * @param  resource $dst_img Destination image
  2795.      * @param  resource $src_img Overlay image
  2796.      * @param  int      $dst_x   x-coordinate of destination point
  2797.      * @param  int      $dst_y   y-coordinate of destination point
  2798.      * @param  int      $src_x   x-coordinate of source point
  2799.      * @param  int      $src_y   y-coordinate of source point
  2800.      * @param  int      $src_w   Source width
  2801.      * @param  int      $src_h   Source height
  2802.      * @param  int      $pct     Optional percentage of the overlay, between 0 and 100 (default: 100)
  2803.      * @return resource Destination image
  2804.      */
  2805.     function imagecopymergealpha(&$dst_im&$src_im$dst_x$dst_y$src_x$src_y$src_w$src_h$pct 0{
  2806.         $dst_x = (int) $dst_x;
  2807.         $dst_y = (int) $dst_y;
  2808.         $src_x = (int) $src_x;
  2809.         $src_y = (int) $src_y;
  2810.         $src_w = (int) $src_w;
  2811.         $src_h = (int) $src_h;
  2812.         $pct   = (int) $pct;
  2813.         $dst_w imagesx($dst_im);
  2814.         $dst_h imagesy($dst_im);
  2815.  
  2816.         for ($y $src_y$y $src_h$y++{
  2817.             for ($x $src_x$x $src_w$x++{
  2818.  
  2819.                 if ($x $dst_x >= && $x $dst_x $dst_w && $x $src_x >= && $x $src_x $src_w
  2820.                  && $y $dst_y >= && $y $dst_y $dst_h && $y $src_y >= && $y $src_y $src_h{
  2821.  
  2822.                     $dst_pixel imagecolorsforindex($dst_imimagecolorat($dst_im$x $dst_x$y $dst_y));
  2823.                     $src_pixel imagecolorsforindex($src_imimagecolorat($src_im$x $src_x$y $src_y));
  2824.  
  2825.                     $src_alpha ($src_pixel['alpha'127);
  2826.                     $dst_alpha ($dst_pixel['alpha'127);
  2827.                     $opacity $src_alpha $pct 100;
  2828.                     if ($dst_alpha >= $opacity$alpha $dst_alpha;
  2829.                     if ($dst_alpha $opacity)  $alpha $opacity;
  2830.                     if ($alpha 1$alpha 1;
  2831.  
  2832.                     if ($opacity 0{
  2833.                         $dst_red   round(( ($dst_pixel['red']   $dst_alpha ($opacity)) ) );
  2834.                         $dst_green round(( ($dst_pixel['green'$dst_alpha ($opacity)) ) );
  2835.                         $dst_blue  round(( ($dst_pixel['blue']  $dst_alpha ($opacity)) ) );
  2836.                         $src_red   round((($src_pixel['red']   $opacity)) );
  2837.                         $src_green round((($src_pixel['green'$opacity)) );
  2838.                         $src_blue  round((($src_pixel['blue']  $opacity)) );
  2839.                         $red   round(($dst_red   $src_red  ($dst_alpha ($opacity$opacity));
  2840.                         $green round(($dst_green $src_green($dst_alpha ($opacity$opacity));
  2841.                         $blue  round(($dst_blue  $src_blue ($dst_alpha ($opacity$opacity));
  2842.                         if ($red   255$red   255;
  2843.                         if ($green 255$green 255;
  2844.                         if ($blue  255$blue  255;
  2845.                         $alpha =  round(($alpha127);
  2846.                         $color imagecolorallocatealpha($dst_im$red$green$blue$alpha);
  2847.                         imagesetpixel($dst_im$x $dst_x$y $dst_y$color);
  2848.                     }
  2849.                 }
  2850.             }
  2851.         }
  2852.         return true;
  2853.     }
  2854.  
  2855.  
  2856.  
  2857.     /**
  2858.      * Actually uploads the file, and act on it according to the set processing class variables
  2859.      *
  2860.      * This function copies the uploaded file to the given location, eventually performing actions on it.
  2861.      * Typically, you can call {@link process} several times for the same file,
  2862.      * for instance to create a resized image and a thumbnail of the same file.
  2863.      * The original uploaded file remains intact in its temporary location, so you can use {@link process} several times.
  2864.      * You will be able to delete the uploaded file with {@link clean} when you have finished all your {@link process} calls.
  2865.      *
  2866.      * According to the processing class variables set in the calling file, the file can be renamed,
  2867.      * and if it is an image, can be resized or converted.
  2868.      *
  2869.      * When the processing is completed, and the file copied to its new location, the
  2870.      * processing class variables will be reset to their default value.
  2871.      * This allows you to set new properties, and perform another {@link process} on the same uploaded file
  2872.      *
  2873.      * If the function is called with a null or empty argument, then it will return the content of the picture
  2874.      *
  2875.      * It will set {@link processed} (and {@link error} is an error occurred)
  2876.      *
  2877.      * @access public
  2878.      * @param  string $server_path Optional path location of the uploaded file, with an ending slash
  2879.      * @return string Optional content of the image
  2880.      */
  2881.     function process($server_path null{
  2882.  
  2883.         $this->error        '';
  2884.         $this->processed    true;
  2885.         $return_mode        false;
  2886.         $return_content     null;
  2887.  
  2888.         if (!$this->uploaded{
  2889.             $this->error $this->translate('file_not_uploaded');
  2890.             $this->processed false;
  2891.         }
  2892.  
  2893.         if ($this->processed{
  2894.             if (empty($server_path|| is_null($server_path)) {
  2895.                 $this->log .= '<b>process file and return the content</b><br />';
  2896.                 $return_mode true;
  2897.             else {
  2898.                 if(strtolower(substr(PHP_OS03)) === 'win'{
  2899.                     if (substr($server_path-11!= '\\'$server_path $server_path '\\';
  2900.                 else {
  2901.                     if (substr($server_path-11!= '/'$server_path $server_path '/';
  2902.                 }
  2903.                 $this->log .= '<b>process file to '  $server_path '</b><br />';
  2904.             }
  2905.         }
  2906.  
  2907.         if ($this->processed{
  2908.             // checks file max size
  2909.             if ($this->file_src_size $this->file_max_size {
  2910.                 $this->processed false;
  2911.                 $this->error $this->translate('file_too_big');
  2912.             else {
  2913.                 $this->log .= '- file size OK<br />';
  2914.             }
  2915.         }
  2916.  
  2917.         if ($this->processed{
  2918.             // turn dangerous scripts into text files
  2919.             if ($this->no_script{
  2920.                 if (((substr($this->file_src_mime05== 'text/' || strpos($this->file_src_mime'javascript'!== false)  && (substr($this->file_src_name-4!= '.txt'))
  2921.                     || preg_match('/\.(php|pl|py|cgi|asp)$/i'$this->file_src_name|| empty($this->file_src_name_ext)) {
  2922.                     $this->file_src_mime 'text/plain';
  2923.                     $this->log .= '- script '  $this->file_src_name ' renamed as ' $this->file_src_name '.txt!<br />';
  2924.                     $this->file_src_name_ext .= (empty($this->file_src_name_ext'txt' '.txt');
  2925.                 }
  2926.             }
  2927.  
  2928.             if ($this->mime_check && empty($this->file_src_mime)) {
  2929.                 $this->processed false;
  2930.                 $this->error $this->translate('no_mime');
  2931.             else if ($this->mime_check && !empty($this->file_src_mime&& strpos($this->file_src_mime'/'!== false{
  2932.                 list($m1$m2explode('/'$this->file_src_mime);
  2933.                 $allowed false;
  2934.                 // check wether the mime type is allowed
  2935.                 foreach($this->allowed as $k => $v{
  2936.                     list($v1$v2explode('/'$v);
  2937.                     if (($v1 == '*' && $v2 == '*'|| ($v1 == $m1 && ($v2 == $m2 || $v2 == '*'))) {
  2938.                         $allowed true;
  2939.                         break;
  2940.                     }
  2941.                 }
  2942.                 // check wether the mime type is forbidden
  2943.                 foreach($this->forbidden as $k => $v{
  2944.                     list($v1$v2explode('/'$v);
  2945.                     if (($v1 == '*' && $v2 == '*'|| ($v1 == $m1 && ($v2 == $m2 || $v2 == '*'))) {
  2946.                         $allowed false;
  2947.                         break;
  2948.                     }
  2949.                 }
  2950.                 if (!$allowed{
  2951.                     $this->processed false;
  2952.                     $this->error $this->translate('incorrect_file');
  2953.                 else {
  2954.                     $this->log .= '- file mime OK : ' $this->file_src_mime '<br />';
  2955.                 }
  2956.             else {
  2957.                 $this->log .= '- file mime (not checked) : ' $this->file_src_mime '<br />';
  2958.             }
  2959.  
  2960.             // if the file is an image, we can check on its dimensions
  2961.             // these checks are not available if open_basedir restrictions are in place
  2962.             if ($this->file_is_image{
  2963.                 if (is_numeric($this->image_src_x&& is_numeric($this->image_src_y)) {
  2964.                     $ratio $this->image_src_x $this->image_src_y;
  2965.                     if (!is_null($this->image_max_width&& $this->image_src_x $this->image_max_width{
  2966.                         $this->processed false;
  2967.                         $this->error $this->translate('image_too_wide');
  2968.                     }
  2969.                     if (!is_null($this->image_min_width&& $this->image_src_x $this->image_min_width{
  2970.                         $this->processed false;
  2971.                         $this->error $this->translate('image_too_narrow');
  2972.                     }
  2973.                     if (!is_null($this->image_max_height&& $this->image_src_y $this->image_max_height{
  2974.                         $this->processed false;
  2975.                         $this->error $this->translate('image_too_high');
  2976.                     }
  2977.                     if (!is_null($this->image_min_height&& $this->image_src_y $this->image_min_height{
  2978.                         $this->processed false;
  2979.                         $this->error $this->translate('image_too_short');
  2980.                     }
  2981.                     if (!is_null($this->image_max_ratio&& $ratio $this->image_max_ratio{
  2982.                         $this->processed false;
  2983.                         $this->error $this->translate('ratio_too_high');
  2984.                     }
  2985.                     if (!is_null($this->image_min_ratio&& $ratio $this->image_min_ratio{
  2986.                         $this->processed false;
  2987.                         $this->error $this->translate('ratio_too_low');
  2988.                     }
  2989.                     if (!is_null($this->image_max_pixels&& $this->image_src_pixels $this->image_max_pixels{
  2990.                         $this->processed false;
  2991.                         $this->error $this->translate('too_many_pixels');
  2992.                     }
  2993.                     if (!is_null($this->image_min_pixels&& $this->image_src_pixels $this->image_min_pixels{
  2994.                         $this->processed false;
  2995.                         $this->error $this->translate('not_enough_pixels');
  2996.                     }
  2997.                 else {
  2998.                     $this->log .= '- no image properties available, can\'t enforce dimension checks : ' $this->file_src_mime '<br />';
  2999.                 }
  3000.             }
  3001.         }
  3002.  
  3003.         if ($this->processed{
  3004.             $this->file_dst_path        $server_path;
  3005.  
  3006.             // repopulate dst variables from src
  3007.             $this->file_dst_name        $this->file_src_name;
  3008.             $this->file_dst_name_body   $this->file_src_name_body;
  3009.             $this->file_dst_name_ext    $this->file_src_name_ext;
  3010.             if ($this->file_overwrite$this->file_auto_rename false;
  3011.  
  3012.             if ($this->image_convert != ''// if we convert as an image
  3013.                 $this->file_dst_name_ext  $this->image_convert;
  3014.                 $this->log .= '- new file name ext : ' $this->image_convert '<br />';
  3015.             }
  3016.             if ($this->file_new_name_body != ''// rename file body
  3017.                 $this->file_dst_name_body $this->file_new_name_body;
  3018.                 $this->log .= '- new file name body : ' $this->file_new_name_body '<br />';
  3019.             }
  3020.             if ($this->file_new_name_ext != ''// rename file ext
  3021.                 $this->file_dst_name_ext  $this->file_new_name_ext;
  3022.                 $this->log .= '- new file name ext : ' $this->file_new_name_ext '<br />';
  3023.             }
  3024.             if ($this->file_name_body_add != ''// append a string to the name
  3025.                 $this->file_dst_name_body  $this->file_dst_name_body $this->file_name_body_add;
  3026.                 $this->log .= '- file name body append : ' $this->file_name_body_add '<br />';
  3027.             }
  3028.             if ($this->file_name_body_pre != ''// prepend a string to the name
  3029.                 $this->file_dst_name_body  $this->file_name_body_pre $this->file_dst_name_body;
  3030.                 $this->log .= '- file name body prepend : ' $this->file_name_body_pre '<br />';
  3031.             }
  3032.             if ($this->file_safe_name// formats the name
  3033.                 $this->file_dst_name_body str_replace(array(' ''-')array('_','_')$this->file_dst_name_body;
  3034.                 $this->file_dst_name_body preg_replace('/[^A-Za-z0-9_]/'''$this->file_dst_name_body;
  3035.                 $this->log .= '- file name safe format<br />';
  3036.             }
  3037.  
  3038.             $this->log .= '- destination variables<br />';
  3039.             if (empty($this->file_dst_path|| is_null($this->file_dst_path)) {
  3040.                 $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_dst_path         : n/a<br />';
  3041.             else {
  3042.                 $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_dst_path         : ' $this->file_dst_path '<br />';
  3043.             }
  3044.             $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_dst_name_body    : ' $this->file_dst_name_body '<br />';
  3045.             $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_dst_name_ext     : ' $this->file_dst_name_ext '<br />';
  3046.  
  3047.             // do we do some image manipulation?
  3048.             $image_manipulation  ($this->file_is_image && (
  3049.                                     $this->image_resize
  3050.                                  || $this->image_convert != ''
  3051.                                  || is_numeric($this->image_brightness)
  3052.                                  || is_numeric($this->image_contrast)
  3053.                                  || is_numeric($this->image_threshold)
  3054.                                  || !empty($this->image_tint_color)
  3055.                                  || !empty($this->image_overlay_color)
  3056.                                  || !empty($this->image_text)
  3057.                                  || $this->image_greyscale
  3058.                                  || $this->image_negative
  3059.                                  || !empty($this->image_watermark)
  3060.                                  || is_numeric($this->image_rotate)
  3061.                                  || is_numeric($this->jpeg_size)
  3062.                                  || !empty($this->image_flip)
  3063.                                  || !empty($this->image_crop)
  3064.                                  || !empty($this->image_precrop)
  3065.                                  || !empty($this->image_border)
  3066.                                  || $this->image_frame 0
  3067.                                  || $this->image_bevel 0
  3068.                                  || $this->image_reflection_height));
  3069.  
  3070.             if ($image_manipulation{
  3071.                 if ($this->image_convert==''{
  3072.                     $this->file_dst_name $this->file_dst_name_body (!empty($this->file_dst_name_ext'.' $this->file_dst_name_ext '');
  3073.                     $this->log .= '- image operation, keep extension<br />';
  3074.                 else {
  3075.                     $this->file_dst_name $this->file_dst_name_body '.' $this->image_convert;
  3076.                     $this->log .= '- image operation, change extension for conversion type<br />';
  3077.                 }
  3078.             else {
  3079.                 $this->file_dst_name $this->file_dst_name_body (!empty($this->file_dst_name_ext'.' $this->file_dst_name_ext '');
  3080.                 $this->log .= '- no image operation, keep extension<br />';
  3081.             }
  3082.  
  3083.             if (!$return_mode{
  3084.                 if (!$this->file_auto_rename{
  3085.                     $this->log .= '- no auto_rename if same filename exists<br />';
  3086.                     $this->file_dst_pathname $this->file_dst_path $this->file_dst_name;
  3087.                 else {
  3088.                     $this->log .= '- checking for auto_rename<br />';
  3089.                     $this->file_dst_pathname $this->file_dst_path $this->file_dst_name;
  3090.                     $body     $this->file_dst_name_body;
  3091.                     $cpt 1;
  3092.                     while (@file_exists($this->file_dst_pathname)) {
  3093.                         $this->file_dst_name_body $body '_' $cpt;
  3094.                         $this->file_dst_name $this->file_dst_name_body (!empty($this->file_dst_name_ext'.' $this->file_dst_name_ext '');
  3095.                         $cpt++;
  3096.                         $this->file_dst_pathname $this->file_dst_path $this->file_dst_name;
  3097.                     }
  3098.                     if ($cpt>1$this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;auto_rename to ' $this->file_dst_name '<br />';
  3099.                 }
  3100.  
  3101.                 $this->log .= '- destination file details<br />';
  3102.                 $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_dst_name         : ' $this->file_dst_name '<br />';
  3103.                 $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_dst_pathname     : ' $this->file_dst_pathname '<br />';
  3104.  
  3105.                 if ($this->file_overwrite{
  3106.                      $this->log .= '- no overwrite checking<br />';
  3107.                 else {
  3108.                     if (@file_exists($this->file_dst_pathname)) {
  3109.                         $this->processed false;
  3110.                         $this->error $this->translate('already_exists'array($this->file_dst_name));
  3111.                     else {
  3112.                         $this->log .= '- ' $this->file_dst_name ' doesn\'t exist already<br />';
  3113.                     }
  3114.                 }
  3115.             }
  3116.         }
  3117.  
  3118.         if ($this->processed{
  3119.             // if we have already moved the uploaded file, we use the temporary copy as source file, and check if it exists
  3120.             if (!empty($this->file_src_temp)) {
  3121.                 $this->log .= '- use the temp file instead of the original file since it is a second process<br />';
  3122.                 $this->file_src_pathname   $this->file_src_temp;
  3123.                 if (!file_exists($this->file_src_pathname)) {
  3124.                     $this->processed false;
  3125.                     $this->error $this->translate('temp_file_missing');
  3126.                 }
  3127.             // if we haven't a temp file, and that we do check on uploads, we use is_uploaded_file()
  3128.             else if (!$this->no_upload_check{
  3129.                 if (!is_uploaded_file($this->file_src_pathname)) {
  3130.                     $this->processed false;
  3131.                     $this->error $this->translate('source_missing');
  3132.                 }
  3133.             // otherwise, if we don't check on uploaded files (local file for instance), we use file_exists()
  3134.             else {
  3135.                 if (!file_exists($this->file_src_pathname)) {
  3136.                     $this->processed false;
  3137.                     $this->error $this->translate('source_missing');
  3138.                 }
  3139.             }
  3140.  
  3141.             // checks if the destination directory exists, and attempt to create it
  3142.             if (!$return_mode{
  3143.                 if ($this->processed && !file_exists($this->file_dst_path)) {
  3144.                     if ($this->dir_auto_create{
  3145.                         $this->log .= '- ' $this->file_dst_path ' doesn\'t exist. Attempting creation:';
  3146.                         if (!$this->rmkdir($this->file_dst_path$this->dir_chmod)) {
  3147.                             $this->log .= ' failed<br />';
  3148.                             $this->processed false;
  3149.                             $this->error $this->translate('destination_dir');
  3150.                         else {
  3151.                             $this->log .= ' success<br />';
  3152.                         }
  3153.                     else {
  3154.                         $this->error $this->translate('destination_dir_missing');
  3155.                     }
  3156.                 }
  3157.  
  3158.                 if ($this->processed && !is_dir($this->file_dst_path)) {
  3159.                     $this->processed false;
  3160.                     $this->error $this->translate('destination_path_not_dir');
  3161.                 }
  3162.  
  3163.                 // checks if the destination directory is writeable, and attempt to make it writeable
  3164.                 $hash md5($this->file_dst_name_body rand(11000));
  3165.                 if ($this->processed && !($f @fopen($this->file_dst_path $hash '.' $this->file_dst_name_ext'a+'))) {
  3166.                     if ($this->dir_auto_chmod{
  3167.                         $this->log .= '- ' $this->file_dst_path ' is not writeable. Attempting chmod:';
  3168.                         if (!@chmod($this->file_dst_path$this->dir_chmod)) {
  3169.                             $this->log .= ' failed<br />';
  3170.                             $this->processed false;
  3171.                             $this->error $this->translate('destination_dir_write');
  3172.                         else {
  3173.                             $this->log .= ' success<br />';
  3174.                             if (!($f @fopen($this->file_dst_path $hash '.' $this->file_dst_name_ext'a+'))) // we re-check
  3175.                                 $this->processed false;
  3176.                                 $this->error $this->translate('destination_dir_write');
  3177.                             else {
  3178.                                 @fclose($f);
  3179.                             }
  3180.                         }
  3181.                     else {
  3182.                         $this->processed false;
  3183.                         $this->error $this->translate('destination_path_write');
  3184.                     }
  3185.                 else {
  3186.                     if ($this->processed@fclose($f);
  3187.                     @unlink($this->file_dst_path $hash '.' $this->file_dst_name_ext);
  3188.                 }
  3189.  
  3190.  
  3191.                 // if we have an uploaded file, and if it is the first process, and if we can't access the file directly (open_basedir restriction)
  3192.                 // then we create a temp file that will be used as the source file in subsequent processes
  3193.                 // the third condition is there to check if the file is not accessible *directly* (it already has positively gone through is_uploaded_file(), so it exists)
  3194.                 if (!$this->no_upload_check && empty($this->file_src_temp&& !@file_exists($this->file_src_pathname)) {
  3195.                     $this->log .= '- attempting to use a temp file:';
  3196.                     $hash md5($this->file_dst_name_body rand(11000));
  3197.                     if (move_uploaded_file($this->file_src_pathname$this->file_dst_path $hash '.' $this->file_dst_name_ext)) {
  3198.                         $this->file_src_pathname $this->file_dst_path $hash '.' $this->file_dst_name_ext;
  3199.                         $this->file_src_temp $this->file_src_pathname;
  3200.                         $this->log .= ' file created<br />';
  3201.                         $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;temp file is: ' $this->file_src_temp '<br />';
  3202.                     else {
  3203.                         $this->log .= ' failed<br />';
  3204.                         $this->processed false;
  3205.                         $this->error $this->translate('temp_file');
  3206.                     }
  3207.                 }
  3208.             }
  3209.         }
  3210.  
  3211.         if ($this->processed{
  3212.  
  3213.             // we do a quick check to ensure the file is really an image
  3214.             // we can do this only now, as it would have failed before in case of open_basedir
  3215.             if ($image_manipulation && !@getimagesize($this->file_src_pathname)) {
  3216.                 $this->log .= '- the file is not an image!<br />';
  3217.                 $image_manipulation false;
  3218.             }
  3219.  
  3220.             if ($image_manipulation{
  3221.  
  3222.                 // checks if the source file is readable
  3223.                 if ($this->processed && !($f @fopen($this->file_src_pathname'r'))) {
  3224.                     $this->processed false;
  3225.                     $this->error $this->translate('source_not_readable');
  3226.                 else {
  3227.                     @fclose($f);
  3228.                 }
  3229.  
  3230.                 // we now do all the image manipulations
  3231.                 $this->log .= '- image resizing or conversion wanted<br />';
  3232.                 if ($this->gdversion()) {
  3233.                     switch($this->image_src_type{
  3234.                         case 'jpg':
  3235.                             if (!function_exists('imagecreatefromjpeg')) {
  3236.                                 $this->processed false;
  3237.                                 $this->error $this->translate('no_create_support'array('JPEG'));
  3238.                             else {
  3239.                                 $image_src @imagecreatefromjpeg($this->file_src_pathname);
  3240.                                 if (!$image_src{
  3241.                                     $this->processed false;
  3242.                                     $this->error $this->translate('create_error'array('JPEG'));
  3243.                                 else {
  3244.                                     $this->log .= '- source image is JPEG<br />';
  3245.                                 }
  3246.                             }
  3247.                             break;
  3248.                         case 'png':
  3249.                             if (!function_exists('imagecreatefrompng')) {
  3250.                                 $this->processed false;
  3251.                                 $this->error $this->translate('no_create_support'array('PNG'));
  3252.                             else {
  3253.                                 $image_src @imagecreatefrompng($this->file_src_pathname);
  3254.                                 if (!$image_src{
  3255.                                     $this->processed false;
  3256.                                     $this->error $this->translate('create_error'array('PNG'));
  3257.                                 else {
  3258.                                     $this->log .= '- source image is PNG<br />';
  3259.                                 }
  3260.                             }
  3261.                             break;
  3262.                         case 'gif':
  3263.                             if (!function_exists('imagecreatefromgif')) {
  3264.                                 $this->processed false;
  3265.                                 $this->error $this->translate('no_create_support'array('GIF'));
  3266.                             else {
  3267.                                 $image_src @imagecreatefromgif($this->file_src_pathname);
  3268.                                 if (!$image_src{
  3269.                                     $this->processed false;
  3270.                                     $this->error $this->translate('create_error'array('GIF'));
  3271.                                 else {
  3272.                                     $this->log .= '- source image is GIF<br />';
  3273.                                 }
  3274.                             }
  3275.                             break;
  3276.                         case 'bmp':
  3277.                             if (!method_exists($this'imagecreatefrombmp')) {
  3278.                                 $this->processed false;
  3279.                                 $this->error $this->translate('no_create_support'array('BMP'));
  3280.                             else {
  3281.                                 $image_src @$this->imagecreatefrombmp($this->file_src_pathname);
  3282.                                 if (!$image_src{
  3283.                                     $this->processed false;
  3284.                                     $this->error $this->translate('create_error'array('BMP'));
  3285.                                 else {
  3286.                                     $this->log .= '- source image is BMP<br />';
  3287.                                 }
  3288.                             }
  3289.                             break;
  3290.                         default:
  3291.                             $this->processed false;
  3292.                             $this->error $this->translate('source_invalid');
  3293.                     }
  3294.                 else {
  3295.                     $this->processed false;
  3296.                     $this->error $this->translate('gd_missing');
  3297.                 }
  3298.  
  3299.                 if ($this->processed && $image_src{
  3300.  
  3301.                     // we have to set image_convert if it is not already
  3302.                     if (empty($this->image_convert)) {
  3303.                         $this->log .= '- setting destination file type to ' $this->file_src_name_ext '<br />';
  3304.                         $this->image_convert $this->file_src_name_ext;
  3305.                     }
  3306.  
  3307.                     if (!in_array($this->image_convert$this->image_supported)) {
  3308.                         $this->image_convert 'jpg';
  3309.                     }
  3310.  
  3311.                     // we set the default color to be the background color if we don't output in a transparent format
  3312.                     if ($this->image_convert != 'png' && $this->image_convert != 'gif' && !empty($this->image_default_color&& empty($this->image_background_color)) $this->image_background_color $this->image_default_color;
  3313.                     if (!empty($this->image_background_color)) $this->image_default_color $this->image_background_color;
  3314.                     if (empty($this->image_default_color)) $this->image_default_color '#FFFFFF';
  3315.  
  3316.                     $this->image_src_x imagesx($image_src);
  3317.                     $this->image_src_y imagesy($image_src);
  3318.                     $gd_version $this->gdversion();
  3319.                     $ratio_crop null;
  3320.  
  3321.                     if (!imageistruecolor($image_src)) {  // $this->image_src_type == 'gif'
  3322.                         $this->log .= '- image is detected as having a palette<br />';
  3323.                         $this->image_is_palette true;
  3324.                         $this->image_transparent_color imagecolortransparent($image_src);
  3325.                         if ($this->image_transparent_color >= && imagecolorstotal($image_src$this->image_transparent_color{
  3326.                             $this->image_is_transparent true;
  3327.                             $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;palette image is detected as transparent<br />';
  3328.                         }
  3329.                         // if the image has a palette (GIF), we convert it to true color, preserving transparency
  3330.                         $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;convert palette image to true color<br />';
  3331.                         $true_color imagecreatetruecolor($this->image_src_x$this->image_src_y);
  3332.                         imagealphablending($true_colorfalse);
  3333.                         imagesavealpha($true_colortrue);
  3334.                         for ($x 0$x $this->image_src_x$x++{
  3335.                             for ($y 0$y $this->image_src_y$y++{
  3336.                                 if ($this->image_transparent_color >= && imagecolorat($image_src$x$y== $this->image_transparent_color{
  3337.                                     imagesetpixel($true_color$x$y127 << 24);
  3338.                                 else {
  3339.                                     $rgb imagecolorsforindex($image_srcimagecolorat($image_src$x$y));
  3340.                                     imagesetpixel($true_color$x$y($rgb['alpha'<< 24($rgb['red'<< 16($rgb['green'<< 8$rgb['blue']);
  3341.                                 }
  3342.                             }
  3343.                         }
  3344.                         $image_src $this->imagetransfer($true_color$image_src);
  3345.                         imagealphablending($image_srcfalse);
  3346.                         imagesavealpha($image_srctrue);
  3347.                         $this->image_is_palette false;
  3348.                     }
  3349.  
  3350.  
  3351.                     $image_dst $image_src;
  3352.  
  3353.                     // pre-crop image, before resizing
  3354.                     if ((!empty($this->image_precrop))) {
  3355.                         if (is_array($this->image_precrop)) {
  3356.                             $vars $this->image_precrop;
  3357.                         else {
  3358.                             $vars explode(' '$this->image_precrop);
  3359.                         }
  3360.                         if (sizeof($vars== 4{
  3361.                             $ct $vars[0]$cr $vars[1]$cb $vars[2]$cl $vars[3];
  3362.                         else if (sizeof($vars== 2{
  3363.                             $ct $vars[0]$cr $vars[1]$cb $vars[0]$cl $vars[1];
  3364.                         else {
  3365.                             $ct $vars[0]$cr $vars[0]$cb $vars[0]$cl $vars[0];
  3366.                         }
  3367.                         if (strpos($ct'%')>0$ct $this->image_src_y (str_replace('%','',$ct100);
  3368.                         if (strpos($cr'%')>0$cr $this->image_src_x (str_replace('%','',$cr100);
  3369.                         if (strpos($cb'%')>0$cb $this->image_src_y (str_replace('%','',$cb100);
  3370.                         if (strpos($cl'%')>0$cl $this->image_src_x (str_replace('%','',$cl100);
  3371.                         if (strpos($ct'px')>0$ct str_replace('px','',$ct);
  3372.                         if (strpos($cr'px')>0$cr str_replace('px','',$cr);
  3373.                         if (strpos($cb'px')>0$cb str_replace('px','',$cb);
  3374.                         if (strpos($cl'px')>0$cl str_replace('px','',$cl);
  3375.                         $ct = (int) $ct;
  3376.                         $cr = (int) $cr;
  3377.                         $cb = (int) $cb;
  3378.                         $cl = (int) $cl;
  3379.                         $this->log .= '- pre-crop image : ' $ct ' ' $cr ' ' $cb ' ' $cl ' <br />';
  3380.                         $this->image_src_x $this->image_src_x $cl $cr;
  3381.                         $this->image_src_y $this->image_src_y $ct $cb;
  3382.                         if ($this->image_src_x 1$this->image_src_x 1;
  3383.                         if ($this->image_src_y 1$this->image_src_y 1;
  3384.                         $tmp $this->imagecreatenew($this->image_src_x$this->image_src_y);
  3385.  
  3386.                         // we copy the image into the recieving image
  3387.                         imagecopy($tmp$image_dst00$cl$ct$this->image_src_x$this->image_src_y);
  3388.  
  3389.                         // if we crop with negative margins, we have to make sure the extra bits are the right color, or transparent
  3390.                         if ($ct || $cr || $cb || $cl {
  3391.                             // use the background color if present
  3392.                             if (!empty($this->image_background_color)) {
  3393.                                 list($red$green$blue$this->getcolors($this->image_background_color);
  3394.                                 $fill imagecolorallocate($tmp$red$green$blue);
  3395.                             else {
  3396.                                 $fill imagecolorallocatealpha($tmp000127);
  3397.                             }
  3398.                             // fills eventual negative margins
  3399.                             if ($ct 0imagefilledrectangle($tmp00$this->image_src_x-$ct$fill);
  3400.                             if ($cr 0imagefilledrectangle($tmp$this->image_src_x $cr0$this->image_src_x$this->image_src_y$fill);
  3401.                             if ($cb 0imagefilledrectangle($tmp0$this->image_src_y $cb$this->image_src_x$this->image_src_y$fill);
  3402.                             if ($cl 0imagefilledrectangle($tmp00-$cl$this->image_src_y$fill);
  3403.                         }
  3404.  
  3405.                         // we transfert tmp into image_dst
  3406.                         $image_dst $this->imagetransfer($tmp$image_dst);
  3407.                     }
  3408.  
  3409.                     // resize image (and move image_src_x, image_src_y dimensions into image_dst_x, image_dst_y)
  3410.                     if ($this->image_resize{
  3411.                         $this->log .= '- resizing...<br />';
  3412.  
  3413.                         if ($this->image_ratio_x{
  3414.                             $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;calculate x size<br />';
  3415.                             $this->image_dst_x round(($this->image_src_x $this->image_y$this->image_src_y);
  3416.                             $this->image_dst_y $this->image_y;
  3417.                         else if ($this->image_ratio_y{
  3418.                             $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;calculate y size<br />';
  3419.                             $this->image_dst_x $this->image_x;
  3420.                             $this->image_dst_y round(($this->image_src_y $this->image_x$this->image_src_x);
  3421.                         else if (is_numeric($this->image_ratio_pixels)) {
  3422.                             $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;calculate x/y size to match a number of pixels<br />';
  3423.                             $pixels $this->image_src_y $this->image_src_x;
  3424.                             $diff sqrt($this->image_ratio_pixels $pixels);
  3425.                             $this->image_dst_x round($this->image_src_x $diff);
  3426.                             $this->image_dst_y round($this->image_src_y $diff);
  3427.                         else if ($this->image_ratio || $this->image_ratio_crop || $this->image_ratio_fill || $this->image_ratio_no_zoom_in || $this->image_ratio_no_zoom_out{
  3428.                             $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;check x/y sizes<br />';
  3429.                             if ((!$this->image_ratio_no_zoom_in && !$this->image_ratio_no_zoom_out)
  3430.                                  || ($this->image_ratio_no_zoom_in && ($this->image_src_x $this->image_x || $this->image_src_y $this->image_y))
  3431.                                  || ($this->image_ratio_no_zoom_out && $this->image_src_x $this->image_x && $this->image_src_y $this->image_y)) {
  3432.                                 $this->image_dst_x $this->image_x;
  3433.                                 $this->image_dst_y $this->image_y;
  3434.                                 if ($this->image_ratio_crop{
  3435.                                     if (!is_string($this->image_ratio_crop)) $this->image_ratio_crop '';
  3436.                                     $this->image_ratio_crop strtolower($this->image_ratio_crop);
  3437.                                     if (($this->image_src_x/$this->image_x($this->image_src_y/$this->image_y)) {
  3438.                                         $this->image_dst_y $this->image_y;
  3439.                                         $this->image_dst_x intval($this->image_src_x*($this->image_y $this->image_src_y));
  3440.                                         $ratio_crop array();
  3441.                                         $ratio_crop['x'$this->image_dst_x $this->image_x;
  3442.                                         if (strpos($this->image_ratio_crop'l'!== false{
  3443.                                             $ratio_crop['l'0;
  3444.                                             $ratio_crop['r'$ratio_crop['x'];
  3445.                                         else if (strpos($this->image_ratio_crop'r'!== false{
  3446.                                             $ratio_crop['l'$ratio_crop['x'];
  3447.                                             $ratio_crop['r'0;
  3448.                                         else {
  3449.                                             $ratio_crop['l'round($ratio_crop['x']/2);
  3450.                                             $ratio_crop['r'$ratio_crop['x'$ratio_crop['l'];
  3451.                                         }
  3452.                                         $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;ratio_crop_x         : ' $ratio_crop['x'' (' $ratio_crop['l'';' $ratio_crop['r'')<br />';
  3453.                                         if (is_null($this->image_crop)) $this->image_crop array(0000);
  3454.                                     else {
  3455.                                         $this->image_dst_x $this->image_x;
  3456.                                         $this->image_dst_y intval($this->image_src_y*($this->image_x $this->image_src_x));
  3457.                                         $ratio_crop array();
  3458.                                         $ratio_crop['y'$this->image_dst_y $this->image_y;
  3459.                                         if (strpos($this->image_ratio_crop't'!== false{
  3460.                                             $ratio_crop['t'0;
  3461.                                             $ratio_crop['b'$ratio_crop['y'];
  3462.                                         else if (strpos($this->image_ratio_crop'b'!== false{
  3463.                                             $ratio_crop['t'$ratio_crop['y'];
  3464.                                             $ratio_crop['b'0;
  3465.                                         else {
  3466.                                             $ratio_crop['t'round($ratio_crop['y']/2);
  3467.                                             $ratio_crop['b'$ratio_crop['y'$ratio_crop['t'];
  3468.                                         }
  3469.                                         $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;ratio_crop_y         : ' $ratio_crop['y'' (' $ratio_crop['t'';' $ratio_crop['b'')<br />';
  3470.                                         if (is_null($this->image_crop)) $this->image_crop array(0000);
  3471.                                     }
  3472.                                 else if ($this->image_ratio_fill{
  3473.                                     if (!is_string($this->image_ratio_fill)) $this->image_ratio_fill '';
  3474.                                     $this->image_ratio_fill strtolower($this->image_ratio_fill);
  3475.                                     if (($this->image_src_x/$this->image_x($this->image_src_y/$this->image_y)) {
  3476.                                         $this->image_dst_y $this->image_y;
  3477.                                         $this->image_dst_x intval($this->image_src_x*($this->image_y $this->image_src_y));
  3478.                                         $ratio_crop array();
  3479.                                         $ratio_crop['x'$this->image_dst_x $this->image_x;
  3480.                                         if (strpos($this->image_ratio_fill'l'!== false{
  3481.                                             $ratio_crop['l'0;
  3482.                                             $ratio_crop['r'$ratio_crop['x'];
  3483.                                         else if (strpos($this->image_ratio_fill'r'!== false{
  3484.                                             $ratio_crop['l'$ratio_crop['x'];
  3485.                                             $ratio_crop['r'0;
  3486.                                         else {
  3487.                                             $ratio_crop['l'round($ratio_crop['x']/2);
  3488.                                             $ratio_crop['r'$ratio_crop['x'$ratio_crop['l'];
  3489.                                         }
  3490.                                         $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;ratio_fill_x         : ' $ratio_crop['x'' (' $ratio_crop['l'';' $ratio_crop['r'')<br />';
  3491.                                         if (is_null($this->image_crop)) $this->image_crop array(0000);
  3492.                                     else {
  3493.                                         $this->image_dst_x $this->image_x;
  3494.                                         $this->image_dst_y intval($this->image_src_y*($this->image_x $this->image_src_x));
  3495.                                         $ratio_crop array();
  3496.                                         $ratio_crop['y'$this->image_dst_y $this->image_y;
  3497.                                         if (strpos($this->image_ratio_fill't'!== false{
  3498.                                             $ratio_crop['t'0;
  3499.                                             $ratio_crop['b'$ratio_crop['y'];
  3500.                                         else if (strpos($this->image_ratio_fill'b'!== false{
  3501.                                             $ratio_crop['t'$ratio_crop['y'];
  3502.                                             $ratio_crop['b'0;
  3503.                                         else {
  3504.                                             $ratio_crop['t'round($ratio_crop['y']/2);
  3505.                                             $ratio_crop['b'$ratio_crop['y'$ratio_crop['t'];
  3506.                                         }
  3507.                                         $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;ratio_fill_y         : ' $ratio_crop['y'' (' $ratio_crop['t'';' $ratio_crop['b'')<br />';
  3508.                                         if (is_null($this->image_crop)) $this->image_crop array(0000);
  3509.                                     }
  3510.                                 else {
  3511.                                     if (($this->image_src_x/$this->image_x($this->image_src_y/$this->image_y)) {
  3512.                                         $this->image_dst_x $this->image_x;
  3513.                                         $this->image_dst_y intval($this->image_src_y*($this->image_x $this->image_src_x));
  3514.                                     else {
  3515.                                         $this->image_dst_y $this->image_y;
  3516.                                         $this->image_dst_x intval($this->image_src_x*($this->image_y $this->image_src_y));
  3517.                                     }
  3518.                                 }
  3519.                             else {
  3520.                                 $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;doesn\'t calculate x/y sizes<br />';
  3521.                                 $this->image_dst_x $this->image_src_x;
  3522.                                 $this->image_dst_y $this->image_src_y;
  3523.                             }
  3524.                         else {
  3525.                             $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;use plain sizes<br />';
  3526.                             $this->image_dst_x $this->image_x;
  3527.                             $this->image_dst_y $this->image_y;
  3528.                         }
  3529.  
  3530.                         if ($this->image_dst_x 1$this->image_dst_x 1;
  3531.                         if ($this->image_dst_y 1$this->image_dst_y 1;
  3532.                         $tmp $this->imagecreatenew($this->image_dst_x$this->image_dst_y);
  3533.  
  3534.                         if ($gd_version >= 2{
  3535.                             $res imagecopyresampled($tmp$image_src0000$this->image_dst_x$this->image_dst_y$this->image_src_x$this->image_src_y);
  3536.                         else {
  3537.                             $res imagecopyresized($tmp$image_src0000$this->image_dst_x$this->image_dst_y$this->image_src_x$this->image_src_y);
  3538.                         }
  3539.  
  3540.                         $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;resized image object created<br />';
  3541.                         $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;image_src_x y        : ' $this->image_src_x ' x ' $this->image_src_y '<br />';
  3542.                         $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;image_dst_x y        : ' $this->image_dst_x ' x ' $this->image_dst_y '<br />';
  3543.                         // we transfert tmp into image_dst
  3544.                         $image_dst $this->imagetransfer($tmp$image_dst);
  3545.  
  3546.                     else {
  3547.                         $this->image_dst_x $this->image_src_x;
  3548.                         $this->image_dst_y $this->image_src_y;
  3549.                     }
  3550.  
  3551.                     // crop image (and also crops if image_ratio_crop is used)
  3552.                     if ((!empty($this->image_crop|| !is_null($ratio_crop))) {
  3553.                         if (is_array($this->image_crop)) {
  3554.                             $vars $this->image_crop;
  3555.                         else {
  3556.                             $vars explode(' '$this->image_crop);
  3557.                         }
  3558.                         if (sizeof($vars== 4{
  3559.                             $ct $vars[0]$cr $vars[1]$cb $vars[2]$cl $vars[3];
  3560.                         else if (sizeof($vars== 2{
  3561.                             $ct $vars[0]$cr $vars[1]$cb $vars[0]$cl $vars[1];
  3562.                         else {
  3563.                             $ct $vars[0]$cr $vars[0]$cb $vars[0]$cl $vars[0];
  3564.                         }
  3565.                         if (strpos($ct'%')>0$ct $this->image_dst_y (str_replace('%','',$ct100);
  3566.                         if (strpos($cr'%')>0$cr $this->image_dst_x (str_replace('%','',$cr100);
  3567.                         if (strpos($cb'%')>0$cb $this->image_dst_y (str_replace('%','',$cb100);
  3568.                         if (strpos($cl'%')>0$cl $this->image_dst_x (str_replace('%','',$cl100);
  3569.                         if (strpos($ct'px')>0$ct str_replace('px','',$ct);
  3570.                         if (strpos($cr'px')>0$cr str_replace('px','',$cr);
  3571.                         if (strpos($cb'px')>0$cb str_replace('px','',$cb);
  3572.                         if (strpos($cl'px')>0$cl str_replace('px','',$cl);
  3573.                         $ct = (int) $ct;
  3574.                         $cr = (int) $cr;
  3575.                         $cb = (int) $cb;
  3576.                         $cl = (int) $cl;
  3577.                         // we adjust the cropping if we use image_ratio_crop
  3578.                         if (!is_null($ratio_crop)) {
  3579.                             if (array_key_exists('t'$ratio_crop)) $ct += $ratio_crop['t'];
  3580.                             if (array_key_exists('r'$ratio_crop)) $cr += $ratio_crop['r'];
  3581.                             if (array_key_exists('b'$ratio_crop)) $cb += $ratio_crop['b'];
  3582.                             if (array_key_exists('l'$ratio_crop)) $cl += $ratio_crop['l'];
  3583.                         }
  3584.                         $this->log .= '- crop image : ' $ct ' ' $cr ' ' $cb ' ' $cl ' <br />';
  3585.                         $this->image_dst_x $this->image_dst_x $cl $cr;
  3586.                         $this->image_dst_y $this->image_dst_y $ct $cb;
  3587.                         if ($this->image_dst_x 1$this->image_dst_x 1;
  3588.                         if ($this->image_dst_y 1$this->image_dst_y 1;
  3589.                         $tmp $this->imagecreatenew($this->image_dst_x$this->image_dst_y);
  3590.  
  3591.                         // we copy the image into the recieving image
  3592.                         imagecopy($tmp$image_dst00$cl$ct$this->image_dst_x$this->image_dst_y);
  3593.  
  3594.                         // if we crop with negative margins, we have to make sure the extra bits are the right color, or transparent
  3595.                         if ($ct || $cr || $cb || $cl {
  3596.                             // use the background color if present
  3597.                             if (!empty($this->image_background_color)) {
  3598.                                 list($red$green$blue$this->getcolors($this->image_background_color);
  3599.                                 $fill imagecolorallocate($tmp$red$green$blue);
  3600.                             else {
  3601.                                 $fill imagecolorallocatealpha($tmp000127);
  3602.                             }
  3603.                             // fills eventual negative margins
  3604.                             if ($ct 0imagefilledrectangle($tmp00$this->image_dst_x-$ct$fill);
  3605.                             if ($cr 0imagefilledrectangle($tmp$this->image_dst_x $cr0$this->image_dst_x$this->image_dst_y$fill);
  3606.                             if ($cb 0imagefilledrectangle($tmp0$this->image_dst_y $cb$this->image_dst_x$this->image_dst_y$fill);
  3607.                             if ($cl 0imagefilledrectangle($tmp00-$cl$this->image_dst_y$fill);
  3608.                         }
  3609.  
  3610.                         // we transfert tmp into image_dst
  3611.                         $image_dst $this->imagetransfer($tmp$image_dst);
  3612.                     }
  3613.  
  3614.                     // flip image
  3615.                     if ($gd_version >= && !empty($this->image_flip)) {
  3616.                         $this->image_flip strtolower($this->image_flip);
  3617.                         $this->log .= '- flip image : ' $this->image_flip '<br />';
  3618.                         $tmp $this->imagecreatenew($this->image_dst_x$this->image_dst_y);
  3619.                         for ($x 0$x $this->image_dst_x$x++{
  3620.                             for ($y 0$y $this->image_dst_y$y++){
  3621.                                 if (strpos($this->image_flip'v'!== false{
  3622.                                     imagecopy($tmp$image_dst$this->image_dst_x $x 1$y$x$y11);
  3623.                                 else {
  3624.                                     imagecopy($tmp$image_dst$x$this->image_dst_y $y 1$x$y11);
  3625.                                 }
  3626.                             }
  3627.                         }
  3628.                         // we transfert tmp into image_dst
  3629.                         $image_dst $this->imagetransfer($tmp$image_dst);
  3630.                     }
  3631.  
  3632.                     // rotate image
  3633.                     if ($gd_version >= && is_numeric($this->image_rotate)) {
  3634.                         if (!in_array($this->image_rotatearray(090180270))) $this->image_rotate 0;
  3635.                         if ($this->image_rotate != 0{
  3636.                             if ($this->image_rotate == 90 || $this->image_rotate == 270{
  3637.                                 $tmp $this->imagecreatenew($this->image_dst_y$this->image_dst_x);
  3638.                             else {
  3639.                                 $tmp $this->imagecreatenew($this->image_dst_x$this->image_dst_y);
  3640.                             }
  3641.                             $this->log .= '- rotate image : ' $this->image_rotate '<br />';
  3642.                             for ($x 0$x $this->image_dst_x$x++{
  3643.                                 for ($y 0$y $this->image_dst_y$y++){
  3644.                                     if ($this->image_rotate == 90{
  3645.                                         imagecopy($tmp$image_dst$y$x$x$this->image_dst_y $y 111);
  3646.                                     else if ($this->image_rotate == 180{
  3647.                                         imagecopy($tmp$image_dst$x$y$this->image_dst_x $x 1$this->image_dst_y $y 111);
  3648.                                     else if ($this->image_rotate == 270{
  3649.                                         imagecopy($tmp$image_dst$y$x$this->image_dst_x $x 1$y11);
  3650.                                     else {
  3651.                                         imagecopy($tmp$image_dst$x$y$x$y11);
  3652.                                     }
  3653.                                 }
  3654.                             }
  3655.                             if ($this->image_rotate == 90 || $this->image_rotate == 270{
  3656.                                 $t $this->image_dst_y;
  3657.                                 $this->image_dst_y $this->image_dst_x;
  3658.                                 $this->image_dst_x $t;
  3659.                             }
  3660.                             // we transfert tmp into image_dst
  3661.                             $image_dst $this->imagetransfer($tmp$image_dst);
  3662.                         }
  3663.                     }
  3664.  
  3665.                     // add color overlay
  3666.                    if ($gd_version >= && (is_numeric($this->image_overlay_percent&& $this->image_overlay_percent && !empty($this->image_overlay_color))) {
  3667.                         $this->log .= '- apply color overlay<br />';
  3668.                         list($red$green$blue$this->getcolors($this->image_overlay_color);
  3669.                         $filter imagecreatetruecolor($this->image_dst_x$this->image_dst_y);
  3670.                         $color imagecolorallocate($filter$red$green$blue);
  3671.                         imagefilledrectangle($filter00$this->image_dst_x$this->image_dst_y$color);
  3672.                         $this->imagecopymergealpha($image_dst$filter0000$this->image_dst_x$this->image_dst_y$this->image_overlay_percent);
  3673.                         imagedestroy($filter);
  3674.                     }
  3675.  
  3676.                     // add brightness, contrast and tint, turns to greyscale and inverts colors
  3677.                     if ($gd_version >= && ($this->image_negative || $this->image_greyscale || is_numeric($this->image_threshold)|| is_numeric($this->image_brightness|| is_numeric($this->image_contrast|| !empty($this->image_tint_color))) {
  3678.                         $this->log .= '- apply tint, light, contrast correction, negative, greyscale and threshold<br />';
  3679.                         if (!empty($this->image_tint_color)) list($tint_red$tint_green$tint_blue$this->getcolors($this->image_tint_color);
  3680.                         imagealphablending($image_dsttrue);
  3681.                         for($y=0$y $this->image_dst_y$y++{
  3682.                             for($x=0$x $this->image_dst_x$x++{
  3683.                                 if ($this->image_greyscale{
  3684.                                     $pixel imagecolorsforindex($image_dstimagecolorat($image_dst$x$y));
  3685.                                     $r $g $b round((0.2125 $pixel['red'](0.7154 $pixel['green'](0.0721 $pixel['blue']));
  3686.                                     $color imagecolorallocatealpha($image_dst$r$g$b$pixel['alpha']);
  3687.                                     imagesetpixel($image_dst$x$y$color);
  3688.                                 }
  3689.                                 if (is_numeric($this->image_threshold)) {
  3690.                                     $pixel imagecolorsforindex($image_dstimagecolorat($image_dst$x$y));
  3691.                                     $c (round($pixel['red'$pixel['green'$pixel['blue']3127;
  3692.                                     $r $g $b ($c $this->image_threshold 255 0);
  3693.                                     $color imagecolorallocatealpha($image_dst$r$g$b$pixel['alpha']);
  3694.                                     imagesetpixel($image_dst$x$y$color);
  3695.                                 }
  3696.                                 if (is_numeric($this->image_brightness)) {
  3697.                                     $pixel imagecolorsforindex($image_dstimagecolorat($image_dst$x$y));
  3698.                                     $r max(min(round($pixel['red'(($this->image_brightness 2)))255)0);
  3699.                                     $g max(min(round($pixel['green'(($this->image_brightness 2)))255)0);
  3700.                                     $b max(min(round($pixel['blue'(($this->image_brightness 2)))255)0);
  3701.                                     $color imagecolorallocatealpha($image_dst$r$g$b$pixel['alpha']);
  3702.                                     imagesetpixel($image_dst$x$y$color);
  3703.                                 }
  3704.                                 if (is_numeric($this->image_contrast)) {
  3705.                                     $pixel imagecolorsforindex($image_dstimagecolorat($image_dst$x$y));
  3706.                                     $r max(min(round(($this->image_contrast 128$pixel['red'128)255)0);
  3707.                                     $g max(min(round(($this->image_contrast 128$pixel['green'128)255)0);
  3708.                                     $b max(min(round(($this->image_contrast 128$pixel['blue'128)255)0);
  3709.                                     $color imagecolorallocatealpha($image_dst$r$g$b$pixel['alpha']);
  3710.                                     imagesetpixel($image_dst$x$y$color);
  3711.                                 }
  3712.                                 if (!empty($this->image_tint_color)) {
  3713.                                     $pixel imagecolorsforindex($image_dstimagecolorat($image_dst$x$y));
  3714.                                     $r min(round($tint_red $pixel['red'169)255);
  3715.                                     $g min(round($tint_green $pixel['green'169)255);
  3716.                                     $b min(round($tint_blue $pixel['blue'169)255);
  3717.                                     $color imagecolorallocatealpha($image_dst$r$g$b$pixel['alpha']);
  3718.                                     imagesetpixel($image_dst$x$y$color);
  3719.                                 }
  3720.                                 if (!empty($this->image_negative)) {
  3721.                                     $pixel imagecolorsforindex($image_dstimagecolorat($image_dst$x$y));
  3722.                                     $r round(255 $pixel['red']);
  3723.                                     $g round(255 $pixel['green']);
  3724.                                     $b round(255 $pixel['blue']);
  3725.                                     $color imagecolorallocatealpha($image_dst$r$g$b$pixel['alpha']);
  3726.                                     imagesetpixel($image_dst$x$y$color);
  3727.                                 }
  3728.                             }
  3729.                         }
  3730.                     }
  3731.  
  3732.                     // adds a border
  3733.                     if ($gd_version >= && !empty($this->image_border)) {
  3734.                         if (is_array($this->image_border)) {
  3735.                             $vars $this->image_border;
  3736.                             $this->log .= '- add border : ' implode(' '$this->image_border'<br />';
  3737.                         else {
  3738.                             $this->log .= '- add border : ' $this->image_border '<br />';
  3739.                             $vars explode(' '$this->image_border);
  3740.                         }
  3741.                         if (sizeof($vars== 4{
  3742.                             $ct $vars[0]$cr $vars[1]$cb $vars[2]$cl $vars[3];
  3743.                         else if (sizeof($vars== 2{
  3744.                             $ct $vars[0]$cr $vars[1]$cb $vars[0]$cl $vars[1];
  3745.                         else {
  3746.                             $ct $vars[0]$cr $vars[0]$cb $vars[0]$cl $vars[0];
  3747.                         }
  3748.                         if (strpos($ct'%')>0$ct $this->image_dst_y (str_replace('%','',$ct100);
  3749.                         if (strpos($cr'%')>0$cr $this->image_dst_x (str_replace('%','',$cr100);
  3750.                         if (strpos($cb'%')>0$cb $this->image_dst_y (str_replace('%','',$cb100);
  3751.                         if (strpos($cl'%')>0$cl $this->image_dst_x (str_replace('%','',$cl100);
  3752.                         if (strpos($ct'px')>0$ct str_replace('px','',$ct);
  3753.                         if (strpos($cr'px')>0$cr str_replace('px','',$cr);
  3754.                         if (strpos($cb'px')>0$cb str_replace('px','',$cb);
  3755.                         if (strpos($cl'px')>0$cl str_replace('px','',$cl);
  3756.                         $ct = (int) $ct;
  3757.                         $cr = (int) $cr;
  3758.                         $cb = (int) $cb;
  3759.                         $cl = (int) $cl;
  3760.                         $this->image_dst_x $this->image_dst_x $cl $cr;
  3761.                         $this->image_dst_y $this->image_dst_y $ct $cb;
  3762.                         if (!empty($this->image_border_color)) list($red$green$blue$this->getcolors($this->image_border_color);
  3763.                         // we now create an image, that we fill with the border color
  3764.                         $tmp $this->imagecreatenew($this->image_dst_x$this->image_dst_y);
  3765.                         $background imagecolorallocatealpha($tmp$red$green$blue0);
  3766.                         imagefilledrectangle($tmp00$this->image_dst_x$this->image_dst_y$background);
  3767.                         // we then copy the source image into the new image, without merging so that only the border is actually kept
  3768.                         imagecopy($tmp$image_dst$cl$ct00$this->image_dst_x $cr $cl$this->image_dst_y $cb $ct);
  3769.                         // we transfert tmp into image_dst
  3770.                         $image_dst $this->imagetransfer($tmp$image_dst);
  3771.                     }
  3772.  
  3773.                     // add frame border
  3774.                     if (is_numeric($this->image_frame)) {
  3775.                         if (is_array($this->image_frame_colors)) {
  3776.                             $vars $this->image_frame_colors;
  3777.                             $this->log .= '- add frame : ' implode(' '$this->image_frame_colors'<br />';
  3778.                         else {
  3779.                             $this->log .= '- add frame : ' $this->image_frame_colors '<br />';
  3780.                             $vars explode(' '$this->image_frame_colors);
  3781.                         }
  3782.                         $nb sizeof($vars);
  3783.                         $this->image_dst_x $this->image_dst_x ($nb 2);
  3784.                         $this->image_dst_y $this->image_dst_y ($nb 2);
  3785.                         $tmp $this->imagecreatenew($this->image_dst_x$this->image_dst_y);
  3786.                         imagecopy($tmp$image_dst$nb$nb00$this->image_dst_x ($nb 2)$this->image_dst_y ($nb 2));
  3787.                         for ($i=0$i<$nb$i++{
  3788.                             list($red$green$blue$this->getcolors($vars[$i]);
  3789.                             $c imagecolorallocate($tmp$red$green$blue);
  3790.                             if ($this->image_frame == 1{
  3791.                                 imageline($tmp$i$i$this->image_dst_x $i -1$i$c);
  3792.                                 imageline($tmp$this->image_dst_x $i -1$this->image_dst_y $i -1$this->image_dst_x $i -1$i$c);
  3793.                                 imageline($tmp$this->image_dst_x $i -1$this->image_dst_y $i -1$i$this->image_dst_y $i -1$c);
  3794.                                 imageline($tmp$i$i$i$this->image_dst_y $i -1$c);
  3795.                             else {
  3796.                                 imageline($tmp$i$i$this->image_dst_x $i -1$i$c);
  3797.                                 imageline($tmp$this->image_dst_x $nb $i$this->image_dst_y $nb $i$this->image_dst_x $nb $i$nb $i$c);
  3798.                                 imageline($tmp$this->image_dst_x $nb $i$this->image_dst_y $nb $i$nb $i$this->image_dst_y $nb $i$c);
  3799.                                 imageline($tmp$i$i$i$this->image_dst_y $i -1$c);
  3800.                             }
  3801.                         }
  3802.                         // we transfert tmp into image_dst
  3803.                         $image_dst $this->imagetransfer($tmp$image_dst);
  3804.                     }
  3805.  
  3806.                     // add bevel border
  3807.                     if ($this->image_bevel 0{
  3808.                         if (empty($this->image_bevel_color1)) $this->image_bevel_color1 '#FFFFFF';
  3809.                         if (empty($this->image_bevel_color2)) $this->image_bevel_color2 '#000000';
  3810.                         list($red1$green1$blue1$this->getcolors($this->image_bevel_color1);
  3811.                         list($red2$green2$blue2$this->getcolors($this->image_bevel_color2);
  3812.                         $tmp $this->imagecreatenew($this->image_dst_x$this->image_dst_y);
  3813.                         imagecopy($tmp$image_dst0000$this->image_dst_x$this->image_dst_y);
  3814.                         imagealphablending($tmptrue);
  3815.                         for ($i=0$i<$this->image_bevel$i++{
  3816.                             $alpha round(($i $this->image_bevel127);
  3817.                             $c1 imagecolorallocatealpha($tmp$red1$green1$blue1$alpha);
  3818.                             $c2 imagecolorallocatealpha($tmp$red2$green2$blue2$alpha);
  3819.                             imageline($tmp$i$i$this->image_dst_x $i -1$i$c1);
  3820.                             imageline($tmp$this->image_dst_x $i -1$this->image_dst_y $i$this->image_dst_x $i -1$i$c2);
  3821.                             imageline($tmp$this->image_dst_x $i -1$this->image_dst_y $i -1$i$this->image_dst_y $i -1$c2);
  3822.                             imageline($tmp$i$i$i$this->image_dst_y $i -1$c1);
  3823.                         }
  3824.                         // we transfert tmp into image_dst
  3825.                         $image_dst $this->imagetransfer($tmp$image_dst);
  3826.                     }
  3827.  
  3828.                     // add watermark image
  3829.                     if ($this->image_watermark!='' && file_exists($this->image_watermark)) {
  3830.                         $this->log .= '- add watermark<br />';
  3831.                         $this->image_watermark_position strtolower($this->image_watermark_position);
  3832.                         $watermark_info getimagesize($this->image_watermark);
  3833.                         $watermark_type (array_key_exists(2$watermark_info$watermark_info[2null)// 1 = GIF, 2 = JPG, 3 = PNG
  3834.                         $watermark_checked false;
  3835.                         if ($watermark_type == IMAGETYPE_GIF{
  3836.                             if (!function_exists('imagecreatefromgif')) {
  3837.                                 $this->error $this->translate('watermark_no_create_support'array('GIF'));
  3838.                             else {
  3839.                                 $filter @imagecreatefromgif($this->image_watermark);
  3840.                                 if (!$filter{
  3841.                                     $this->error $this->translate('watermark_create_error'array('GIF'));
  3842.                                 else {
  3843.                                     $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;watermark source image is GIF<br />';
  3844.                                     $watermark_checked true;
  3845.                                 }
  3846.                             }
  3847.                         else if ($watermark_type == IMAGETYPE_JPEG{
  3848.                             if (!function_exists('imagecreatefromjpeg')) {
  3849.                                 $this->error $this->translate('watermark_no_create_support'array('JPEG'));
  3850.                             else {
  3851.                                 $filter @imagecreatefromjpeg($this->image_watermark);
  3852.                                 if (!$filter{
  3853.                                     $this->error $this->translate('watermark_create_error'array('JPEG'));
  3854.                                 else {
  3855.                                     $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;watermark source image is JPEG<br />';
  3856.                                     $watermark_checked true;
  3857.                                 }
  3858.                             }
  3859.                         else if ($watermark_type == IMAGETYPE_PNG{
  3860.                             if (!function_exists('imagecreatefrompng')) {
  3861.                                 $this->error $this->translate('watermark_no_create_support'array('PNG'));
  3862.                             else {
  3863.                                 $filter @imagecreatefrompng($this->image_watermark);
  3864.                                 if (!$filter{
  3865.                                     $this->error $this->translate('watermark_create_error'array('PNG'));
  3866.                                 else {
  3867.                                     $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;watermark source image is PNG<br />';
  3868.                                     $watermark_checked true;
  3869.                                 }
  3870.                             }
  3871.                         else if ($watermark_type == IMAGETYPE_BMP{
  3872.                             if (!method_exists($this'imagecreatefrombmp')) {
  3873.                                 $this->error $this->translate('watermark_no_create_support'array('BMP'));
  3874.                             else {
  3875.                                 $filter @$this->imagecreatefrombmp($this->image_watermark);
  3876.                                 if (!$filter{
  3877.                                     $this->error $this->translate('watermark_create_error'array('BMP'));
  3878.                                 else {
  3879.                                     $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;watermark source image is BMP<br />';
  3880.                                     $watermark_checked true;
  3881.                                 }
  3882.                             }
  3883.                         else {
  3884.                             $this->error $this->translate('watermark_invalid');
  3885.                         }
  3886.                         if ($watermark_checked{
  3887.                             $watermark_width  imagesx($filter);
  3888.                             $watermark_height imagesy($filter);
  3889.                             $watermark_x 0;
  3890.                             $watermark_y 0;
  3891.                             if (is_numeric($this->image_watermark_x)) {
  3892.                                 if ($this->image_watermark_x 0{
  3893.                                     $watermark_x $this->image_dst_x $watermark_width $this->image_watermark_x;
  3894.                                 else {
  3895.                                     $watermark_x $this->image_watermark_x;
  3896.                                 }
  3897.                             else {
  3898.                                 if (strpos($this->image_watermark_position'r'!== false{
  3899.                                     $watermark_x $this->image_dst_x $watermark_width;
  3900.                                 else if (strpos($this->image_watermark_position'l'!== false{
  3901.                                     $watermark_x 0;
  3902.                                 else {
  3903.                                     $watermark_x ($this->image_dst_x $watermark_width2;
  3904.                                 }
  3905.                             }
  3906.                             if (is_numeric($this->image_watermark_y)) {
  3907.                                 if ($this->image_watermark_y 0{
  3908.                                     $watermark_y $this->image_dst_y $watermark_height $this->image_watermark_y;
  3909.                                 else {
  3910.                                     $watermark_y $this->image_watermark_y;
  3911.                                 }
  3912.                             else {
  3913.                                 if (strpos($this->image_watermark_position'b'!== false{
  3914.                                     $watermark_y $this->image_dst_y $watermark_height;
  3915.                                 else if (strpos($this->image_watermark_position't'!== false{
  3916.                                     $watermark_y 0;
  3917.                                 else {
  3918.                                     $watermark_y ($this->image_dst_y $watermark_height2;
  3919.                                 }
  3920.                             }
  3921.                             imagecopyresampled ($image_dst$filter$watermark_x$watermark_y00$watermark_width$watermark_height$watermark_width$watermark_height);
  3922.                         else {
  3923.                             $this->error $this->translate('watermark_invalid');
  3924.                         }
  3925.                     }
  3926.  
  3927.                     // add text
  3928.                     if (!empty($this->image_text)) {
  3929.                         $this->log .= '- add text<br />';
  3930.  
  3931.                         // calculate sizes in human readable format
  3932.                         $src_size       $this->file_src_size 1024;
  3933.                         $src_size_mb    number_format($src_size 10241"."" ");
  3934.                         $src_size_kb    number_format($src_size1"."" ");
  3935.                         $src_size_human ($src_size 1024 $src_size_mb " MB" $src_size_kb " kb");
  3936.  
  3937.                         $this->image_text str_replace(
  3938.                             array('[src_name]',
  3939.                                   '[src_name_body]',
  3940.                                   '[src_name_ext]',
  3941.                                   '[src_pathname]',
  3942.                                   '[src_mime]',
  3943.                                   '[src_size]',
  3944.                                   '[src_size_kb]',
  3945.                                   '[src_size_mb]',
  3946.                                   '[src_size_human]',
  3947.                                   '[src_x]',
  3948.                                   '[src_y]',
  3949.                                   '[src_pixels]',
  3950.                                   '[src_type]',
  3951.                                   '[src_bits]',
  3952.                                   '[dst_path]',
  3953.                                   '[dst_name_body]',
  3954.                                   '[dst_name_ext]',
  3955.                                   '[dst_name]',
  3956.                                   '[dst_pathname]',
  3957.                                   '[dst_x]',
  3958.                                   '[dst_y]',
  3959.                                   '[date]',
  3960.                                   '[time]',
  3961.                                   '[host]',
  3962.                                   '[server]',
  3963.                                   '[ip]',
  3964.                                   '[gd_version]'),
  3965.                             array($this->file_src_name,
  3966.                                   $this->file_src_name_body,
  3967.                                   $this->file_src_name_ext,
  3968.                                   $this->file_src_pathname,
  3969.                                   $this->file_src_mime,
  3970.                                   $this->file_src_size,
  3971.                                   $src_size_kb,
  3972.                                   $src_size_mb,
  3973.                                   $src_size_human,
  3974.                                   $this->image_src_x,
  3975.                                   $this->image_src_y,
  3976.                                   $this->image_src_pixels,
  3977.                                   $this->image_src_type,
  3978.                                   $this->image_src_bits,
  3979.                                   $this->file_dst_path,
  3980.                                   $this->file_dst_name_body,
  3981.                                   $this->file_dst_name_ext,
  3982.                                   $this->file_dst_name,
  3983.                                   $this->file_dst_pathname,
  3984.                                   $this->image_dst_x,
  3985.                                   $this->image_dst_y,
  3986.                                   date('Y-m-d'),
  3987.                                   date('H:i:s'),
  3988.                                   (isset($_SERVER['HTTP_HOST']$_SERVER['HTTP_HOST''n/a'),
  3989.                                   (isset($_SERVER['SERVER_NAME']$_SERVER['SERVER_NAME''n/a'),
  3990.                                   (isset($_SERVER['REMOTE_ADDR']$_SERVER['REMOTE_ADDR''n/a'),
  3991.                                   $this->gdversion(true)),
  3992.                             $this->image_text);
  3993.  
  3994.                         if (!is_numeric($this->image_text_padding)) $this->image_text_padding 0;
  3995.                         if (!is_numeric($this->image_text_line_spacing)) $this->image_text_line_spacing 0;
  3996.                         if (!is_numeric($this->image_text_padding_x)) $this->image_text_padding_x $this->image_text_padding;
  3997.                         if (!is_numeric($this->image_text_padding_y)) $this->image_text_padding_y $this->image_text_padding;
  3998.                         $this->image_text_position strtolower($this->image_text_position);
  3999.                         $this->image_text_direction strtolower($this->image_text_direction);
  4000.                         $this->image_text_alignment strtolower($this->image_text_alignment);
  4001.  
  4002.                         // if the font is a string, we assume that we might want to load a font
  4003.                         if (!is_numeric($this->image_text_font&& strlen($this->image_text_font&& substr(strtolower($this->image_text_font)-4== '.gdf'{
  4004.                             $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;try to load font ' $this->image_text_font '... ';
  4005.                             if ($this->image_text_font @imageloadfont($this->image_text_font)) {
  4006.                                 $this->log .=  'success<br />';
  4007.                             else {
  4008.                                 $this->log .=  'error<br />';
  4009.                                 $this->image_text_font 5;
  4010.                             }
  4011.                         }
  4012.  
  4013.                         $text explode("\n"$this->image_text);
  4014.                         $char_width imagefontwidth($this->image_text_font);
  4015.                         $char_height imagefontheight($this->image_text_font);
  4016.                         $text_height 0;
  4017.                         $text_width 0;
  4018.                         $line_height 0;
  4019.                         $line_width 0;
  4020.  
  4021.                         foreach ($text as $k => $v{
  4022.                             if ($this->image_text_direction == 'v'{
  4023.                                 $h ($char_width strlen($v));
  4024.                                 if ($h $text_height$text_height $h;
  4025.                                 $line_width $char_height;
  4026.                                 $text_width += $line_width ($k (sizeof($text)-1$this->image_text_line_spacing 0);
  4027.                             else {
  4028.                                 $w ($char_width strlen($v));
  4029.                                 if ($w $text_width$text_width $w;
  4030.                                 $line_height $char_height;
  4031.                                 $text_height += $line_height ($k (sizeof($text)-1$this->image_text_line_spacing 0);
  4032.                             }
  4033.                         }
  4034.                         $text_width  += ($this->image_text_padding_x);
  4035.                         $text_height += ($this->image_text_padding_y);
  4036.                         $text_x 0;
  4037.                         $text_y 0;
  4038.                         if (is_numeric($this->image_text_x)) {
  4039.                             if ($this->image_text_x 0{
  4040.                                 $text_x $this->image_dst_x $text_width $this->image_text_x;
  4041.                             else {
  4042.                                 $text_x $this->image_text_x;
  4043.                             }
  4044.                         else {
  4045.                             if (strpos($this->image_text_position'r'!== false{
  4046.                                 $text_x $this->image_dst_x $text_width;
  4047.                             else if (strpos($this->image_text_position'l'!== false{
  4048.                                 $text_x 0;
  4049.                             else {
  4050.                                 $text_x ($this->image_dst_x $text_width2;
  4051.                             }
  4052.                         }
  4053.                         if (is_numeric($this->image_text_y)) {
  4054.                             if ($this->image_text_y 0{
  4055.                                 $text_y $this->image_dst_y $text_height $this->image_text_y;
  4056.                             else {
  4057.                                 $text_y $this->image_text_y;
  4058.                             }
  4059.                         else {
  4060.                             if (strpos($this->image_text_position'b'!== false{
  4061.                                 $text_y $this->image_dst_y $text_height;
  4062.                             else if (strpos($this->image_text_position't'!== false{
  4063.                                 $text_y 0;
  4064.                             else {
  4065.                                 $text_y ($this->image_dst_y $text_height2;
  4066.                             }
  4067.                         }
  4068.  
  4069.                         // add a background, maybe transparent
  4070.                         if (!empty($this->image_text_background)) {
  4071.                             list($red$green$blue$this->getcolors($this->image_text_background);
  4072.                             if ($gd_version >= && (is_numeric($this->image_text_background_percent)) && $this->image_text_background_percent >= && $this->image_text_background_percent <= 100{
  4073.                                 $filter imagecreatetruecolor($text_width$text_height);
  4074.                                 $background_color imagecolorallocate($filter$red$green$blue);
  4075.                                 imagefilledrectangle($filter00$text_width$text_height$background_color);
  4076.                                 $this->imagecopymergealpha($image_dst$filter$text_x$text_y00$text_width$text_height$this->image_text_background_percent);
  4077.                                 imagedestroy($filter);
  4078.                             else {
  4079.                                 $background_color imagecolorallocate($image_dst ,$red$green$blue);
  4080.                                 imagefilledrectangle($image_dst$text_x$text_y$text_x $text_width$text_y $text_height$background_color);
  4081.                             }
  4082.                         }
  4083.  
  4084.                         $text_x += $this->image_text_padding_x;
  4085.                         $text_y += $this->image_text_padding_y;
  4086.                         $t_width $text_width ($this->image_text_padding_x);
  4087.                         $t_height $text_height ($this->image_text_padding_y);
  4088.                         list($red$green$blue$this->getcolors($this->image_text_color);
  4089.  
  4090.                         // add the text, maybe transparent
  4091.                         if ($gd_version >= && (is_numeric($this->image_text_percent)) && $this->image_text_percent >= && $this->image_text_percent <= 100{
  4092.                             if ($t_width 0$t_width 0;
  4093.                             if ($t_height 0$t_height 0;
  4094.                             $filter $this->imagecreatenew($t_width$t_heightfalsetrue);
  4095.                             $text_color imagecolorallocate($filter ,$red$green$blue);
  4096.  
  4097.                             foreach ($text as $k => $v{
  4098.                                 if ($this->image_text_direction == 'v'{
  4099.                                     imagestringup($filter,
  4100.                                                   $this->image_text_font,
  4101.                                                   $k ($line_width  ($k && $k (sizeof($text)) $this->image_text_line_spacing 0)),
  4102.                                                   $text_height ($this->image_text_padding_y($this->image_text_alignment == 'l' (($t_height strlen($v$char_width($this->image_text_alignment == 'r' 2))) ,
  4103.                                                   $v,
  4104.                                                   $text_color);
  4105.                                 else {
  4106.                                     imagestring($filter,
  4107.                                                 $this->image_text_font,
  4108.                                                 ($this->image_text_alignment == 'l' (($t_width strlen($v$char_width($this->image_text_alignment == 'r' 2))),
  4109.                                                 $k ($line_height  ($k && $k (sizeof($text)) $this->image_text_line_spacing 0)),
  4110.                                                 $v,
  4111.                                                 $text_color);
  4112.                                 }
  4113.                             }
  4114.                             $this->imagecopymergealpha($image_dst$filter$text_x$text_y00$t_width$t_height$this->image_text_percent);
  4115.                             imagedestroy($filter);
  4116.  
  4117.                         else {
  4118.                             $text_color imageColorAllocate($image_dst ,$red$green$blue);
  4119.                             foreach ($text as $k => $v{
  4120.                                 if ($this->image_text_direction == 'v'{
  4121.                                     imagestringup($image_dst,
  4122.                                                   $this->image_text_font,
  4123.                                                   $text_x $k ($line_width  ($k && $k (sizeof($text)) $this->image_text_line_spacing 0)),
  4124.                                                   $text_y $text_height ($this->image_text_padding_y($this->image_text_alignment == 'l' (($t_height strlen($v$char_width($this->image_text_alignment == 'r' 2))),
  4125.                                                   $v,
  4126.                                                   $text_color);
  4127.                                 else {
  4128.                                     imagestring($image_dst,
  4129.                                                 $this->image_text_font,
  4130.                                                 $text_x ($this->image_text_alignment == 'l' (($t_width strlen($v$char_width($this->image_text_alignment == 'r' 2))),
  4131.                                                 $text_y $k ($line_height  ($k && $k (sizeof($text)) $this->image_text_line_spacing 0)),
  4132.                                                 $v,
  4133.                                                 $text_color);
  4134.                                 }
  4135.                             }
  4136.                         }
  4137.                     }
  4138.  
  4139.                     // add a reflection
  4140.                     if ($this->image_reflection_height{
  4141.                         $this->log .= '- add reflection : ' $this->image_reflection_height '<br />';
  4142.                         // we decode image_reflection_height, which can be a integer, a string in pixels or percentage
  4143.                         $image_reflection_height $this->image_reflection_height;
  4144.                         if (strpos($image_reflection_height'%')>0$image_reflection_height $this->image_dst_y (str_replace('%','',$image_reflection_height 100));
  4145.                         if (strpos($image_reflection_height'px')>0$image_reflection_height str_replace('px','',$image_reflection_height);
  4146.                         $image_reflection_height = (int) $image_reflection_height;
  4147.                         if ($image_reflection_height $this->image_dst_y$image_reflection_height $this->image_dst_y;
  4148.                         if (empty($this->image_reflection_opacity)) $this->image_reflection_opacity 60;
  4149.                         // create the new destination image
  4150.                         $tmp $this->imagecreatenew($this->image_dst_x$this->image_dst_y $image_reflection_height $this->image_reflection_spacetrue);
  4151.                         $transparency $this->image_reflection_opacity;
  4152.  
  4153.                         // copy the original image
  4154.                         imagecopy($tmp$image_dst0000$this->image_dst_x$this->image_dst_y ($this->image_reflection_space $this->image_reflection_space 0));
  4155.  
  4156.                         // we have to make sure the extra bit is the right color, or transparent
  4157.                         if ($image_reflection_height $this->image_reflection_space 0{
  4158.                             // use the background color if present
  4159.                             if (!empty($this->image_background_color)) {
  4160.                                 list($red$green$blue$this->getcolors($this->image_background_color);
  4161.                                 $fill imagecolorallocate($tmp$red$green$blue);
  4162.                             else {
  4163.                                 $fill imagecolorallocatealpha($tmp000127);
  4164.                             }
  4165.                             // fill in from the edge of the extra bit
  4166.                             imagefill($tmpround($this->image_dst_x 2)$this->image_dst_y $image_reflection_height $this->image_reflection_space 1$fill);
  4167.                         }
  4168.  
  4169.                         // copy the reflection
  4170.                         for ($y 0$y $image_reflection_height$y++{
  4171.                             for ($x 0$x $this->image_dst_x$x++{
  4172.                                 $pixel_b imagecolorsforindex($tmpimagecolorat($tmp$x$y $this->image_dst_y $this->image_reflection_space));
  4173.                                 $pixel_o imagecolorsforindex($image_dstimagecolorat($image_dst$x$this->image_dst_y $y ($this->image_reflection_space $this->image_reflection_space 0)));
  4174.                                 $alpha_o ($pixel_o['alpha'127);
  4175.                                 $alpha_b ($pixel_b['alpha'127);
  4176.                                 $opacity $alpha_o $transparency 100;
  4177.                                 if ($opacity 0{
  4178.                                     $red   round((($pixel_o['red']   $opacity($pixel_b['red']  $alpha_b($alpha_b $opacity));
  4179.                                     $green round((($pixel_o['green'$opacity($pixel_b['green']$alpha_b($alpha_b $opacity));
  4180.                                     $blue  round((($pixel_o['blue']  $opacity($pixel_b['blue'$alpha_b($alpha_b $opacity));
  4181.                                     $alpha ($opacity $alpha_b);
  4182.                                     if ($alpha 1$alpha 1;
  4183.                                     $alpha =  round(($alpha127);
  4184.                                     $color imagecolorallocatealpha($tmp$red$green$blue$alpha);
  4185.                                     imagesetpixel($tmp$x$y $this->image_dst_y $this->image_reflection_space$color);
  4186.                                 }
  4187.                             }
  4188.                             if ($transparency 0$transparency $transparency ($this->image_reflection_opacity $image_reflection_height);
  4189.                         }
  4190.  
  4191.                         // copy the resulting image into the destination image
  4192.                         $this->image_dst_y $this->image_dst_y $image_reflection_height $this->image_reflection_space;
  4193.                         $image_dst $this->imagetransfer($tmp$image_dst);
  4194.                     }
  4195.  
  4196.                     // reduce the JPEG image to a set desired size
  4197.                     if (is_numeric($this->jpeg_size&& $this->jpeg_size && ($this->image_convert == 'jpeg' || $this->image_convert == 'jpg')) {
  4198.                         // inspired by: JPEGReducer class version 1, 25 November 2004, Author: Huda M ElMatsani, justhuda at netscape dot net
  4199.                         $this->log .= '- JPEG desired file size : ' $this->jpeg_size '<br />';
  4200.                         // calculate size of each image. 75%, 50%, and 25% quality
  4201.                         ob_start()imagejpeg($image_dst,'',75);  $buffer ob_get_contents()ob_end_clean();
  4202.                         $size75 strlen($buffer);
  4203.                         ob_start()imagejpeg($image_dst,'',50);  $buffer ob_get_contents()ob_end_clean();
  4204.                         $size50 strlen($buffer);
  4205.                         ob_start()imagejpeg($image_dst,'',25);  $buffer ob_get_contents()ob_end_clean();
  4206.                         $size25 strlen($buffer);
  4207.  
  4208.                         // calculate gradient of size reduction by quality
  4209.                         $mgrad1 25 ($size50-$size25);
  4210.                         $mgrad2 25 ($size75-$size50);
  4211.                         $mgrad3 50 ($size75-$size25);
  4212.                         $mgrad  ($mgrad1 $mgrad2 $mgrad33;
  4213.                         // result of approx. quality factor for expected size
  4214.                         $q_factor round($mgrad ($this->jpeg_size $size5050);
  4215.  
  4216.                         if ($q_factor<1{
  4217.                             $this->jpeg_quality=1;
  4218.                         elseif ($q_factor>100{
  4219.                             $this->jpeg_quality=100;
  4220.                         else {
  4221.                             $this->jpeg_quality=$q_factor;
  4222.                         }
  4223.                         $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;JPEG quality factor set to ' $this->jpeg_quality '<br />';
  4224.                     }
  4225.  
  4226.                     // converts image from true color, and fix transparency if needed
  4227.                     $this->log .= '- converting...<br />';
  4228.                     switch($this->image_convert{
  4229.                         case 'gif':
  4230.                             // if the image is true color, we convert it to a palette
  4231.                             if (imageistruecolor($image_dst)) {
  4232.                                 $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;true color to palette<br />';
  4233.                                 // creates a black and white mask
  4234.                                 $mask array(array());
  4235.                                 for ($x 0$x $this->image_dst_x$x++{
  4236.                                     for ($y 0$y $this->image_dst_y$y++{
  4237.                                         $pixel imagecolorsforindex($image_dstimagecolorat($image_dst$x$y));
  4238.                                         $mask[$x][$y$pixel['alpha'];
  4239.                                     }
  4240.                                 }
  4241.                                 list($red$green$blue$this->getcolors($this->image_default_color);
  4242.                                 // first, we merge the image with the background color, so we know which colors we will have
  4243.                                 for ($x 0$x $this->image_dst_x$x++{
  4244.                                     for ($y 0$y $this->image_dst_y$y++{
  4245.                                         if ($mask[$x][$y0){
  4246.                                             // we have some transparency. we combine the color with the default color
  4247.                                             $pixel imagecolorsforindex($image_dstimagecolorat($image_dst$x$y));
  4248.                                             $alpha ($mask[$x][$y127);
  4249.                                             $pixel['red'round(($pixel['red'(-$alpha$red ($alpha)));
  4250.                                             $pixel['green'round(($pixel['green'(-$alpha$green ($alpha)));
  4251.                                             $pixel['blue'round(($pixel['blue'(-$alpha$blue ($alpha)));
  4252.                                             $color imagecolorallocate($image_dst$pixel['red']$pixel['green']$pixel['blue']);
  4253.                                             imagesetpixel($image_dst$x$y$color);
  4254.                                         }
  4255.                                     }
  4256.                                 }
  4257.                                 // transfrom the true color image into palette, with it merged default color in
  4258.                                 // we will have the best color possible, including the background
  4259.                                 if (empty($this->image_background_color)) {
  4260.                                     imagetruecolortopalette($image_dsttrue255);
  4261.                                     $transparency imagecolorallocate($image_dst2541253);
  4262.                                     imagecolortransparent($image_dst$transparency);
  4263.                                     // make the transparent areas transparent
  4264.                                     for ($x 0$x $this->image_dst_x$x++{
  4265.                                         for ($y 0$y $this->image_dst_y$y++{
  4266.                                             // we test wether we have enough opacity to justify keeping the color
  4267.                                             if ($mask[$x][$y120imagesetpixel($image_dst$x$y$transparency);
  4268.                                         }
  4269.                                     }
  4270.                                 }
  4271.                                 unset($mask);
  4272.                             }
  4273.                             break;
  4274.                         case 'jpg':
  4275.                         case 'bmp':
  4276.                             // if the image doesn't support any transparency, then we merge it with the default color
  4277.                             $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;fills in transparency with default color<br />';
  4278.                             list($red$green$blue$this->getcolors($this->image_default_color);
  4279.                             $transparency imagecolorallocate($image_dst$red$green$blue);
  4280.                             // make the transaparent areas transparent
  4281.                             for ($x 0$x $this->image_dst_x$x++{
  4282.                                 for ($y 0$y $this->image_dst_y$y++{
  4283.                                     // we test wether we have some transparency, in which case we will merge the colors
  4284.                                     if (imageistruecolor($image_dst)) {
  4285.                                         $rgba imagecolorat($image_dst$x$y);
  4286.                                         $pixel array('red' => ($rgba >> 160xFF,
  4287.                                                        'green' => ($rgba >> 80xFF,
  4288.                                                        'blue' => $rgba 0xFF,
  4289.                                                        'alpha' => ($rgba 0x7F000000>> 24);
  4290.                                     else {
  4291.                                         $pixel imagecolorsforindex($image_dstimagecolorat($image_dst$x$y));
  4292.                                     }
  4293.                                     if ($pixel['alpha'== 127{
  4294.                                         // we have full transparency. we make the pixel transparent
  4295.                                         imagesetpixel($image_dst$x$y$transparency);
  4296.                                     else if ($pixel['alpha'0{
  4297.                                         // we have some transparency. we combine the color with the default color
  4298.                                         $alpha ($pixel['alpha'127);
  4299.                                         $pixel['red'round(($pixel['red'(-$alpha$red ($alpha)));
  4300.                                         $pixel['green'round(($pixel['green'(-$alpha$green ($alpha)));
  4301.                                         $pixel['blue'round(($pixel['blue'(-$alpha$blue ($alpha)));
  4302.                                         $color imagecolorclosest($image_dst$pixel['red']$pixel['green']$pixel['blue']);
  4303.                                         imagesetpixel($image_dst$x$y$color);
  4304.                                     }
  4305.                                 }
  4306.                             }
  4307.  
  4308.                             break;
  4309.                         default:
  4310.                             break;
  4311.                     }
  4312.  
  4313.                     // outputs image
  4314.                     $this->log .= '- saving image...<br />';
  4315.                     switch($this->image_convert{
  4316.                         case 'jpeg':
  4317.                         case 'jpg':
  4318.                             if (!$return_mode{
  4319.                                 $result @imagejpeg($image_dst$this->file_dst_pathname$this->jpeg_quality);
  4320.                             else {
  4321.                                 ob_start();
  4322.                                 $result @imagejpeg($image_dst''$this->jpeg_quality);
  4323.                                 $return_content ob_get_contents();
  4324.                                 ob_end_clean();
  4325.                             }
  4326.                             if (!$result{
  4327.                                 $this->processed false;
  4328.                                 $this->error $this->translate('file_create'array('JPEG'));
  4329.                             else {
  4330.                                 $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;JPEG image created<br />';
  4331.                             }
  4332.                             break;
  4333.                         case 'png':
  4334.                             imagealphablending$image_dstfalse );
  4335.                             imagesavealpha$image_dsttrue );
  4336.                             if (!$return_mode{
  4337.                                 $result @imagepng($image_dst$this->file_dst_pathname);
  4338.                             else {
  4339.                                 ob_start();
  4340.                                 $result @imagepng($image_dst);
  4341.                                 $return_content ob_get_contents();
  4342.                                 ob_end_clean();
  4343.                             }
  4344.                             if (!$result{
  4345.                                 $this->processed false;
  4346.                                 $this->error $this->translate('file_create'array('PNG'));
  4347.                             else {
  4348.                                 $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;PNG image created<br />';
  4349.                             }
  4350.                             break;
  4351.                         case 'gif':
  4352.                             if (!$return_mode{
  4353.                                 $result @imagegif($image_dst$this->file_dst_pathname);
  4354.                             else {
  4355.                                 ob_start();
  4356.                                 $result @imagegif($image_dst);
  4357.                                 $return_content ob_get_contents();
  4358.                                 ob_end_clean();
  4359.                             }
  4360.                             if (!$result{
  4361.                                 $this->processed false;
  4362.                                 $this->error $this->translate('file_create'array('GIF'));
  4363.                             else {
  4364.                                 $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;GIF image created<br />';
  4365.                             }
  4366.                             break;
  4367.                         case 'bmp':
  4368.                             if (!$return_mode{
  4369.                                 $result $this->imagebmp($image_dst$this->file_dst_pathname);
  4370.                             else {
  4371.                                 ob_start();
  4372.                                 $result $this->imagebmp($image_dst);
  4373.                                 $return_content ob_get_contents();
  4374.                                 ob_end_clean();
  4375.                             }
  4376.                             if (!$result{
  4377.                                 $this->processed false;
  4378.                                 $this->error $this->translate('file_create'array('BMP'));
  4379.                             else {
  4380.                                 $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;BMP image created<br />';
  4381.                             }
  4382.                             break;
  4383.  
  4384.                         default:
  4385.                             $this->processed false;
  4386.                             $this->error $this->translate('no_conversion_type');
  4387.                     }
  4388.                     if ($this->processed{
  4389.                         if (is_resource($image_src)) imagedestroy($image_src);
  4390.                         if (is_resource($image_dst)) imagedestroy($image_dst);
  4391.                         $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;image objects destroyed<br />';
  4392.                     }
  4393.                 }
  4394.  
  4395.             else {
  4396.                 $this->log .= '- no image processing wanted<br />';
  4397.  
  4398.                 if (!$return_mode{
  4399.                     // copy the file to its final destination. we don't use move_uploaded_file here
  4400.                     // if we happen to have open_basedir restrictions, it is a temp file that we copy, not the original uploaded file
  4401.                     if (!copy($this->file_src_pathname$this->file_dst_pathname)) {
  4402.                         $this->processed false;
  4403.                         $this->error $this->translate('copy_failed');
  4404.                     }
  4405.                 else {
  4406.                     // returns the file, so that its content can be received by the caller
  4407.                     $return_content @file_get_contents($this->file_src_pathname);
  4408.                     if ($return_content === FALSE{
  4409.                         $this->processed false;
  4410.                         $this->error $this->translate('reading_failed');
  4411.                     }
  4412.                 }
  4413.             }
  4414.         }
  4415.  
  4416.         if ($this->processed{
  4417.             $this->log .= '- <b>process OK</b><br />';
  4418.         else {
  4419.             $this->log .= '- <b>error</b>: ' $this->error '<br />';
  4420.         }
  4421.  
  4422.         // we reinit all the vars
  4423.         $this->init();
  4424.  
  4425.         // we may return the image content
  4426.         if ($return_modereturn $return_content;
  4427.  
  4428.     }
  4429.  
  4430.     /**
  4431.      * Deletes the uploaded file from its temporary location
  4432.      *
  4433.      * When PHP uploads a file, it stores it in a temporary location.
  4434.      * When you {@link process} the file, you actually copy the resulting file to the given location, it doesn't alter the original file.
  4435.      * Once you have processed the file as many times as you wanted, you can delete the uploaded file.
  4436.      * If there is open_basedir restrictions, the uploaded file is in fact a temporary file
  4437.      *
  4438.      * You might want not to use this function if you work on local files, as it will delete the source file
  4439.      *
  4440.      * @access public
  4441.      */
  4442.     function clean({
  4443.         $this->log .= '<b>cleanup</b><br />';
  4444.         $this->log .= '- delete temp file '  $this->file_src_pathname '<br />';
  4445.         @unlink($this->file_src_pathname);
  4446.     }
  4447.  
  4448.  
  4449.     /**
  4450.      * Opens a BMP image
  4451.      *
  4452.      * This function has been written by DHKold, and is used with permission of the author
  4453.      *
  4454.      * @access public
  4455.      */
  4456.     function imagecreatefrombmp($filename{
  4457.         if ($f1 fopen($filename,"rb")) return false;
  4458.  
  4459.         $file unpack("vfile_type/Vfile_size/Vreserved/Vbitmap_offset"fread($f1,14));
  4460.         if ($file['file_type'!= 19778return false;
  4461.  
  4462.         $bmp unpack('Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel'.
  4463.                       '/Vcompression/Vsize_bitmap/Vhoriz_resolution'.
  4464.                       '/Vvert_resolution/Vcolors_used/Vcolors_important'fread($f1,40));
  4465.         $bmp['colors'pow(2,$bmp['bits_per_pixel']);
  4466.         if ($bmp['size_bitmap'== 0$bmp['size_bitmap'$file['file_size'$file['bitmap_offset'];
  4467.         $bmp['bytes_per_pixel'$bmp['bits_per_pixel']/8;
  4468.         $bmp['bytes_per_pixel2'ceil($bmp['bytes_per_pixel']);
  4469.         $bmp['decal'($bmp['width']*$bmp['bytes_per_pixel']/4);
  4470.         $bmp['decal'-= floor($bmp['width']*$bmp['bytes_per_pixel']/4);
  4471.         $bmp['decal'4-(4*$bmp['decal']);
  4472.         if ($bmp['decal'== 4$bmp['decal'0;
  4473.  
  4474.         $palette array();
  4475.         if ($bmp['colors'16777216{
  4476.             $palette unpack('V'.$bmp['colors']fread($f1,$bmp['colors']*4));
  4477.         }
  4478.  
  4479.         $im fread($f1,$bmp['size_bitmap']);
  4480.         $vide chr(0);
  4481.  
  4482.         $res imagecreatetruecolor($bmp['width'],$bmp['height']);
  4483.         $P 0;
  4484.         $Y $bmp['height']-1;
  4485.         while ($Y >= 0{
  4486.             $X=0;
  4487.             while ($X $bmp['width']{
  4488.                 if ($bmp['bits_per_pixel'== 24)
  4489.                     $color unpack("V",substr($im,$P,3).$vide);
  4490.                 elseif ($bmp['bits_per_pixel'== 16{
  4491.                     $color unpack("n",substr($im,$P,2));
  4492.                     $color[1$palette[$color[1]+1];
  4493.                 elseif ($bmp['bits_per_pixel'== 8{
  4494.                     $color unpack("n",$vide.substr($im,$P,1));
  4495.                     $color[1$palette[$color[1]+1];
  4496.                 elseif ($bmp['bits_per_pixel'== 4{
  4497.                     $color unpack("n",$vide.substr($im,floor($P),1));
  4498.                     if (($P*2)%== 0$color[1($color[1>> 4else $color[1($color[10x0F);
  4499.                     $color[1$palette[$color[1]+1];
  4500.                 elseif ($bmp['bits_per_pixel'== 1)  {
  4501.                     $color unpack("n",$vide.substr($im,floor($P),1));
  4502.                     if     (($P*8)%== 0$color[1=  $color[1]        >>7;
  4503.                     elseif (($P*8)%== 1$color[1($color[10x40)>>6;
  4504.                     elseif (($P*8)%== 2$color[1($color[10x20)>>5;
  4505.                     elseif (($P*8)%== 3$color[1($color[10x10)>>4;
  4506.                     elseif (($P*8)%== 4$color[1($color[10x8)>>3;
  4507.                     elseif (($P*8)%== 5$color[1($color[10x4)>>2;
  4508.                     elseif (($P*8)%== 6$color[1($color[10x2)>>1;
  4509.                     elseif (($P*8)%== 7$color[1($color[10x1);
  4510.                     $color[1$palette[$color[1]+1];
  4511.                 else
  4512.                     return FALSE;
  4513.                 imagesetpixel($res,$X,$Y,$color[1]);
  4514.                 $X++;
  4515.                 $P += $bmp['bytes_per_pixel'];
  4516.             }
  4517.             $Y--;
  4518.             $P+=$bmp['decal'];
  4519.         }
  4520.         fclose($f1);
  4521.         return $res;
  4522.     }
  4523.  
  4524.     /**
  4525.      * Saves a BMP image
  4526.      *
  4527.      * This function has been published on the PHP website, and can be used freely
  4528.      *
  4529.      * @access public
  4530.      */
  4531.     function imagebmp(&$im$filename ""{
  4532.  
  4533.         if (!$imreturn false;
  4534.         $w imagesx($im);
  4535.         $h imagesy($im);
  4536.         $result '';
  4537.  
  4538.         // if the image is not true color, we convert it first
  4539.         if (!imageistruecolor($im)) {
  4540.             $tmp imagecreatetruecolor($w$h);
  4541.             imagecopy($tmp$im0000$w$h);
  4542.             imagedestroy($im);
  4543.             $im $tmp;
  4544.         }
  4545.  
  4546.         $biBPLine $w 3;
  4547.         $biStride ($biBPLine 3~3;
  4548.         $biSizeImage $biStride $h;
  4549.         $bfOffBits 54;
  4550.         $bfSize $bfOffBits $biSizeImage;
  4551.  
  4552.         $result .= substr('BM'02);
  4553.         $result .=  pack ('VvvV'$bfSize00$bfOffBits);
  4554.         $result .= pack ('VVVvvVVVVVV'40$w$h1240$biSizeImage0000);
  4555.  
  4556.         $numpad $biStride $biBPLine;
  4557.         for ($y $h 1$y >= 0--$y{
  4558.             for ($x 0$x $w++$x{
  4559.                 $col imagecolorat ($im$x$y);
  4560.                 $result .=  substr(pack ('V'$col)03);
  4561.             }
  4562.             for ($i 0$i $numpad++$i)
  4563.                 $result .= pack ('C'0);
  4564.         }
  4565.  
  4566.         if($filename==""){
  4567.             echo $result;
  4568.         else {
  4569.             $file fopen($filename"wb");
  4570.             fwrite($file$result);
  4571.             fclose($file);
  4572.         }
  4573.         return true;
  4574.     }
  4575. }
  4576.  
  4577. ?>