Source for file class.upload.php 0.30

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