Source for file class.upload.php 0.31

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