* @license http://opensource.org/licenses/gpl-license.php GNU Public License * @copyright Colin Verot * @package cmf * @subpackage external */ /** * Class upload * * What does it do? * * It manages file uploads for you. In short, it manages the uploaded file, * and allows you to do whatever you want with the file, especially if it * is an image, and as many times as you want. * * It is the ideal class to quickly integrate file upload in your site. * If the file is an image, you can convert, resize, crop it in many ways. * You can also apply filters, add borders, text, watermarks, etc... * That's all you need for a gallery script for instance. Supported formats * are PNG, JPG, GIF and BMP. * * You can also use the class to work on local files, which is especially * useful to use the image manipulation features. * * The class works with PHP 4 and 5, and its error messages can * be localized at will. * * How does it work? * * You instanciate the class with the $_FILES['my_field'] array * where my_field is the field name from your upload form. * The class will check if the original file has been uploaded * to its temporary location (alternatively, you can instanciate * the class with a local filename). * * You can then set a number of processing variables to act on the file. * For instance, you can rename the file, and if it is an image, * convert and resize it in many ways. * You can also set what will the class do if the file already exists. * * Then you call the function {@link process} to actually perform the actions * according to the processing parameters you set above. * It will create new instances of the original file, * so the original file remains the same between each process. * The file will be manipulated, and copied to the given location. * The processing variables will be reseted once it is done. * * You can repeat setting up a new set of processing variables, * and calling {@link process} again as many times as you want. * When you have finished, you can call {@link clean} to delete * the original uploaded file. * * If you don't set any processing parameters and call {@link process} * just after instanciating the class. The uploaded file will be simply * copied to the given location without any alteration or checks. * * Don't forget to add enctype="multipart/form-data" in your form * tag
if you want your form to upload the file. * * How to use it?
* Create a simple HTML file, with a form such as: *
 * 
 *   
 *   
 * 
 * 
* Create a file called upload.php: *
 *  $handle = new upload($_FILES['image_field']);
 *  if ($handle->uploaded) {
 *      $handle->file_new_name_body   = 'image_resized';
 *      $handle->image_resize         = true;
 *      $handle->image_x              = 100;
 *      $handle->image_ratio_y        = true;
 *      $handle->process('/home/user/files/');
 *      if ($handle->processed) {
 *          echo 'image resized';
 *          $handle->clean();
 *      } else {
 *          echo 'error : ' . $handle->error;
 *      }
 *  }
 * 
* * How to process local files?
* Use the class as following, the rest being the same as above: *
 *  $handle = new upload('/home/user/myfile.jpg');
 * 
* * How to set the language?
* Instantiate the class with a second argument being the language code: *
 *  $handle = new upload($_FILES['image_field'], 'fr_FR');
 *  $handle = new upload('/home/user/myfile.jpg', 'fr_FR');
 * 
* * How to output the resulting file or picture directly to the browser?
* Simply call {@link process}() without an argument (or with null as first argument): *
 *  $handle = new upload($_FILES['image_field']);
 *  header('Content-type: ' . $handle->file_src_mime);
 *  echo $handle->Process();
 *  die();
 * 
* Or if you want to force the download of the file: *
 *  $handle = new upload($_FILES['image_field']);
 *  header('Content-type: ' . $handle->file_src_mime);
 *  header("Content-Disposition: attachment; filename=".rawurlencode($handle->file_src_name).";");
 *  echo $handle->Process();
 *  die();
 * 
* * Processing parameters (reseted after each process) * * * The following eight settings can be used to invalidate an upload if the file is an image (note that open_basedir restrictions prevent the use of these settings) * * * The following variables are used only if {@link image_resize} == true * * Use either one of the following * * The following image manipulations require GD2+ * * * * * * * * Values that can be read before calling {@link process}() * * If the file is a supported image type (and open_basedir restrictions allow it) * * * Values that can be read before after {@link process}() * * If the file is a supported image type * * * Requirements * * Most of the image operations require GD. GD2 is greatly recommended * * The class is compatible with PHP 4.3+, and compatible with PHP5 * * Changelog * * * @package cmf * @subpackage external */ class upload { /** * Uploaded file name * * @access public * @var string */ var $file_src_name; /** * Uploaded file name body (i.e. without extension) * * @access public * @var string */ var $file_src_name_body; /** * Uploaded file name extension * * @access public * @var string */ var $file_src_name_ext; /** * Uploaded file MIME type * * @access public * @var string */ var $file_src_mime; /** * Uploaded file size, in bytes * * @access public * @var double */ var $file_src_size; /** * Holds eventual PHP error code from $_FILES * * @access public * @var string */ var $file_src_error; /** * Uloaded file name, including server path * * @access private * @var string */ var $file_src_pathname; /** * Uloaded file name temporary copy * * @access private * @var string */ var $file_src_temp; /** * Destination file name * * @access private * @var string */ var $file_dst_path; /** * Destination file name * * @access public * @var string */ var $file_dst_name; /** * Destination file name body (i.e. without extension) * * @access public * @var string */ var $file_dst_name_body; /** * Destination file extension * * @access public * @var string */ var $file_dst_name_ext; /** * Destination file name, including path * * @access private * @var string */ var $file_dst_pathname; /** * Source image width * * @access private * @var integer */ var $image_src_x; /** * Source image height * * @access private * @var integer */ var $image_src_y; /** * Source image color depth * * @access private * @var integer */ var $image_src_bits; /** * Number of pixels * * @access private * @var long */ var $image_src_pixels; /** * Type of image (png, gif, jpg or bmp) * * @access private * @var string */ var $image_src_type; /** * Destination image width * * @access private * @var integer */ var $image_dst_x; /** * Destination image height * * @access private * @var integer */ var $image_dst_y; /** * Supported image formats * * @access private * @var array */ var $image_supported; /** * Flag to determine if the source file is an image * * @access private * @var boolean */ var $file_is_image; /** * Flag set after instanciating the class * * Indicates if the file has been uploaded properly * * @access public * @var bool */ var $uploaded; /** * Flag stopping PHP upload checks * * Indicates whether we instanciated the class with a filename, in which case * we will not check on the validity of the PHP *upload* * * This flag is automatically set to true when working on a local file * * Warning: for uploads, this flag MUST be set to false for security reason * * @access public * @var bool */ var $no_upload_check; /** * Flag set after calling a process * * Indicates if the processing, and copy of the resulting file went OK * * @access public * @var bool */ var $processed; /** * Holds eventual error message in plain english * * @access public * @var string */ var $error; /** * Holds an HTML formatted log * * @access public * @var string */ var $log; // overiddable processing variables /** * Set this variable to replace the name body (i.e. without extension) * * @access public * @var string */ var $file_new_name_body; /** * Set this variable to add a string to the faile name body * * @access public * @var string */ var $file_name_body_add; /** * Set this variable to change the file extension * * @access public * @var string */ var $file_new_name_ext; /** * Set this variable to format the filename (spaces changed to _) * * @access public * @var boolean */ var $file_safe_name; /** * Set this variable to false if you don't want to check the MIME against the allowed list * * This variable is set to true by default for security reason * * @access public * @var boolean */ var $mime_check; /** * Set this variable to true if you want to check the MIME type against a mime_magic file * * This variable is set to false by default as many systems don't have mime_magic installed or properly set * * @access public * @var boolean */ var $mime_magic_check; /** * Set this variable to false if you don't want to turn dangerous scripts into simple text files * * @access public * @var boolean */ var $no_script; /** * Set this variable to true to allow automatic renaming of the file * if the file already exists * * Default value is true * * For instance, on uploading foo.ext,
* if foo.ext already exists, upload will be renamed foo_1.ext
* and if foo_1.ext already exists, upload will be renamed foo_2.ext
* * @access public * @var bool */ var $file_auto_rename; /** * Set this variable to true to allow automatic creation of the destination * directory if it is missing (works recursively) * * Default value is true * * @access public * @var bool */ var $dir_auto_create; /** * Set this variable to true to allow automatic chmod of the destination * directory if it is not writeable * * Default value is true * * @access public * @var bool */ var $dir_auto_chmod; /** * Set this variable to the default chmod you want the class to use * when creating directories, or attempting to write in a directory * * Default value is 0777 (without quotes) * * @access public * @var bool */ var $dir_chmod; /** * Set this variable tu true to allow overwriting of an existing file * * Default value is false, so no files will be overwritten * * @access public * @var bool */ var $file_overwrite; /** * Set this variable to change the maximum size in bytes for an uploaded file * * Default value is the value upload_max_filesize from php.ini * * @access public * @var double */ var $file_max_size; /** * Set this variable to true to resize the file if it is an image * * You will probably want to set {@link image_x} and {@link image_y}, and maybe one of the ratio variables * * Default value is false (no resizing) * * @access public * @var bool */ var $image_resize; /** * Set this variable to convert the file if it is an image * * Possibles values are : ''; 'png'; 'jpeg'; 'gif'; 'bmp' * * Default value is '' (no conversion)
* If {@link resize} is true, {@link convert} will be set to the source file extension * * @access public * @var string */ var $image_convert; /** * Set this variable to the wanted (or maximum/minimum) width for the processed image, in pixels * * Default value is 150 * * @access public * @var integer */ var $image_x; /** * Set this variable to the wanted (or maximum/minimum) height for the processed image, in pixels * * Default value is 150 * * @access public * @var integer */ var $image_y; /** * Set this variable to keep the original size ratio to fit within {@link image_x} x {@link image_y} * * Default value is false * * @access public * @var bool */ var $image_ratio; /** * Set this variable to keep the original size ratio to fit within {@link image_x} x {@link image_y} * * The image will be resized as to fill the whole space, and excedent will be cropped * * Value can also be a string, one or more character from 'TBLR' (top, bottom, left and right) * If set as a string, it determines which side of the image is kept while cropping. * By default, the part of the image kept is in the center, i.e. it crops equally on both sides * * Default value is false * * @access public * @var mixed */ var $image_ratio_crop; /** * Set this variable to keep the original size ratio to fit within {@link image_x} x {@link image_y} * * The image will be resized to fit entirely in the space, and the rest will be colored. * The default color is white, but can be set with {@link image_default_color} * * Value can also be a string, one or more character from 'TBLR' (top, bottom, left and right) * If set as a string, it determines in which side of the space the image is displayed. * By default, the image is displayed in the center, i.e. it fills the remaining space equally on both sides * * Default value is false * * @access public * @var mixed */ var $image_ratio_fill; /** * Set this variable to a number of pixels so that {@link image_x} and {@link image_y} are the best match possible * * The image will be resized to have approximatively the number of pixels * The aspect ratio wil be conserved * * Default value is false * * @access public * @var mixed */ var $image_ratio_pixels; /** * Set this variable to keep the original size ratio to fit within {@link image_x} x {@link image_y}, * but only if original image is bigger * * Default value is false * * @access public * @var bool */ var $image_ratio_no_zoom_in; /** * Set this variable to keep the original size ratio to fit within {@link image_x} x {@link image_y}, * but only if original image is smaller * * Default value is false * * @access public * @var bool */ var $image_ratio_no_zoom_out; /** * Set this variable to calculate {@link image_x} automatically , using {@link image_y} and conserving ratio * * Default value is false * * @access public * @var bool */ var $image_ratio_x; /** * Set this variable to calculate {@link image_y} automatically , using {@link image_x} and conserving ratio * * Default value is false * * @access public * @var bool */ var $image_ratio_y; /** * Set this variable to set a maximum image width, above which the upload will be invalid * * Default value is null * * @access public * @var integer */ var $image_max_width; /** * Set this variable to set a maximum image height, above which the upload will be invalid * * Default value is null * * @access public * @var integer */ var $image_max_height; /** * Set this variable to set a maximum number of pixels for an image, above which the upload will be invalid * * Default value is null * * @access public * @var long */ var $image_max_pixels; /** * Set this variable to set a maximum image aspect ratio, above which the upload will be invalid * * Note that ratio = width / height * * Default value is null * * @access public * @var float */ var $image_max_ratio; /** * Set this variable to set a minimum image width, below which the upload will be invalid * * Default value is null * * @access public * @var integer */ var $image_min_width; /** * Set this variable to set a minimum image height, below which the upload will be invalid * * Default value is null * * @access public * @var integer */ var $image_min_height; /** * Set this variable to set a minimum number of pixels for an image, below which the upload will be invalid * * Default value is null * * @access public * @var long */ var $image_min_pixels; /** * Set this variable to set a minimum image aspect ratio, below which the upload will be invalid * * Note that ratio = width / height * * Default value is null * * @access public * @var float */ var $image_min_ratio; /** * Quality of JPEG created/converted destination image * * Default value is 85 * * @access public * @var integer */ var $jpeg_quality; /** * Determines the quality of the JPG image to fit a desired file size * * Value is in bytes. The JPG quality will be set between 1 and 100% * The calculations are approximations. * * Default value is null (no calculations) * * @access public * @var integer */ var $jpeg_size; /** * Preserve transparency when resizing or converting an image (deprecated) * * Default value is automatically set to true for transparent GIFs * This setting is now deprecated * * @access public * @var integer */ var $preserve_transparency; /** * Flag set to true when the image is transparent * * This is actually used only for transparent GIFs * * @access public * @var boolean */ var $image_is_transparent; /** * Transparent color in a palette * * This is actually used only for transparent GIFs * * @access public * @var boolean */ var $image_transparent_color; /** * Background color, used to paint transparent areas with * * If set, it will forcibly remove transparency by painting transparent areas with the color * This setting will fill in all transparent areas in PNG and GIF, as opposed to {@link image_default_color} * which will do so only in BMP, JPEG, and alpha transparent areas in transparent GIFs * This setting overrides {@link image_default_color} * * Default value is null * * @access public * @var string */ var $image_background_color; /** * Default color for non alpha-transparent images * * This setting is to be used to define a background color for semi transparent areas * of an alpha transparent when the output format doesn't support alpha transparency * This is useful when, from an alpha transparent PNG image, or an image with alpha transparent features * if you want to output it as a transparent GIFs for instance, you can set a blending color for transparent areas * If you output in JPEG or BMP, this color will be used to fill in the previously transparent areas * * The default color white * * @access public * @var boolean */ var $image_default_color; /** * Flag set to true when the image is not true color * * @access public * @var boolean */ var $image_is_palette; /** * Corrects the image brightness * * Value can range between -127 and 127 * * Default value is null * * @access public * @var integer */ var $image_brightness; /** * Corrects the image contrast * * Value can range between -127 and 127 * * Default value is null * * @access public * @var integer */ var $image_contrast; /** * Applies threshold filter * * Value can range between -127 and 127 * * Default value is null * * @access public * @var integer */ var $image_threshold; /** * Applies a tint on the image * * Value is an hexadecimal color, such as #FFFFFF * * Default value is null * * @access public * @var string; */ var $image_tint_color; /** * Applies a colored overlay on the image * * Value is an hexadecimal color, such as #FFFFFF * * To use with {@link image_overlay_percent} * * Default value is null * * @access public * @var string; */ var $image_overlay_color; /** * Sets the percentage for the colored overlay * * Value is a percentage, as an integer between 0 and 100 * * Unless used with {@link image_overlay_color}, this setting has no effect * * Default value is 50 * * @access public * @var integer */ var $image_overlay_percent; /** * Inverts the color of an image * * Default value is FALSE * * @access public * @var boolean; */ var $image_negative; /** * Turns the image into greyscale * * Default value is FALSE * * @access public * @var boolean; */ var $image_greyscale; /** * Adds a text label on the image * * Value is a string, any text. Text will not word-wrap, although you can use breaklines in your text "\n" * * If set, this setting allow the use of all other settings starting with image_text_ * * Replacement tokens can be used in the string: *
     * gd_version    src_name       src_name_body src_name_ext
     * src_pathname  src_mime       src_x         src_y       
     * src_type      src_bits       src_pixels
     * src_size      src_size_kb    src_size_mb   src_size_human 
     * dst_path      dst_name_body  dst_pathname
     * dst_name      dst_name_ext   dst_x         dst_y
     * date          time           host          server        ip
     * 
* The tokens must be enclosed in square brackets: [dst_x] will be replaced by the width of the picture * * Default value is null * * @access public * @var string; */ var $image_text; /** * Sets the text direction for the text label * * Value is either 'h' or 'v', as in horizontal and vertical * * Default value is h (horizontal) * * @access public * @var string; */ var $image_text_direction; /** * Sets the text color for the text label * * Value is an hexadecimal color, such as #FFFFFF * * Default value is #FFFFFF (white) * * @access public * @var string; */ var $image_text_color; /** * Sets the text visibility in the text label * * Value is a percentage, as an integer between 0 and 100 * * Default value is 100 * * @access public * @var integer */ var $image_text_percent; /** * Sets the text background color for the text label * * Value is an hexadecimal color, such as #FFFFFF * * Default value is null (no background) * * @access public * @var string; */ var $image_text_background; /** * Sets the text background visibility in the text label * * Value is a percentage, as an integer between 0 and 100 * * Default value is 100 * * @access public * @var integer */ var $image_text_background_percent; /** * Sets the text font in the text label * * Value is a an integer between 1 and 5 for GD built-in fonts. 1 is the smallest font, 5 the biggest * 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. * * Default value is 5 * * @access public * @var mixed; */ var $image_text_font; /** * Sets the text label position within the image * * Value is one or two out of 'TBLR' (top, bottom, left, right) * * The positions are as following: *
     *                        TL  T  TR
     *                        L       R
     *                        BL  B  BR
     * 
* * Default value is null (centered, horizontal and vertical) * * Note that is {@link image_text_x} and {@link image_text_y} are used, this setting has no effect * * @access public * @var string; */ var $image_text_position; /** * Sets the text label absolute X position within the image * * Value is in pixels, representing the distance between the left of the image and the label * If a negative value is used, it will represent the distance between the right of the image and the label * * Default value is null (so {@link image_text_position} is used) * * @access public * @var integer */ var $image_text_x; /** * Sets the text label absolute Y position within the image * * Value is in pixels, representing the distance between the top of the image and the label * If a negative value is used, it will represent the distance between the bottom of the image and the label * * Default value is null (so {@link image_text_position} is used) * * @access public * @var integer */ var $image_text_y; /** * Sets the text label padding * * Value is in pixels, representing the distance between the text and the label background border * * Default value is 0 * * This setting can be overriden by {@link image_text_padding_x} and {@link image_text_padding_y} * * @access public * @var integer */ var $image_text_padding; /** * Sets the text label horizontal padding * * Value is in pixels, representing the distance between the text and the left and right label background borders * * Default value is null * * If set, this setting overrides the horizontal part of {@link image_text_padding} * * @access public * @var integer */ var $image_text_padding_x; /** * Sets the text label vertical padding * * Value is in pixels, representing the distance between the text and the top and bottom label background borders * * Default value is null * * If set, his setting overrides the vertical part of {@link image_text_padding} * * @access public * @var integer */ var $image_text_padding_y; /** * Sets the text alignment * * Value is a string, which can be either 'L', 'C' or 'R' * * Default value is 'C' * * This setting is relevant only if the text has several lines. * * @access public * @var string; */ var $image_text_alignment; /** * Sets the text line spacing * * Value is an integer, in pixels * * Default value is 0 * * This setting is relevant only if the text has several lines. * * @access public * @var integer */ var $image_text_line_spacing; /** * Sets the height of the reflection * * Value is an integer in pixels, or a string which format can be in pixels or percentage. * For instance, values can be : 40, '40', '40px' or '40%' * * Default value is null, no reflection * * @access public * @var mixed; */ var $image_reflection_height; /** * Sets the space between the source image and its relection * * Value is an integer in pixels, which can be negative * * Default value is 2 * * This setting is relevant only if {@link image_reflection_height} is set * * @access public * @var integer */ var $image_reflection_space; /** * Sets the color of the reflection background (deprecated) * * Value is an hexadecimal color, such as #FFFFFF * * Default value is #FFFFFF * * This setting is relevant only if {@link image_reflection_height} is set * * This setting is now deprecated in favor of {@link image_default_color} * * @access public * @var string; */ var $image_reflection_color; /** * Sets the initial opacity of the reflection * * Value is an integer between 0 (no opacity) and 100 (full opacity). * The reflection will start from {@link image_reflection_opacity} and end up at 0 * * Default value is 60 * * This setting is relevant only if {@link image_reflection_height} is set * * @access public * @var integer */ var $image_reflection_opacity; /** * Flips the image vertically or horizontally * * Value is either 'h' or 'v', as in horizontal and vertical * * Default value is null (no flip) * * @access public * @var string; */ var $image_flip; /** * Rotates the image by increments of 45 degrees * * Value is either 90, 180 or 270 * * Default value is null (no rotation) * * @access public * @var string; */ var $image_rotate; /** * Crops an image * * Values are four dimensions, or two, or one (CSS style) * They represent the amount cropped top, right, bottom and left. * These values can either be in an array, or a space separated string. * Each value can be in pixels (with or without 'px'), or percentage (of the source image) * * For instance, are valid: *
     * $foo->image_crop = 20                  OR array(20);
     * $foo->image_crop = '20px'              OR array('20px');
     * $foo->image_crop = '20 40'             OR array('20', 40);
     * $foo->image_crop = '-20 25%'           OR array(-20, '25%');
     * $foo->image_crop = '20px 25%'          OR array('20px', '25%');
     * $foo->image_crop = '20% 25%'           OR array('20%', '25%');
     * $foo->image_crop = '20% 25% 10% 30%'   OR array('20%', '25%', '10%', '30%');
     * $foo->image_crop = '20px 25px 2px 2px' OR array('20px', '25%px', '2px', '2px');
     * $foo->image_crop = '20 25% 40px 10%'   OR array(20, '25%', '40px', '10%');
     * 
* * If a value is negative, the image will be expanded, and the extra parts will be filled with black * * Default value is null (no cropping) * * @access public * @var string OR array; */ var $image_crop; /** * Adds a bevel border on the image * * Value is a positive integer, representing the thickness of the bevel * * If the bevel colors are the same as the background, it makes a fade out effect * * Default value is null (no bevel) * * @access public * @var integer */ var $image_bevel; /** * Top and left bevel color * * Value is a color, in hexadecimal format * This setting is used only if {@link image_bevel} is set * * Default value is #FFFFFF * * @access public * @var string; */ var $image_bevel_color1; /** * Right and bottom bevel color * * Value is a color, in hexadecimal format * This setting is used only if {@link image_bevel} is set * * Default value is #000000 * * @access public * @var string; */ var $image_bevel_color2; /** * Adds a single-color border on the outer of the image * * Values are four dimensions, or two, or one (CSS style) * They represent the border thickness top, right, bottom and left. * These values can either be in an array, or a space separated string. * Each value can be in pixels (with or without 'px'), or percentage (of the source image) * * See {@link image_crop} for valid formats * * If a value is negative, the image will be cropped. * Note that the dimensions of the picture will be increased by the borders' thickness * * Default value is null (no border) * * @access public * @var integer */ var $image_border; /** * Border color * * Value is a color, in hexadecimal format. * This setting is used only if {@link image_border} is set * * Default value is #FFFFFF * * @access public * @var string; */ var $image_border_color; /** * Adds a multi-color frame on the outer of the image * * Value is an integer. Two values are possible for now: * 1 for flat border, meaning that the frame is mirrored horizontally and vertically * 2 for crossed border, meaning that the frame will be inversed, as in a bevel effect * * The frame will be composed of colored lines set in {@link image_frame_colors} * * Note that the dimensions of the picture will be increased by the borders' thickness * * Default value is null (no frame) * * @access public * @var integer */ var $image_frame; /** * Sets the colors used to draw a frame * * Values is a list of n colors in hexadecimal format. * These values can either be in an array, or a space separated string. * * The colors are listed in the following order: from the outset of the image to its center * * For instance, are valid: *
     * $foo->image_frame_colors = '#FFFFFF #999999 #666666 #000000';
     * $foo->image_frame_colors = array('#FFFFFF', '#999999', '#666666', '#000000');
     * 
* * This setting is used only if {@link image_frame} is set * * Default value is '#FFFFFF #999999 #666666 #000000' * * @access public * @var string OR array; */ var $image_frame_colors; /** * Adds a watermark on the image * * Value is a local image filename, relative or absolute. GIF, JPG, BMP and PNG are supported, as well as PNG alpha. * * If set, this setting allow the use of all other settings starting with image_watermark_ * * Default value is null * * @access public * @var string; */ var $image_watermark; /** * Sets the watermarkposition within the image * * Value is one or two out of 'TBLR' (top, bottom, left, right) * * The positions are as following: TL T TR * L R * BL B BR * * Default value is null (centered, horizontal and vertical) * * Note that is {@link image_watermark_x} and {@link image_watermark_y} are used, this setting has no effect * * @access public * @var string; */ var $image_watermark_position; /** * Sets the watermark absolute X position within the image * * Value is in pixels, representing the distance between the top of the image and the watermark * If a negative value is used, it will represent the distance between the bottom of the image and the watermark * * Default value is null (so {@link image_watermark_position} is used) * * @access public * @var integer */ var $image_watermark_x; /** * Sets the twatermark absolute Y position within the image * * Value is in pixels, representing the distance between the left of the image and the watermark * If a negative value is used, it will represent the distance between the right of the image and the watermark * * Default value is null (so {@link image_watermark_position} is used) * * @access public * @var integer */ var $image_watermark_y; /** * Allowed MIME types * * Default is a selection of safe mime-types, but you might want to change it * * Simple wildcards are allowed, such as image/* or application/* * * @access public * @var array */ var $allowed; /** * Forbidden MIME types * * Default is a selection of safe mime-types, but you might want to change it * To only check for forbidden MIME types, and allow everything else, set {@link allowed} to array('* / *') without the spaces * * Simple wildcards are allowed, such as image/* or application/* * * @access public * @var array */ var $forbidden; /** * Array of translated error messages * * By default, the language is english (en_GB) * Translations can be in separate files, in a lang/ subdirectory * * @access public * @var array */ var $translation; /** * Language selected for the translations * * By default, the language is english ("en_GB") * * @access public * @var array */ var $language; /** * Init or re-init all the processing variables to their default values * * This function is called in the constructor, and after each call of {@link process} * * @access private */ function init() { // overiddable variables $this->file_new_name_body = ''; // replace the name body $this->file_name_body_add = ''; // append to the name body $this->file_new_name_ext = ''; // replace the file extension $this->file_safe_name = true; // format safely the filename $this->file_overwrite = false; // allows overwritting if the file already exists $this->file_auto_rename = true; // auto-rename if the file already exists $this->dir_auto_create = true; // auto-creates directory if missing $this->dir_auto_chmod = true; // auto-chmod directory if not writeable $this->dir_chmod = 0777; // default chmod to use $this->mime_check = true; // don't check the mime type against the allowed list $this->mime_magic_check = false; // don't double check the MIME type with mime_magic $this->no_script = true; // turns scripts into test files $val = trim(ini_get('upload_max_filesize')); $last = strtolower($val{strlen($val)-1}); switch($last) { case 'g': $val *= 1024; case 'm': $val *= 1024; case 'k': $val *= 1024; } $this->file_max_size = $val; $this->image_resize = false; // resize the image $this->image_convert = ''; // convert. values :''; 'png'; 'jpeg'; 'gif'; 'bmp' $this->image_x = 150; $this->image_y = 150; $this->image_ratio = false; // keeps aspect ratio with x and y dimensions $this->image_ratio_crop = false; // keeps aspect ratio with x and y dimensions, filling the space $this->image_ratio_fill = false; // keeps aspect ratio with x and y dimensions, fitting the image in the space, and coloring the rest $this->image_ratio_pixels = false; // keeps aspect ratio, calculating x and y so that the image is approx the set number of pixels $this->image_ratio_no_zoom_in = false; $this->image_ratio_no_zoom_out = false; $this->image_ratio_x = false; // calculate the $image_x if true $this->image_ratio_y = false; // calculate the $image_y if true $this->jpeg_quality = 85; $this->jpeg_size = null; $this->preserve_transparency = false; $this->image_is_transparent = false; $this->image_transparent_color = null; $this->image_background_color = null; $this->image_default_color = '#ffffff'; $this->image_is_palette = false; $this->image_max_width = null; $this->image_max_height = null; $this->image_max_pixels = null; $this->image_max_ratio = null; $this->image_min_width = null; $this->image_min_height = null; $this->image_min_pixels = null; $this->image_min_ratio = null; $this->image_brightness = null; $this->image_contrast = null; $this->image_threshold = null; $this->image_tint_color = null; $this->image_overlay_color = null; $this->image_overlay_percent = null; $this->image_negative = false; $this->image_greyscale = false; $this->image_text = null; $this->image_text_direction = null; $this->image_text_color = '#FFFFFF'; $this->image_text_percent = 100; $this->image_text_background = null; $this->image_text_background_percent = 100; $this->image_text_font = 5; $this->image_text_x = null; $this->image_text_y = null; $this->image_text_position = null; $this->image_text_padding = 0; $this->image_text_padding_x = null; $this->image_text_padding_y = null; $this->image_text_alignment = 'C'; $this->image_text_line_spacing = 0; $this->image_reflection_height = null; $this->image_reflection_space = 2; $this->image_reflection_color = '#ffffff'; $this->image_reflection_opacity = 60; $this->image_watermark = null; $this->image_watermark_x = null; $this->image_watermark_y = null; $this->image_watermark_position = null; $this->image_flip = null; $this->image_rotate = null; $this->image_crop = null; $this->image_bevel = null; $this->image_bevel_color1 = '#FFFFFF'; $this->image_bevel_color2 = '#000000'; $this->image_border = null; $this->image_border_color = '#FFFFFF'; $this->image_frame = null; $this->image_frame_colors = '#FFFFFF #999999 #666666 #000000'; $this->forbidden = array(); $this->allowed = array("application/rar", "application/x-rar-compressed", "application/arj", "application/excel", "application/gnutar", "application/octet-stream", "application/pdf", "application/powerpoint", "application/postscript", "application/plain", "application/rtf", "application/vocaltec-media-file", "application/wordperfect", "application/x-bzip", "application/x-bzip2", "application/x-compressed", "application/x-excel", "application/x-gzip", "application/x-latex", "application/x-midi", "application/x-msexcel", "application/x-rtf", "application/x-sit", "application/x-stuffit", "application/x-shockwave-flash", "application/x-troff-msvideo", "application/x-zip-compressed", "application/xml", "application/zip", "application/msword", "application/mspowerpoint", "application/vnd.ms-excel", "application/vnd.ms-powerpoint", "application/vnd.ms-word", "application/vnd.ms-word.document.macroEnabled.12", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/vnd.ms-word.template.macroEnabled.12", "application/vnd.openxmlformats-officedocument.wordprocessingml.template", "application/vnd.ms-powerpoint.template.macroEnabled.12", "application/vnd.openxmlformats-officedocument.presentationml.template", "application/vnd.ms-powerpoint.addin.macroEnabled.12", "application/vnd.ms-powerpoint.slideshow.macroEnabled.12", "application/vnd.openxmlformats-officedocument.presentationml.slideshow", "application/vnd.ms-powerpoint.presentation.macroEnabled.12", "application/vnd.openxmlformats-officedocument.presentationml.presentation", "application/vnd.ms-excel.addin.macroEnabled.12", "application/vnd.ms-excel.sheet.binary.macroEnabled.12", "application/vnd.ms-excel.sheet.macroEnabled.12", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "application/vnd.ms-excel.template.macroEnabled.12", "application/vnd.openxmlformats-officedocument.spreadsheetml.template", "audio/*", "image/*", "video/*", "multipart/x-zip", "multipart/x-gzip", "text/richtext", "text/plain", "text/xml"); } /** * Constructor. Checks if the file has been uploaded * * The constructor takes $_FILES['form_field'] array as argument * where form_field is the form field name * * The constructor will check if the file has been uploaded in its temporary location, and * accordingly will set {@link uploaded} (and {@link error} is an error occurred) * * If the file has been uploaded, the constructor will populate all the variables holding the upload * information (none of the processing class variables are used here). * You can have access to information about the file (name, size, MIME type...). * * * Alternatively, you can set the first argument to be a local filename (string) * This allows processing of a local file, as if the file was uploaded * * The optional second argument allows you to set the language for the error messages * * @access private * @param array $file $_FILES['form_field'] * or string $file Local filename * @param string $lang Optional language code */ function upload($file, $lang = 'en_GB') { $this->file_src_name = ''; $this->file_src_name_body = ''; $this->file_src_name_ext = ''; $this->file_src_mime = ''; $this->file_src_size = ''; $this->file_src_error = ''; $this->file_src_pathname = ''; $this->file_src_temp = ''; $this->file_dst_path = ''; $this->file_dst_name = ''; $this->file_dst_name_body = ''; $this->file_dst_name_ext = ''; $this->file_dst_pathname = ''; $this->image_src_x = null; $this->image_src_y = null; $this->image_src_bits = null; $this->image_src_type = null; $this->image_src_pixels = null; $this->image_dst_x = 0; $this->image_dst_y = 0; $this->uploaded = true; $this->no_upload_check = false; $this->processed = true; $this->error = ''; $this->log = ''; $this->allowed = array(); $this->forbidden = array(); $this->file_is_image = false; $this->init(); $info = null; // sets default language $this->translation = array(); $this->translation['file_error'] = 'File error. Please try again.'; $this->translation['local_file_missing'] = 'Local file doesn\'t exist.'; $this->translation['local_file_not_readable'] = 'Local file is not readable.'; $this->translation['uploaded_too_big_ini'] = 'File upload error (the uploaded file exceeds the upload_max_filesize directive in php.ini).'; $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).'; $this->translation['uploaded_partial'] = 'File upload error (the uploaded file was only partially uploaded).'; $this->translation['uploaded_missing'] = 'File upload error (no file was uploaded).'; $this->translation['uploaded_unknown'] = 'File upload error (unknown error code).'; $this->translation['try_again'] = 'File upload error. Please try again.'; $this->translation['file_too_big'] = 'File too big.'; $this->translation['no_mime'] = 'MIME type can\'t be detected.'; $this->translation['incorrect_file'] = 'Incorrect type of file.'; $this->translation['image_too_wide'] = 'Image too wide.'; $this->translation['image_too_narrow'] = 'Image too narrow.'; $this->translation['image_too_high'] = 'Image too high.'; $this->translation['image_too_short'] = 'Image too short.'; $this->translation['ratio_too_high'] = 'Image ratio too high (image too wide).'; $this->translation['ratio_too_low'] = 'Image ratio too low (image too high).'; $this->translation['too_many_pixels'] = 'Image has too many pixels.'; $this->translation['not_enough_pixels'] = 'Image has not enough pixels.'; $this->translation['file_not_uploaded'] = 'File not uploaded. Can\'t carry on a process.'; $this->translation['already_exists'] = '%s already exists. Please change the file name.'; $this->translation['temp_file_missing'] = 'No correct temp source file. Can\'t carry on a process.'; $this->translation['source_missing'] = 'No correct uploaded source file. Can\'t carry on a process.'; $this->translation['destination_dir'] = 'Destination directory can\'t be created. Can\'t carry on a process.'; $this->translation['destination_dir_missing'] = 'Destination directory doesn\'t exist. Can\'t carry on a process.'; $this->translation['destination_path_not_dir'] = 'Destination path is not a directory. Can\'t carry on a process.'; $this->translation['destination_dir_write'] = 'Destination directory can\'t be made writeable. Can\'t carry on a process.'; $this->translation['destination_path_write'] = 'Destination path is not a writeable. Can\'t carry on a process.'; $this->translation['temp_file'] = 'Can\'t create the temporary file. Can\'t carry on a process.'; $this->translation['source_not_readable'] = 'Source file is not readable. Can\'t carry on a process.'; $this->translation['no_create_support'] = 'No create from %s support.'; $this->translation['create_error'] = 'Error in creating %s image from source.'; $this->translation['source_invalid'] = 'Can\'t read image source. Not an image?.'; $this->translation['gd_missing'] = 'GD doesn\'t seem to be present.'; $this->translation['watermark_no_create_support'] = 'No create from %s support, can\'t read watermark.'; $this->translation['watermark_create_error'] = 'No %s read support, can\'t create watermark.'; $this->translation['watermark_invalid'] = 'Unknown image format, can\'t read watermark.'; $this->translation['file_create'] = 'No %s create support.'; $this->translation['no_conversion_type'] = 'No conversion type defined.'; $this->translation['copy_failed'] = 'Error copying file on the server. copy() failed.'; $this->translation['reading_failed'] = 'Error reading the file.'; // determines the language $this->lang = $lang; if ($this->lang != 'en_GB' && file_exists('lang/class.upload.' . $lang . '.php')) { $translation = null; include('lang/class.upload.' . $lang . '.php'); if (is_array($translation)) { $this->translation = array_merge($this->translation, $translation); } else { $this->lang = 'en_GB'; } } // determines the supported MIME types, and matching image format $this->image_supported = array(); if ($this->gdversion()) { if (imagetypes() & IMG_GIF) { $this->image_supported['image/gif'] = 'gif'; } if (imagetypes() & IMG_JPG) { $this->image_supported['image/jpg'] = 'jpg'; $this->image_supported['image/jpeg'] = 'jpg'; $this->image_supported['image/pjpeg'] = 'jpg'; } if (imagetypes() & IMG_PNG) { $this->image_supported['image/png'] = 'png'; $this->image_supported['image/x-png'] = 'png'; } if (imagetypes() & IMG_WBMP) { $this->image_supported['image/bmp'] = 'bmp'; $this->image_supported['image/x-ms-bmp'] = 'bmp'; $this->image_supported['image/x-windows-bmp'] = 'bmp'; } } // display some system information if (empty($this->log)) { $this->log .= 'system information
'; $inis = ini_get_all(); $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; $gd = $this->gdversion() ? $this->gdversion(true) : 'GD not present'; $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' : '')); $this->log .= '- GD version : ' . $gd . '
'; $this->log .= '- supported image types : ' . (!empty($supported) ? $supported : 'none') . '
'; $this->log .= '- open_basedir : ' . (!empty($open_basedir) ? $open_basedir : 'no restriction') . '
'; $this->log .= '- language : ' . $this->lang . '
'; } if (!$file) { $this->uploaded = false; $this->error = $this->translate('file_error'); } // check if we sent a local filename rather than a $_FILE element if (!is_array($file)) { if (empty($file)) { $this->uploaded = false; $this->error = $this->translate('file_error'); } else { $this->no_upload_check = TRUE; // this is a local filename, i.e.not uploaded $this->log .= '' . $this->translate("source is a local file") . ' ' . $file . '
'; if ($this->uploaded && !file_exists($file)) { $this->uploaded = false; $this->error = $this->translate('local_file_missing'); } if ($this->uploaded && !is_readable($file)) { $this->uploaded = false; $this->error = $this->translate('local_file_not_readable'); } if ($this->uploaded) { $this->file_src_pathname = $file; $this->file_src_name = basename($file); $this->log .= '- local file name OK
'; ereg('\.([^\.]*$)', $this->file_src_name, $extension); if (is_array($extension)) { $this->file_src_name_ext = strtolower($extension[1]); $this->file_src_name_body = substr($this->file_src_name, 0, ((strlen($this->file_src_name) - strlen($this->file_src_name_ext)))-1); } else { $this->file_src_name_ext = ''; $this->file_src_name_body = $this->file_src_name; } $this->file_src_size = (file_exists($file) ? filesize($file) : 0); // we try to retrieve the MIME type $info = getimagesize($this->file_src_pathname); $this->file_src_mime = (is_array($info) && array_key_exists('mime', $info) ? $info['mime'] : null); // if we don't have a MIME type, we attempt to retrieve it the old way if (empty($this->file_src_mime)) { $mime = (is_array($info) && array_key_exists(2, $info) ? $info[2] : null); // 1 = GIF, 2 = JPG, 3 = PNG $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)))); } // if we still don't have a MIME type, we attempt to retrieve it otherwise if (empty($this->file_src_mime) && function_exists('mime_content_type')) { $this->file_src_mime = mime_content_type($this->file_src_pathname); } $this->file_src_error = 0; // determine whether the file is an image if (array_key_exists($this->file_src_mime, $this->image_supported)) { $this->file_is_image = true; $this->image_src_type = $this->image_supported[$this->file_src_mime]; } } } } else { // this is an element from $_FILE, i.e. an uploaded file $this->log .= 'source is an uploaded file
'; if ($this->uploaded) { $this->file_src_error = $file['error']; switch($this->file_src_error) { case 0: // all is OK $this->log .= '- upload OK
'; break; case 1: $this->uploaded = false; $this->error = $this->translate('uploaded_too_big_ini'); break; case 2: $this->uploaded = false; $this->error = $this->translate('uploaded_too_big_html'); break; case 3: $this->uploaded = false; $this->error = $this->translate('uploaded_partial'); break; case 4: $this->uploaded = false; $this->error = $this->translate('uploaded_missing'); break; default: $this->uploaded = false; $this->error = $this->translate('uploaded_unknown'); } } if ($this->uploaded) { $this->file_src_pathname = $file['tmp_name']; $this->file_src_name = $file['name']; if ($this->file_src_name == '') { $this->uploaded = false; $this->error = $this->translate('try_again'); } } if ($this->uploaded) { $this->log .= '- file name OK
'; ereg('\.([^\.]*$)', $this->file_src_name, $extension); if (is_array($extension)) { $this->file_src_name_ext = strtolower($extension[1]); $this->file_src_name_body = substr($this->file_src_name, 0, ((strlen($this->file_src_name) - strlen($this->file_src_name_ext)))-1); } else { $this->file_src_name_ext = ''; $this->file_src_name_body = $this->file_src_name; } $this->file_src_size = $file['size']; $this->file_src_mime = $file['type']; // if the file is an image, we gather some useful data if (array_key_exists($this->file_src_mime, $this->image_supported)) { $this->file_is_image = true; $this->image_src_type = $this->image_supported[$this->file_src_mime]; $info = @getimagesize($this->file_src_pathname); } } } // if the file is an image, we gather some useful data if ($this->file_is_image) { if (is_array($info)) { $this->image_src_x = $info[0]; $this->image_src_y = $info[1]; $this->image_src_pixels = $this->image_src_x * $this->image_src_y; $this->image_src_bits = array_key_exists('bits', $info) ? $info['bits'] : null; } else { $this->log .= '- can\'t retrieve image information. open_basedir restriction in place?
'; } } $this->log .= '- source variables
'; $this->log .= '    file_src_name : ' . $this->file_src_name . '
'; $this->log .= '    file_src_name_body : ' . $this->file_src_name_body . '
'; $this->log .= '    file_src_name_ext : ' . $this->file_src_name_ext . '
'; $this->log .= '    file_src_pathname : ' . $this->file_src_pathname . '
'; $this->log .= '    file_src_mime : ' . $this->file_src_mime . '
'; $this->log .= '    file_src_size : ' . $this->file_src_size . ' (max= ' . $this->file_max_size . ')
'; $this->log .= '    file_src_error : ' . $this->file_src_error . '
'; if ($this->file_is_image) { $this->log .= '- source file is an image
'; $this->log .= '    image_src_x : ' . $this->image_src_x . '
'; $this->log .= '    image_src_y : ' . $this->image_src_y . '
'; $this->log .= '    image_src_pixels : ' . $this->image_src_pixels . '
'; $this->log .= '    image_src_type : ' . $this->image_src_type . '
'; $this->log .= '    image_src_bits : ' . $this->image_src_bits . '
'; } } /** * Returns the version of GD * * @access public * @param boolean $full Optional flag to get precise version * @return float GD version */ function gdversion($full = false) { static $gd_version = null; static $gd_full_version = null; if ($gd_version === null) { if (function_exists('gd_info')) { $gd = gd_info(); $gd = $gd["GD Version"]; $regex = "/([\d\.]+)/i"; } else { ob_start(); phpinfo(8); $gd = ob_get_contents(); ob_end_clean(); $regex = "/\bgd\s+version\b[^\d\n\r]+?([\d\.]+)/i"; } if (preg_match($regex, $gd, $m)) { $gd_full_version = (string) $m[1]; $gd_version = (float) $m[1]; } else { $gd_full_version = 'none'; $gd_version = 0; } } if ($full) { return $gd_full_version; } else { return $gd_version; } } /** * Creates directories recursively * * @access private * @param string $path Path to create * @param integer $mode Optional permissions * @return boolean Success */ function rmkdir($path, $mode = 0777) { return is_dir($path) || ( $this->rmkdir(dirname($path), $mode) && $this->_mkdir($path, $mode) ); } /** * Creates directory * * @access private * @param string $path Path to create * @param integer $mode Optional permissions * @return boolean Success */ function _mkdir($path, $mode = 0777) { $old = umask(0); $res = @mkdir($path, $mode); umask($old); return $res; } /** * Translate error messages * * @access private * @param string $str Message to translate * @param array $tokens Optional token values * @return string Translated string */ function translate($str, $tokens = array()) { if (array_key_exists($str, $this->translation)) $str = $this->translation[$str]; if (is_array($tokens) && sizeof($tokens) > 0) $str = vsprintf($str, $tokens); return $str; } /** * Creates a container image * * @access private * @param integer $x Width * @param integer $y Height * @param boolean $fill Optional flag to draw the background color or not * @param boolean $trsp Optional flag to set the background to be transparent * @return resource Container image */ function imagecreatenew($x, $y, $fill = true, $trsp = false) { if ($this->gdversion() >= 2 && !$this->image_is_palette) { // create a true color image $dst_im = imagecreatetruecolor($x, $y); // this preserves transparency in PNGs, in true color if (empty($this->image_background_color) || $trsp) { imagealphablending($dst_im, false ); imagefilledrectangle($dst_im, 0, 0, $x, $y, imagecolorallocatealpha($dst_im, 0, 0, 0, 127)); } } else { // creates a palette image $dst_im = imagecreate($x, $y); // preserves transparency for palette images, if the original image has transparency if (($fill && $this->image_is_transparent && empty($this->image_background_color)) || $trsp) { imagefilledrectangle($dst_im, 0, 0, $x, $y, $this->image_transparent_color); imagecolortransparent($dst_im, $this->image_transparent_color); } } // fills with background color if any is set if ($fill && !empty($this->image_background_color) && !$trsp) { sscanf($this->image_background_color, "#%2x%2x%2x", $red, $green, $blue); $background_color = imagecolorallocate($dst_im, $red, $green, $blue); imagefilledrectangle($dst_im, 0, 0, $x, $y, $background_color); } return $dst_im; } /** * Transfers an image from the container to the destination image * * @access private * @param resource $src_im Container image * @param resource $dst_im Destination image * @return resource Destination image */ function imagetransfer($src_im, $dst_im) { if (is_resource($dst_im)) imagedestroy($dst_im); $dst_im = & $src_im; return $dst_im; } /** * Merges two images * * If the output format is PNG, then we do it pixel per pixel to retain the alpha channel * * @access private * @param resource $dst_img Destination image * @param resource $src_img Overlay image * @param int $dst_x x-coordinate of destination point * @param int $dst_y y-coordinate of destination point * @param int $src_x x-coordinate of source point * @param int $src_y y-coordinate of source point * @param int $src_w Source width * @param int $src_h Source height * @param int $pct Optional percentage of the overlay, between 0 and 100 (default: 100) * @return resource Destination image */ function imagecopymergealpha(&$dst_im, &$src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct = 0) { $dst_x = (int) $dst_x; $dst_y = (int) $dst_y; $src_x = (int) $src_x; $src_y = (int) $src_y; $src_w = (int) $src_w; $src_h = (int) $src_h; $pct = (int) $pct; $dst_w = imagesx($dst_im); $dst_h = imagesy($dst_im); for ($y = $src_y; $y < $src_h; $y++) { for ($x = $src_x; $x < $src_w; $x++) { if ($x >= 0 && $x <= $dst_w && $y >= 0 && $y <= $dst_h) { $dst_pixel = imagecolorsforindex($dst_im, imagecolorat($dst_im, $x + $dst_x, $y + $dst_y)); $src_pixel = imagecolorsforindex($src_im, imagecolorat($src_im, $x + $src_x, $y + $src_y)); $src_alpha = 1 - ($src_pixel['alpha'] / 127); $dst_alpha = 1 - ($dst_pixel['alpha'] / 127); $opacity = $src_alpha * $pct / 100; if ($dst_alpha >= $opacity) $alpha = $dst_alpha; if ($dst_alpha < $opacity) $alpha = $opacity; if ($alpha > 1) $alpha = 1; if ($opacity > 0) { $dst_red = round(( ($dst_pixel['red'] * $dst_alpha * (1 - $opacity)) ) ); $dst_green = round(( ($dst_pixel['green'] * $dst_alpha * (1 - $opacity)) ) ); $dst_blue = round(( ($dst_pixel['blue'] * $dst_alpha * (1 - $opacity)) ) ); $src_red = round((($src_pixel['red'] * $opacity)) ); $src_green = round((($src_pixel['green'] * $opacity)) ); $src_blue = round((($src_pixel['blue'] * $opacity)) ); $red = round(($dst_red + $src_red ) / ($dst_alpha * (1 - $opacity) + $opacity)); $green = round(($dst_green + $src_green) / ($dst_alpha * (1 - $opacity) + $opacity)); $blue = round(($dst_blue + $src_blue ) / ($dst_alpha * (1 - $opacity) + $opacity)); if ($red > 255) $red = 255; if ($green > 255) $green = 255; if ($blue > 255) $blue = 255; $alpha = round((1 - $alpha) * 127); $color = imagecolorallocatealpha($dst_im, $red, $green, $blue, $alpha); imagesetpixel($dst_im, $x + $dst_x, $y + $dst_y, $color); } } } } return true; } /** * Actually uploads the file, and act on it according to the set processing class variables * * This function copies the uploaded file to the given location, eventually performing actions on it. * Typically, you can call {@link process} several times for the same file, * for instance to create a resized image and a thumbnail of the same file. * The original uploaded file remains intact in its temporary location, so you can use {@link process} several times. * You will be able to delete the uploaded file with {@link clean} when you have finished all your {@link process} calls. * * According to the processing class variables set in the calling file, the file can be renamed, * and if it is an image, can be resized or converted. * * When the processing is completed, and the file copied to its new location, the * processing class variables will be reset to their default value. * This allows you to set new properties, and perform another {@link process} on the same uploaded file * * If the function is called with a null or empty argument, then it will return the content of the picture * * It will set {@link processed} (and {@link error} is an error occurred) * * @access public * @param string $server_path Optional path location of the uploaded file, with an ending slash * @return string Optional content of the image */ function process($server_path = null) { $this->error = ''; $this->processed = true; $return_mode = false; $return_content = null; if (empty($server_path) || is_null($server_path)) { $this->log .= 'process file and return the content
'; $return_mode = true; } else { if(strtolower(substr(PHP_OS, 0, 3)) === 'win') { if (substr($server_path, -1, 1) != '\\') $server_path = $server_path . '\\'; } else { if (substr($server_path, -1, 1) != '/') $server_path = $server_path . '/'; } $this->log .= 'process file to ' . $server_path . '
'; } // checks file size and mine type if ($this->uploaded) { if ($this->file_src_size > $this->file_max_size ) { $this->processed = false; $this->error = $this->translate('file_too_big'); } else { $this->log .= '- file size OK
'; } // turn dangerous scripts into text files if ($this->no_script) { if (((substr($this->file_src_mime, 0, 5) == 'text/' || strpos($this->file_src_mime, 'javascript') !== false) && (substr($this->file_src_name, -4) != '.txt')) || preg_match('/\.(php|pl|py|cgi|asp)$/i', $this->file_src_name) || empty($this->file_src_name_ext)) { $this->file_src_mime = 'text/plain'; $this->log .= '- script ' . $this->file_src_name . ' renamed as ' . $this->file_src_name . '.txt!
'; $this->file_src_name_ext .= (empty($this->file_src_name_ext) ? 'txt' : '.txt'); } } // checks MIME type with mime_magic if ($this->mime_magic_check && function_exists('mime_content_type')) { $detected_mime = mime_content_type($this->file_src_pathname); if ($this->file_src_mime != $detected_mime) { $this->log .= '- MIME type detected as ' . $detected_mime . ' but given as ' . $this->file_src_mime . '!
'; $this->file_src_mime = $detected_mime; } } if ($this->mime_check && empty($this->file_src_mime)) { $this->processed = false; $this->error = $this->translate('no_mime'); } else if ($this->mime_check && !empty($this->file_src_mime) && strpos($this->file_src_mime, '/') !== false) { list($m1, $m2) = explode('/', $this->file_src_mime); $allowed = false; // check wether the mime type is allowed foreach($this->allowed as $k => $v) { list($v1, $v2) = explode('/', $v); if (($v1 == '*' && $v2 == '*') || ($v1 == $m1 && ($v2 == $m2 || $v2 == '*'))) { $allowed = true; break; } } // check wether the mime type is forbidden foreach($this->forbidden as $k => $v) { list($v1, $v2) = explode('/', $v); if (($v1 == '*' && $v2 == '*') || ($v1 == $m1 && ($v2 == $m2 || $v2 == '*'))) { $allowed = false; break; } } if (!$allowed) { $this->processed = false; $this->error = $this->translate('incorrect_file'); } else { $this->log .= '- file mime OK : ' . $this->file_src_mime . '
'; } } else { $this->log .= '- file mime OK : ' . $this->file_src_mime . '
'; } // if the file is an image, we can check on its dimensions // these checks are not available if open_basedir restrictions are in place if ($this->file_is_image) { if (is_numeric($this->image_src_x) && is_numeric($this->image_src_y)) { $ratio = $this->image_src_x / $this->image_src_y; if (!is_null($this->image_max_width) && $this->image_src_x > $this->image_max_width) { $this->processed = false; $this->error = $this->translate('image_too_wide'); } if (!is_null($this->image_min_width) && $this->image_src_x < $this->image_min_width) { $this->processed = false; $this->error = $this->translate('image_too_narrow'); } if (!is_null($this->image_max_height) && $this->image_src_y > $this->image_max_height) { $this->processed = false; $this->error = $this->translate('image_too_high'); } if (!is_null($this->image_min_height) && $this->image_src_y < $this->image_min_height) { $this->processed = false; $this->error = $this->translate('image_too_short'); } if (!is_null($this->image_max_ratio) && $ratio > $this->image_max_ratio) { $this->processed = false; $this->error = $this->translate('ratio_too_high'); } if (!is_null($this->image_min_ratio) && $ratio < $this->image_min_ratio) { $this->processed = false; $this->error = $this->translate('ratio_too_low'); } if (!is_null($this->image_max_pixels) && $this->image_src_pixels > $this->image_max_pixels) { $this->processed = false; $this->error = $this->translate('too_many_pixels'); } if (!is_null($this->image_min_pixels) && $this->image_src_pixels < $this->image_min_pixels) { $this->processed = false; $this->error = $this->translate('not_enough_pixels'); } } else { $this->log .= '- no image properties available, can\'t enforce dimension checks : ' . $this->file_src_mime . '
'; } } } else { $this->error = $this->translate('file_not_uploaded'); $this->processed = false; } if ($this->processed) { $this->file_dst_path = $server_path; // repopulate dst variables from src $this->file_dst_name = $this->file_src_name; $this->file_dst_name_body = $this->file_src_name_body; $this->file_dst_name_ext = $this->file_src_name_ext; if ($this->image_convert != '') { // if we convert as an image $this->file_dst_name_ext = $this->image_convert; $this->log .= '- new file name ext : ' . $this->image_convert . '
'; } if ($this->file_new_name_body != '') { // rename file body $this->file_dst_name_body = $this->file_new_name_body; $this->log .= '- new file name body : ' . $this->file_new_name_body . '
'; } if ($this->file_new_name_ext != '') { // rename file ext $this->file_dst_name_ext = $this->file_new_name_ext; $this->log .= '- new file name ext : ' . $this->file_new_name_ext . '
'; } if ($this->file_name_body_add != '') { // append a bit to the name $this->file_dst_name_body = $this->file_dst_name_body . $this->file_name_body_add; $this->log .= '- file name body add : ' . $this->file_name_body_add . '
'; } if ($this->file_safe_name) { // formats the name $this->file_dst_name_body = str_replace(array(' ', '-'), array('_','_'), $this->file_dst_name_body) ; $this->file_dst_name_body = ereg_replace('[^A-Za-z0-9_]', '', $this->file_dst_name_body) ; $this->log .= '- file name safe format
'; } $this->log .= '- destination variables
'; if (empty($this->file_dst_path) || is_null($this->file_dst_path)) { $this->log .= '    file_dst_path : n/a
'; } else { $this->log .= '    file_dst_path : ' . $this->file_dst_path . '
'; } $this->log .= '    file_dst_name_body : ' . $this->file_dst_name_body . '
'; $this->log .= '    file_dst_name_ext : ' . $this->file_dst_name_ext . '
'; // do we do some image manipulation? $image_manipulation = ($this->file_is_image && ( $this->image_resize || $this->image_convert != '' || is_numeric($this->image_brightness) || is_numeric($this->image_contrast) || is_numeric($this->image_threshold) || !empty($this->image_tint_color) || !empty($this->image_overlay_color) || !empty($this->image_text) || $this->image_greyscale || $this->image_negative || !empty($this->image_watermark) || is_numeric($this->image_rotate) || is_numeric($this->jpeg_size) || !empty($this->image_flip) || !empty($this->image_crop) || !empty($this->image_border) || $this->image_frame > 0 || $this->image_bevel > 0 || $this->image_reflection_height)); if ($image_manipulation) { if ($this->image_convert=='') { $this->file_dst_name = $this->file_dst_name_body . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : ''); $this->log .= '- image operation, keep extension
'; } else { $this->file_dst_name = $this->file_dst_name_body . '.' . $this->image_convert; $this->log .= '- image operation, change extension for conversion type
'; } } else { $this->file_dst_name = $this->file_dst_name_body . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : ''); $this->log .= '- no image operation, keep extension
'; } if (!$return_mode) { if (!$this->file_auto_rename) { $this->log .= '- no auto_rename if same filename exists
'; $this->file_dst_pathname = $this->file_dst_path . $this->file_dst_name; } else { $this->log .= '- checking for auto_rename
'; $this->file_dst_pathname = $this->file_dst_path . $this->file_dst_name; $body = $this->file_dst_name_body; $cpt = 1; while (@file_exists($this->file_dst_pathname)) { $this->file_dst_name_body = $body . '_' . $cpt; $this->file_dst_name = $this->file_dst_name_body . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : ''); $cpt++; $this->file_dst_pathname = $this->file_dst_path . $this->file_dst_name; } if ($cpt>1) $this->log .= '    auto_rename to ' . $this->file_dst_name . '
'; } $this->log .= '- destination file details
'; $this->log .= '    file_dst_name : ' . $this->file_dst_name . '
'; $this->log .= '    file_dst_pathname : ' . $this->file_dst_pathname . '
'; if ($this->file_overwrite) { $this->log .= '- no overwrite checking
'; } else { if (@file_exists($this->file_dst_pathname)) { $this->processed = false; $this->error = $this->translate('already_exists', array($this->file_dst_name)); } else { $this->log .= '- ' . $this->file_dst_name . ' doesn\'t exist already
'; } } } } else { $this->processed = false; } // if we have already moved the uploaded file, we use the temporary copy as source file, and check if it exists if (!empty($this->file_src_temp)) { $this->log .= '- use the temp file instead of the original file since it is a second process
'; $this->file_src_pathname = $this->file_src_temp; if (!file_exists($this->file_src_pathname)) { $this->processed = false; $this->error = $this->translate('temp_file_missing'); } // if we haven't a temp file, and that we do check on uploads, we use is_uploaded_file() } else if (!$this->no_upload_check) { if (!is_uploaded_file($this->file_src_pathname)) { $this->processed = false; $this->error = $this->translate('source_missing'); } // otherwise, if we don't check on uploaded files (local file for instance), we use file_exists() } else { if (!file_exists($this->file_src_pathname)) { $this->processed = false; $this->error = $this->translate('source_missing'); } } // checks if the destination directory exists, and attempt to create it if (!$return_mode) { if ($this->processed && !file_exists($this->file_dst_path)) { if ($this->dir_auto_create) { $this->log .= '- ' . $this->file_dst_path . ' doesn\'t exist. Attempting creation:'; if (!$this->rmkdir($this->file_dst_path, $this->dir_chmod)) { $this->log .= ' failed
'; $this->processed = false; $this->error = $this->translate('destination_dir'); } else { $this->log .= ' success
'; } } else { $this->error = $this->translate('destination_dir_missing'); } } if ($this->processed && !is_dir($this->file_dst_path)) { $this->processed = false; $this->error = $this->translate('destination_path_not_dir'); } // checks if the destination directory is writeable, and attempt to make it writeable $hash = md5($this->file_dst_name_body . rand(1, 1000)); if ($this->processed && !($f = @fopen($this->file_dst_path . $hash . '.' . $this->file_dst_name_ext, 'a+'))) { if ($this->dir_auto_chmod) { $this->log .= '- ' . $this->file_dst_path . ' is not writeable. Attempting chmod:'; if (!@chmod($this->file_dst_path, $this->dir_chmod)) { $this->log .= ' failed
'; $this->processed = false; $this->error = $this->translate('destination_dir_write'); } else { $this->log .= ' success
'; if (!($f = @fopen($this->file_dst_path . $hash . '.' . $this->file_dst_name_ext, 'a+'))) { // we re-check $this->processed = false; $this->error = $this->translate('destination_dir_write'); } else { @fclose($f); } } } else { $this->processed = false; $this->error = $this->translate('destination_path_write'); } } else { if ($this->processed) @fclose($f); @unlink($this->file_dst_path . $hash . '.' . $this->file_dst_name_ext); } // 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) // then we create a temp file that will be used as the source file in subsequent processes // 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) if (!$this->no_upload_check && empty($this->file_src_temp) && !file_exists($this->file_src_pathname)) { $this->log .= '- attempting creating a temp file:'; $hash = md5($this->file_dst_name_body . rand(1, 1000)); if (move_uploaded_file($this->file_src_pathname, $this->file_dst_path . $hash . '.' . $this->file_dst_name_ext)) { $this->file_src_pathname = $this->file_dst_path . $hash . '.' . $this->file_dst_name_ext; $this->file_src_temp = $this->file_src_pathname; $this->log .= ' file created
'; $this->log .= ' temp file is: ' . $this->file_src_temp . '
'; } else { $this->log .= ' failed
'; $this->processed = false; $this->error = $this->translate('temp_file'); } } } if ($this->processed) { if ($image_manipulation) { // checks if the source file is readable if ($this->processed && !($f = @fopen($this->file_src_pathname, 'r'))) { $this->processed = false; $this->error = $this->translate('source_not_readable'); } else { @fclose($f); } // we now do all the image manipulations $this->log .= '- image resizing or conversion wanted
'; if ($this->gdversion()) { switch($this->image_src_type) { case 'jpg': if (!function_exists('imagecreatefromjpeg')) { $this->processed = false; $this->error = $this->translate('no_create_support', array('JPEG')); } else { $image_src = @imagecreatefromjpeg($this->file_src_pathname); if (!$image_src) { $this->processed = false; $this->error = $this->translate('create_error', array('JPEG')); } else { $this->log .= '- source image is JPEG
'; } } break; case 'png': if (!function_exists('imagecreatefrompng')) { $this->processed = false; $this->error = $this->translate('no_create_support', array('PNG')); } else { $image_src = @imagecreatefrompng($this->file_src_pathname); if (!$image_src) { $this->processed = false; $this->error = $this->translate('create_error', array('PNG')); } else { $this->log .= '- source image is PNG
'; } } break; case 'gif': if (!function_exists('imagecreatefromgif')) { $this->processed = false; $this->error = $this->translate('no_create_support', array('GIF')); } else { $image_src = @imagecreatefromgif($this->file_src_pathname); if (!$image_src) { $this->processed = false; $this->error = $this->translate('create_error', array('GIF')); } else { $this->log .= '- source image is GIF
'; } } break; case 'bmp': if (!method_exists($this, 'imagecreatefrombmp')) { $this->processed = false; $this->error = $this->translate('no_create_support', array('BMP')); } else { $image_src = @$this->imagecreatefrombmp($this->file_src_pathname); if (!$image_src) { $this->processed = false; $this->error = $this->translate('create_error', array('BMP')); } else { $this->log .= '- source image is BMP
'; } } break; default: $this->processed = false; $this->error = $this->translate('source_invalid'); } } else { $this->processed = false; $this->error = $this->translate('gd_missing'); } if ($this->processed && $image_src) { // we have to set image_convert if it is not already if (empty($this->image_convert)) { $this->log .= '- setting destination file type to ' . $this->file_src_name_ext . '
'; $this->image_convert = $this->file_src_name_ext; } if (!in_array($this->image_convert, $this->image_supported)) { $this->image_convert = 'jpg'; } // we set the default color to be the background color if we don't output in a transparent format 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; if (!empty($this->image_background_color)) $this->image_default_color = $this->image_background_color; if (empty($this->image_default_color)) $this->image_default_color = '#FFFFFF'; $this->image_src_x = imagesx($image_src); $this->image_src_y = imagesy($image_src); $this->image_dst_x = $this->image_src_x; $this->image_dst_y = $this->image_src_y; $gd_version = $this->gdversion(); $ratio_crop = null; if (!imageistruecolor($image_src)) { // $this->image_src_type == 'gif' $this->log .= '- image is detected as having a palette
'; $this->image_is_palette = true; $this->image_transparent_color = imagecolortransparent($image_src); if ($this->image_transparent_color >= 0 && imagecolorstotal($image_src) > $this->image_transparent_color) { $this->image_is_transparent = true; //$this->image_transparent_color = imagecolorsforindex($image_src, $this->image_transparent_color); $this->log .= '    palette image is detected as transparent
'; } // if the image has a palette (GIF), we convert it to true color, preserving transparency $this->log .= '    convert palette image to true color
'; $transparent_color = imagecolortransparent($image_src); if ($transparent_color >= 0 && imagecolorstotal($image_src) > $transparent_color) { $rgb = imagecolorsforindex($image_src, $transparent_color); $transparent_color = ($rgb['red'] << 16) | ($rgb['green'] << 8) | $rgb['blue']; imagecolortransparent($image_src, imagecolorallocate($image_src, 0, 0, 0)); } $true_color = imagecreatetruecolor($this->image_src_x, $this->image_src_y); imagealphablending($image_src, false); imagesavealpha($image_src, true); imagecopy($true_color, $image_src, 0, 0, 0, 0, $this->image_src_x, $this->image_src_y); $image_src = $this->imagetransfer($true_color, $image_src); if ($transparent_color >= 0) { $this->log .= '    preserve transparency
'; imagealphablending($image_src, false); imagesavealpha($image_src, true); for ($x = 0; $x < $this->image_src_x; $x++) { for ($y = 0; $y < $this->image_src_y; $y++) { if (imagecolorat($image_src, $x, $y) == $transparent_color) imagesetpixel($image_src, $x, $y, 127 << 24); } } } $this->image_is_palette = false; } if ($this->image_resize) { $this->log .= '- resizing...
'; if ($this->image_ratio_x) { $this->log .= '    calculate x size<