Source for file class.upload.php
0.29
Documentation is available at class.upload.php
Webpage is available at http://www.verot.net/php_class_upload.htm
// +------------------------------------------------------------------------+
// +------------------------------------------------------------------------+
// | Copyright (c) Colin Verot 2003-2009. All rights reserved. |
// | Last modified 03/02/2010 |
// | Email colin@verot.net |
// | Web http://www.verot.net |
// +------------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or modify |
// | it under the terms of the GNU General Public License version 2 as |
// | published by the Free Software Foundation. |
// | This program is distributed in the hope that it will be useful, |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
// | GNU General Public License for more details. |
// | You should have received a copy of the GNU General Public License |
// | along with this program; if not, write to the |
// | Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
// | Boston, MA 02111-1307 USA |
// | Please give credit on sites that use class.upload and submit changes |
// | of the script so other people can use them as well. |
// | This script is free to use, don't abuse. |
// +------------------------------------------------------------------------+
* @author Colin Verot <colin@verot.net>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* <b>What does it do?</b>
* 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 also supports
* The class works with PHP 4 and 5, and its error messages can
* <b>How does it work?</b>
* 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 reset 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 <i>enctype="multipart/form-data"</i> in your form
* tag <form> if you want your form to upload the file.
* <b>How to use it?</b><br>
* Create a simple HTML file, with a form such as:
* <form enctype="multipart/form-data" method="post" action="upload.php">
* <input type="file" size="32" name="image_field" value="">
* <input type="submit" name="Submit" value="upload">
* 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 'error : ' . $handle->error;
* <b>How to process local files?</b><br>
* Use the class as following, the rest being the same as above:
* $handle = new upload('/home/user/myfile.jpg');
* <b>How to set the language?</b><br>
* 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');
* <b>How to output the resulting file or picture directly to the browser?</b><br>
* 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();
* 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();
* <b>Processing parameters</b> (reset after each process)
* <li><b>file_new_name_body</b> replaces the name body (default: '')<br>
* <pre>$handle->file_new_name_body = 'new name';</pre></li>
* <li><b>file_name_body_add</b> appends to the name body (default: '')<br>
* <pre>$handle->file_name_body_add = '_uploaded';</pre></li>
* <li><b>file_name_body_pre</b> prepends to the name body (default: '')<br>
* <pre>$handle->file_name_body_pre = 'thumb_';</pre></li>
* <li><b>file_new_name_ext</b> replaces the file extension (default: '')<br>
* <pre>$handle->file_new_name_ext = 'txt';</pre></li>
* <li><b>file_safe_name</b> formats the filename (spaces changed to _) (default: true)<br>
* <pre>$handle->file_safe_name = true;</pre></li>
* <li><b>file_overwrite</b> sets behaviour if file already exists (default: false)<br>
* <pre>$handle->file_overwrite = true;</pre></li>
* <li><b>file_auto_rename</b> automatically renames file if it already exists (default: true)<br>
* <pre>$handle->file_auto_rename = true;</pre></li>
* <li><b>auto_create_dir</b> automatically creates destination directory if missing (default: true)<br>
* <pre>$handle->auto_create_dir = true;</pre></li>
* <li><b>dir_auto_chmod</b> automatically attempts to chmod the destination directory if not writeable (default: true)<br>
* <pre>$handle->dir_auto_chmod = true;</pre></li>
* <li><b>dir_chmod</b> chmod used when creating directory or if directory not writeable (default: 0777)<br>
* <pre>$handle->dir_chmod = 0777;</pre></li>
* <li><b>file_max_size</b> sets maximum upload size (default: upload_max_filesize from php.ini)<br>
* <pre>$handle->file_max_size = '1024'; // 1KB</pre></li>
* <li><b>mime_check</b> sets if the class check the MIME against the {@link allowed} list (default: true)<br>
* <pre>$handle->mime_check = true;</pre></li>
* <li><b>no_script</b> sets if the class turns scripts into text files (default: true)<br>
* <pre>$handle->no_script = false;</pre></li>
* <li><b>allowed</b> array of allowed mime-types. wildcard accepted, as in image/* (default: check {@link Init})<br>
* <pre>$handle->allowed = array('application/pdf','application/msword', 'image/*');</pre></li>
* <li><b>forbidden</b> array of forbidden mime-types. wildcard accepted, as in image/* (default: check {@link Init})<br>
* <pre>$handle->forbidden = array('application/*');</pre></li>
* <li><b>image_convert</b> if set, image will be converted (possible values : ''|'png'|'jpeg'|'gif'|'bmp'; default: '')<br>
* <pre>$handle->image_convert = 'jpg';</pre></li>
* <li><b>image_background_color</b> if set, will forcibly fill transparent areas with the color, in hexadecimal (default: null)<br>
* <pre>$handle->image_background_color = '#FF00FF';</pre></li>
* <li><b>image_default_color</b> fallback color background color for non alpha-transparent output formats, such as JPEG or BMP, in hexadecimal (default: #FFFFFF)<br>
* <pre>$handle->image_default_color = '#FF00FF';</pre></li>
* <li><b>jpeg_quality</b> sets the compression quality for JPEG images (default: 85)<br>
* <pre>$handle->jpeg_quality = 50;</pre></li>
* <li><b>jpeg_size</b> if set to a size in bytes, will approximate {@link jpeg_quality} so the output image fits within the size (default: null)<br>
* <pre>$handle->jpeg_size = 3072;</pre></li>
* 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)
* <li><b>image_max_width</b> if set to a dimension in pixels, the upload will be invalid if the image width is greater (default: null)<br>
* <pre>$handle->image_max_width = 200;</pre></li>
* <li><b>image_max_height</b> if set to a dimension in pixels, the upload will be invalid if the image height is greater (default: null)<br>
* <pre>$handle->image_max_height = 100;</pre></li>
* <li><b>image_max_pixels</b> if set to a number of pixels, the upload will be invalid if the image number of pixels is greater (default: null)<br>
* <pre>$handle->image_max_pixels = 50000;</pre></li>
* <li><b>image_max_ratio</b> if set to a aspect ratio (width/height), the upload will be invalid if the image apect ratio is greater (default: null)<br>
* <pre>$handle->image_max_ratio = 1.5;</pre></li>
* <li><b>image_min_width</b> if set to a dimension in pixels, the upload will be invalid if the image width is lower (default: null)<br>
* <pre>$handle->image_min_width = 100;</pre></li>
* <li><b>image_min_height</b> if set to a dimension in pixels, the upload will be invalid if the image height is lower (default: null)<br>
* <pre>$handle->image_min_height = 500;</pre></li>
* <li><b>image_min_pixels</b> if set to a number of pixels, the upload will be invalid if the image number of pixels is lower (default: null)<br>
* <pre>$handle->image_min_pixels = 20000;</pre></li>
* <li><b>image_min_ratio</b> if set to a aspect ratio (width/height), the upload will be invalid if the image apect ratio is lower (default: null)<br>
* <pre>$handle->image_min_ratio = 0.5;</pre></li>
* <li><b>image_resize</b> determines is an image will be resized (default: false)<br>
* <pre>$handle->image_resize = true;</pre></li>
* The following variables are used only if {@link image_resize} == true
* <li><b>image_x</b> destination image width (default: 150)<br>
* <pre>$handle->image_x = 100;</pre></li>
* <li><b>image_y</b> destination image height (default: 150)<br>
* <pre>$handle->image_y = 200;</pre></li>
* Use either one of the following
* <li><b>image_ratio</b> if true, resize image conserving the original sizes ratio, using {@link image_x} AND {@link image_y} as max sizes if true (default: false)<br>
* <pre>$handle->image_ratio = true;</pre></li>
* <li><b>image_ratio_crop</b> if true, resize image conserving the original sizes ratio, using {@link image_x} AND {@link image_y} as max sizes, and cropping excedent to fill the space. setting can also be a string, with one or more from 'TBLR', indicating which side of the image will be kept while cropping (default: false)<br>
* <pre>$handle->image_ratio_crop = true;</pre></li>
* <li><b>image_ratio_fill</b> if true, resize image conserving the original sizes ratio, using {@link image_x} AND {@link image_y} as max sizes, fitting the image in the space and coloring the remaining space. setting can also be a string, with one or more from 'TBLR', indicating which side of the space the image will be in (default: false)<br>
* <pre>$handle->image_ratio_fill = true;</pre></li>
* <li><b>image_ratio_no_zoom_in</b> same as {@link image_ratio}, but won't resize if the source image is smaller than {@link image_x} x {@link image_y} (default: false)<br>
* <pre>$handle->image_ratio_no_zoom_in = true;</pre></li>
* <li><b>image_ratio_no_zoom_out</b> same as {@link image_ratio}, but won't resize if the source image is bigger than {@link image_x} x {@link image_y} (default: false)<br>
* <pre>$handle->image_ratio_no_zoom_out = true;</pre></li>
* <li><b>image_ratio_x</b> if true, resize image, calculating {@link image_x} from {@link image_y} and conserving the original sizes ratio (default: false)<br>
* <pre>$handle->image_ratio_x = true;</pre></li>
* <li><b>image_ratio_y</b> if true, resize image, calculating {@link image_y} from {@link image_x} and conserving the original sizes ratio (default: false)<br>
* <pre>$handle->image_ratio_y = true;</pre></li>
* <li><b>image_ratio_pixels</b> if set to a long integer, resize image, calculating {@link image_y} and {@link image_x} to match a the number of pixels (default: false)<br>
* <pre>$handle->image_ratio_pixels = 25000;</pre></li>
* The following image manipulations require GD2+
* <li><b>image_brightness</b> if set, corrects the brightness. value between -127 and 127 (default: null)<br>
* <pre>$handle->image_brightness = 40;</pre></li>
* <li><b>image_contrast</b> if set, corrects the contrast. value between -127 and 127 (default: null)<br>
* <pre>$handle->image_contrast = 50;</pre></li>
* <li><b>image_tint_color</b> if set, will tint the image with a color, value as hexadecimal #FFFFFF (default: null)<br>
* <pre>$handle->image_tint_color = '#FF0000';</pre></li>
* <li><b>image_overlay_color</b> if set, will add a colored overlay, value as hexadecimal #FFFFFF (default: null)<br>
* <pre>$handle->image_overlay_color = '#FF0000';</pre></li>
* <li><b>image_overlay_percent</b> used when {@link image_overlay_color} is set, determines the opacity (default: 50)<br>
* <pre>$handle->image_overlay_percent = 20;</pre></li>
* <li><b>image_negative</b> inverts the colors in the image (default: false)<br>
* <pre>$handle->image_negative = true;</pre></li>
* <li><b>image_greyscale</b> transforms an image into greyscale (default: false)<br>
* <pre>$handle->image_greyscale = true;</pre></li>
* <li><b>image_threshold</b> applies a threshold filter. value between -127 and 127 (default: null)<br>
* <pre>$handle->image_threshold = 20;</pre></li>
* <li><b>image_text</b> creates a text label on the image, value is a string, with eventual replacement tokens (default: null)<br>
* <pre>$handle->image_text = 'test';</pre></li>
* <li><b>image_text_direction</b> text label direction, either 'h' horizontal or 'v' vertical (default: 'h')<br>
* <pre>$handle->image_text_direction = 'v';</pre></li>
* <li><b>image_text_color</b> text color for the text label, in hexadecimal (default: #FFFFFF)<br>
* <pre>$handle->image_text_color = '#FF0000';</pre></li>
* <li><b>image_text_percent</b> text opacity on the text label, integer between 0 and 100 (default: 100)<br>
* <pre>$handle->image_text_percent = 50;</pre></li>
* <li><b>image_text_background</b> text label background color, in hexadecimal (default: null)<br>
* <pre>$handle->image_text_background = '#FFFFFF';</pre></li>
* <li><b>image_text_background_percent</b> text label background opacity, integer between 0 and 100 (default: 100)<br>
* <pre>$handle->image_text_background_percent = 50;</pre></li>
* <li><b>image_text_font</b> built-in font for the text label, from 1 to 5. 1 is the smallest (default: 5)<br>
* <pre>$handle->image_text_font = 4;</pre></li>
* <li><b>image_text_x</b> absolute text label position, in pixels from the left border. can be negative (default: null)<br>
* <pre>$handle->image_text_x = 5;</pre></li>
* <li><b>image_text_y</b> absolute text label position, in pixels from the top border. can be negative (default: null)<br>
* <pre>$handle->image_text_y = 5;</pre></li>
* <li><b>image_text_position</b> text label position withing the image, a combination of one or two from 'TBLR': top, bottom, left, right (default: null)<br>
* <pre>$handle->image_text_position = 'LR';</pre></li>
* <li><b>image_text_padding</b> text label padding, in pixels. can be overridden by {@link image_text_padding_x} and {@link image_text_padding_y} (default: 0)<br>
* <pre>$handle->image_text_padding = 5;</pre></li>
* <li><b>image_text_padding_x</b> text label horizontal padding (default: null)<br>
* <pre>$handle->image_text_padding_x = 2;</pre></li>
* <li><b>image_text_padding_y</b> text label vertical padding (default: null)<br>
* <pre>$handle->image_text_padding_y = 10;</pre></li>
* <li><b>image_text_alignment</b> text alignment when text has multiple lines, either 'L', 'C' or 'R' (default: 'C')<br>
* <pre>$handle->image_text_alignment = 'R';</pre></li>
* <li><b>image_text_line_spacing</b> space between lines in pixels, when text has multiple lines (default: 0)<br>
* <pre>$handle->image_text_line_spacing = 3;</pre></li>
* <li><b>image_flip</b> flips image, wither 'h' horizontal or 'v' vertical (default: null)<br>
* <pre>$handle->image_flip = 'h';</pre></li>
* <li><b>image_rotate</b> rotates image. possible values are 90, 180 and 270 (default: null)<br>
* <pre>$handle->image_rotate = 90;</pre></li>
* <li><b>image_crop</b> crops image. accepts 4, 2 or 1 values as 'T R B L' or 'TB LR' or 'TBLR'. dimension can be 20, or 20px or 20% (default: null)<br>
* <pre>$handle->image_crop = array(50,40,30,20); OR '-20 20%'...</pre></li>
* <li><b>image_precrop</b> crops image, before an eventual resizing. accepts 4, 2 or 1 values as 'T R B L' or 'TB LR' or 'TBLR'. dimension can be 20, or 20px or 20% (default: null)<br>
* <pre>$handle->image_precrop = array(50,40,30,20); OR '-20 20%'...</pre></li>
* <li><b>image_bevel</b> adds a bevel border to the image. value is thickness in pixels (default: null)<br>
* <pre>$handle->image_bevel = 20;</pre></li>
* <li><b>image_bevel_color1</b> top and left bevel color, in hexadecimal (default: #FFFFFF)<br>
* <pre>$handle->image_bevel_color1 = '#FFFFFF';</pre></li>
* <li><b>image_bevel_color2</b> bottom and right bevel color, in hexadecimal (default: #000000)<br>
* <pre>$handle->image_bevel_color2 = '#000000';</pre></li>
* <li><b>image_border</b> adds a unicolor border to the image. accepts 4, 2 or 1 values as 'T R B L' or 'TB LR' or 'TBLR'. dimension can be 20, or 20px or 20% (default: null)<br>
* <pre>$handle->image_border = '3px'; OR '-20 20%' OR array(3,2)...</pre></li>
* <li><b>image_border_color</b> border color, in hexadecimal (default: #FFFFFF)<br>
* <pre>$handle->image_border_color = '#FFFFFF';</pre></li>
* <li><b>image_frame</b> type of frame: 1=flat 2=crossed (default: null)<br>
* <pre>$handle->image_frame = 2;</pre></li>
* <li><b>image_frame_colors</b> list of hex colors, in an array or a space separated string (default: '#FFFFFF #999999 #666666 #000000')<br>
* <pre>$handle->image_frame_colors = array('#999999', '#FF0000', '#666666', '#333333', '#000000');</pre></li>
* <li><b>image_watermark</b> adds a watermark on the image, value is a local filename. accepted files are GIF, JPG, BMP, PNG and PNG alpha (default: null)<br>
* <pre>$handle->image_watermark = 'watermark.png';</pre></li>
* <li><b>image_watermark_x</b> absolute watermark position, in pixels from the left border. can be negative (default: null)<br>
* <pre>$handle->image_watermark_x = 5;</pre></li>
* <li><b>image_watermark_y</b> absolute watermark position, in pixels from the top border. can be negative (default: null)<br>
* <pre>$handle->image_watermark_y = 5;</pre></li>
* <li><b>image_watermark_position</b> watermark position withing the image, a combination of one or two from 'TBLR': top, bottom, left, right (default: null)<br>
* <pre>$handle->image_watermark_position = 'LR';</pre></li>
* <li><b>image_reflection_height</b> if set, a reflection will be added. Format is either in pixels or percentage, such as 40, '40', '40px' or '40%' (default: null)<br>
* <pre>$handle->image_reflection_height = '25%';</pre></li>
* <li><b>image_reflection_space</b> space in pixels between the source image and the reflection, can be negative (default: null)<br>
* <pre>$handle->image_reflection_space = 3;</pre></li>
* <li><b>image_reflection_color</b> reflection background color, in hexadecimal. Now deprecated in favor of {@link image_default_color} (default: #FFFFFF)<br>
* <pre>$handle->image_default_color = '#000000';</pre></li>
* <li><b>image_reflection_opacity</b> opacity level at which the reflection starts, integer between 0 and 100 (default: 60)<br>
* <pre>$handle->image_reflection_opacity = 60;</pre></li>
* <b>Values that can be read before calling {@link process}()</b>
* <li><b>file_src_name</b> Source file name</li>
* <li><b>file_src_name_body</b> Source file name body</li>
* <li><b>file_src_name_ext</b> Source file extension</li>
* <li><b>file_src_pathname</b> Source file complete path and name</li>
* <li><b>file_src_mime</b> Source file mime type</li>
* <li><b>file_src_size</b> Source file size in bytes</li>
* <li><b>file_src_error</b> Upload error code</li>
* <li><b>file_is_image</b> Boolean flag, true if the file is a supported image type</li>
* If the file is a supported image type (and <i>open_basedir</i> restrictions allow it)
* <li><b>image_src_x</b> Source file width in pixels</li>
* <li><b>image_src_y</b> Source file height in pixels</li>
* <li><b>image_src_pixels</b> Source file number of pixels</li>
* <li><b>image_src_type</b> Source file type (png, jpg, gif or bmp)</li>
* <li><b>image_src_bits</b> Source file color depth</li>
* <b>Values that can be read after calling {@link process}()</b>
* <li><b>file_dst_path</b> Destination file path</li>
* <li><b>file_dst_name_body</b> Destination file name body</li>
* <li><b>file_dst_name_ext</b> Destination file extension</li>
* <li><b>file_dst_name</b> Destination file name</li>
* <li><b>file_dst_pathname</b> Destination file complete path and name</li>
* If the file is a supported image type
* <li><b>image_dst_x</b> Destination file width</li>
* <li><b>image_dst_y</b> Destination file height</li>
* <li><b>image_convert</b> Destination file format</li>
* Most of the image operations require GD. GD2 is greatly recommended
* The class is compatible with PHP 4.3+, and compatible with PHP5
* <li><b>v 0.29</b> 03/02/2010<br>
* - added protection against malicious images<br>
* - added zip and torrent MIME type<br>
* - replaced split() with explode()<br>
* - initialise image_dst_x/y with image_src_x/y<br>
* - 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>
* - added more extensions and MIME types<br>
* - improved MIME type validation<br>
* - improved logging</li>
* <li><b>v 0.28</b> 10/08/2009<br>
* - replaced ereg functions to be compatible with PHP 5.3<br>
* - added flv MIME type<br>
* - improved MIME type detection<br>
* - added {@link file_name_body_pre} to prepend a string to the file name<br>
* - 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>
* - use exec() rather than shell_exec(), to play better with safe mode <br>
* - added some error messages<br>
* - fix bug when checking on conditions, {@link processed} wasn't propagated properly</li>
* <li><b>v 0.27</b> 14/05/2009<br>
* - look for the language files directory from __FILE__<br>
* - deactivate {@link file_auto_rename} if {@link file_overwrite} is set<br>
* - improved transparency replacement for true color images<br>
* - fixed calls to newer version of UNIX file utility<br>
* - fixed error when using PECL Fileinfo extension in SAFE MODE, and when using the finfo class<br>
* - added {@link image_precrop} to crop the image before an eventual resizing</li>
* <li><b>v 0.26</b> 13/11/2008<br>
* - rewrote conversion from palette to true color to handle transparency better<br>
* - fixed imagecopymergealpha() when the overlayed image is of wrong dimensions<br>
* - fixed imagecreatenew() when the image to create have less than 1 pixels width or height<br>
* - 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>
* - added support for Flash uploaders<br>
* - some bug fixing and error handling</li>
* <li><b>v 0.25</b> 17/11/2007<br>
* - added translation files and mechanism to instantiate the class with a language different from English<br>
* - added {@link forbidden} to set an array of forbidden MIME types<br>
* - implemented support for simple wildcards in {@link allowed} and {@link forbidden}, such as image/*<br>
* - preset the file extension to the desired conversion format when converting an image<br>
* - added read and write support for BMP images<br>
* - added a flag {@link file_is_image} to determine if the file is a supported image type<br>
* - 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>
* - improved logging; now provides useful system information<br>
* - 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>
* - added {@link image_ratio_pixels} to resize an image to a number of pixels, keeping aspect ratio<br>
* - added {@link image_is_palette} and {@link image_is_transparent} and {@link image_transparent_color} for GIF images<br>
* - added {@link image_default_color} to define a fallback color for non alpha-transparent output formats, such as JPEG or BMP<br>
* - changed {@link image_background_color}, which now forces transparent areas to be painted<br>
* - improved reflections and color overlays so that it works with alpha transparent images<br>
* - {@link image_reflection_color} is now deprecated in favour of {@link image_default_color}<br />
* - transparent PNGs are now processed in true color, and fully preserving the alpha channel when doing merges<br>
* - transparent GIFs are now automatically detected. {@link preserve_transparency} is deprecated<br>
* - transparent true color images can be saved as GIF while retaining transparency, semi transparent areas being merged with {@link image_default_color}<br>
* - transparent true color images can be saved as JPG/BMP with the semi transparent areas being merged with {@link image_default_color}<br>
* - fixed conversion of images to true color<br>
* - 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>
* <li><b>v 0.24</b> 25/05/2007<br>
* - added {@link image_background_color}, to set the default background color of an image<br>
* - added possibility of using replacement tokens in text labels<br>
* - changed default JPEG quality to 85<br>
* - fixed a small bug when using greyscale filter and associated filters<br>
* - 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>
* - improved the recursive creation of directories<br>
* - the class now converts palette based images to true colors before doing graphic manipulations</li>
* <li><b>v 0.23</b> 23/12/2006<br>
* - 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>
* <li><b>v 0.22</b> 16/12/2006<br>
* - added automatic creation of a temporary file if the upload directory is not within open_basedir<br>
* - fixed a bug which was preventing to work on a local file by overwriting it with its processed copy<br>
* - added MIME types video/x-ms-wmv and image/x-png and fixed PNG support for IE weird MIME types<br>
* - 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>
* - added support for multiple lines in the text, using "\n" as a line break<br>
* - added {@link image_text_line_spacing} which allow to set the space between several lines of text<br>
* - added {@link image_text_alignment} which allow to set the alignment when text has several lines<br>
* - {@link image_text_font} can now be set to the path of a GDF font to load external fonts<br>
* - added {@link image_reflection_height} to create a reflection of the source image, which height is in pixels or percentage<br>
* - added {@link image_reflection_space} to set the space in pixels between the source image and the reflection<br>
* - added {@link image_reflection_color} to set the reflection background color<br>
* - added {@link image_reflection_opacity} to set the initial level of opacity of the reflection</li>
* <li><b>v 0.21</b> 30/09/2006<br>
* - 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>
* - added {@link mime_check}, which default is true, to set checks against {@link allowed} MIME list<br>
* - if MIME is empty, the class now triggers an error<br>
* - color #000000 is OK for {@link image_text_color}, and related text transparency bug fixed<br>
* - {@link gd_version}() now uses gd_info(), or else phpinfo()<br>
* - fixed path issue when the destination path has no trailing slash on Windows systems <br>
* - removed inline functions to be fully PHP5 compatible </li>
* <li><b>v 0.20</b> 11/08/2006<br>
* - added some more error checking and messages (GD presence, permissions...)<br>
* - fix when uploading files without extension<br>
* - changed values for {@link image_brightness} and {@link image_contrast} to be between -127 and 127<br>
* - added {@link dir_auto_create} to automatically and recursively create destination directory if missing.<br>
* - added {@link dir_auto_chmod} to automatically chmod the destination directory if not writeable.<br>
* - added {@link dir_chmod} to set the default chmod to use.<br>
* - added {@link image_crop} to crop images<br>
* - added {@link image_negative} to invert the colors on the image<br>
* - added {@link image_greyscale} to turn the image into greyscale<br>
* - added {@link image_threshold} to apply a threshold filter on the image<br>
* - added {@link image_bevel}, {@link image_bevel_color1} and {@link image_bevel_color2} to add a bevel border<br>
* - added {@link image_border} and {@link image_border_color} to add a single color border<br>
* - added {@link image_frame} and {@link image_frame_colors} to add a multicolored frame</li>
* <li><b>v 0.19</b> 29/03/2006<br>
* - class is now compatible i18n (thanks Sylwester).<br>
* - the class can mow manipulate local files, not only uploaded files (instanciate the class with a local filename).<br>
* - {@link file_safe_name} has been improved a bit.<br>
* - 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>
* - added {@link image_text} and all derivated settings to add a text label on the image.<br>
* - added {@link image_watermark} and all derivated settings to add a watermark image on the image.<br>
* - added {@link image_flip} and {@link image_rotate} for more image manipulations<br>
* - added {@link jpeg_size} to calculate the JPG compression quality in order to fit within one filesize.</li>
* <li><b>v 0.18</b> 02/02/2006<br>
* - added {@link no_script} to turn dangerous scripts into text files.<br>
* - added {@link mime_magic_check} to set the class to use mime_magic.<br>
* - added {@link preserve_transparency} *experimental*. Thanks Gregor.<br>
* - fixed size and mime checking, wasn't working :/ Thanks Willem.<br>
* - fixed memory leak when resizing images.<br>
* - when resizing, it is not necessary anymore to set {@link image_convert}.<br>
* - il is now possible to simply convert an image, with no resizing.<br>
* - sets the default {@link file_max_size} to upload_max_filesize from php.ini. Thanks Edward</li>
* <li><b>v 0.17</b> 28/05/2005<br>
* - the class can be used with any version of GD.<br>
* - added security check on the file with a list of mime-types.<br>
* - changed the license to GPL v2 only</li>
* <li><b>v 0.16</b> 19/05/2005<br>
* - added {@link file_auto_rename} automatic file renaming if the same filename already exists.<br>
* - added {@link file_safe_name} safe formatting of the filename (spaces to _underscores so far).<br>
* - added some more error reporting to avoid crash if GD is not present</li>
* <li><b>v 0.15</b> 16/04/2005<br>
* - added JPEG compression quality setting. Thanks Vad</li>
* <li><b>v 0.14</b> 14/03/2005<br>
* - reworked the class file to allow parsing with phpDocumentor</li>
* <li><b>v 0.13</b> 07/03/2005<br>
* - fixed a bug with {@link image_ratio}. Thanks Justin.<br>
* - added {@link image_ratio_no_zoom_in} and {@link image_ratio_no_zoom_out} </li>
* <li><b>v 0.12</b> 21/01/2005<br>
* - added {@link image_ratio} to resize within max values, keeping image ratio</li>
* <li><b>v 0.11</b> 22/08/2003<br>
* - update for GD2 (changed imageresized() into imagecopyresampled() and imagecreate() into imagecreatetruecolor())</li>
* Uploaded file name body (i.e. without extension)
* Uploaded file name extension
* Uploaded file MIME type
* Uploaded file size, in bytes
* Holds eventual PHP error code from $_FILES
* Uloaded file name, including server path
* Uloaded file name temporary copy
* Destination file name body (i.e. without extension)
* Destination file extension
* Destination file name, including path
* Source image color depth
* Type of image (png, gif, jpg or bmp)
* Destination image width
* Destination image height
* Supported image formats
* Flag to determine if the source file is an image
* Flag set after instanciating the class
* Indicates if the file has been uploaded properly
* 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
* Flag set after calling a process
* Indicates if the processing, and copy of the resulting file went OK
* Holds eventual error message in plain english
* Holds an HTML formatted log
// overiddable processing variables
* Set this variable to replace the name body (i.e. without extension)
* Set this variable to append a string to the file name body
* Set this variable to prepend a string to the file name body
* Set this variable to change the file extension
* Set this variable to format the filename (spaces changed to _)
* 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
* Set this variable to false if you don't want to check the MIME with Fileinfo PECL extension
* You can also set it with the path of the magic database file.
* If set to true, the class will try to read the MAGIC environment variable
* and if it is empty, will default to '/usr/share/file/magic'
* If set to an empty string, it will call finfo_open without the path argument
* This variable is set to true by default for security reason
* Set this variable to false if you don't want to check the MIME with UNIX file() command
* This variable is set to true by default for security reason
* Set this variable to false if you don't want to check the MIME with the magic.mime file
* The function mime_content_type() will be deprecated,
* and this variable will be set to false in a future release
* This variable is set to true by default for security reason
* Set this variable to false if you don't want to check the MIME with getimagesize()
* The class tries to get a MIME type from getimagesize()
* If no MIME is returned, it tries to guess the MIME type from the file type
* This variable is set to true by default for security reason
* Set this variable to false if you don't want to turn dangerous scripts into simple text files
* Set this variable to true to allow automatic renaming of the file
* if the file already exists
* For instance, on uploading foo.ext,<br>
* if foo.ext already exists, upload will be renamed foo_1.ext<br>
* and if foo_1.ext already exists, upload will be renamed foo_2.ext<br>
* Note that this option doesn't have any effect if {@link file_overwrite} is true
* Set this variable to true to allow automatic creation of the destination
* directory if it is missing (works recursively)
* Set this variable to true to allow automatic chmod of the destination
* directory if it is not writeable
* 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)
* Set this variable tu true to allow overwriting of an existing file
* Default value is false, so no files will be overwritten
* Set this variable to change the maximum size in bytes for an uploaded file
* Default value is the value <i>upload_max_filesize</i> from php.ini
* 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)
* Set this variable to convert the file if it is an image
* Possibles values are : ''; 'png'; 'jpeg'; 'gif'; 'bmp'
* Default value is '' (no conversion)<br>
* If {@link resize} is true, {@link convert} will be set to the source file extension
* Set this variable to the wanted (or maximum/minimum) width for the processed image, in pixels
* Set this variable to the wanted (or maximum/minimum) height for the processed image, in pixels
* Set this variable to keep the original size ratio to fit within {@link image_x} x {@link image_y}
* 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
* 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
* 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
* 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
* 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
* Set this variable to calculate {@link image_x} automatically , using {@link image_y} and conserving ratio
* Set this variable to calculate {@link image_y} automatically , using {@link image_x} and conserving ratio
* Set this variable to set a maximum image width, above which the upload will be invalid
* Set this variable to set a maximum image height, above which the upload will be invalid
* Set this variable to set a maximum number of pixels for an image, above which the upload will be invalid
* Set this variable to set a maximum image aspect ratio, above which the upload will be invalid
* Note that ratio = width / height
* Set this variable to set a minimum image width, below which the upload will be invalid
* Set this variable to set a minimum image height, below which the upload will be invalid
* Set this variable to set a minimum number of pixels for an image, below which the upload will be invalid
* Set this variable to set a minimum image aspect ratio, below which the upload will be invalid
* Note that ratio = width / height
* Quality of JPEG created/converted destination image
* 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)
* Preserve transparency when resizing or converting an image (deprecated)
* Default value is automatically set to true for transparent GIFs
* This setting is now deprecated
* Flag set to true when the image is transparent
* This is actually used only for transparent GIFs
* Transparent color in a palette
* This is actually used only for transparent GIFs
* 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 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
* Flag set to true when the image is not true color
* Corrects the image brightness
* Value can range between -127 and 127
* Corrects the image contrast
* Value can range between -127 and 127
* Applies threshold filter
* Value can range between -127 and 127
* Applies a tint on the image
* Value is an hexadecimal color, such as #FFFFFF
* Applies a colored overlay on the image
* Value is an hexadecimal color, such as #FFFFFF
* To use with {@link image_overlay_percent}
* 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
* Inverts the color of an image
* Turns the image into 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
* Sets the text direction for the text label
* Value is either 'h' or 'v', as in horizontal and vertical
* Default value is h (horizontal)
* Sets the text color for the text label
* Value is an hexadecimal color, such as #FFFFFF
* Default value is #FFFFFF (white)
* Sets the text visibility in the text label
* Value is a percentage, as an integer between 0 and 100
* Sets the text background color for the text label
* Value is an hexadecimal color, such as #FFFFFF
* Default value is null (no background)
* Sets the text background visibility in the text label
* Value is a percentage, as an integer between 0 and 100
* 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.
* 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:
* 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
* 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)
* 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)
* Sets the text label padding
* Value is in pixels, representing the distance between the text and the label background border
* This setting can be overriden by {@link image_text_padding_x} and {@link image_text_padding_y}
* Sets the text label horizontal padding
* Value is in pixels, representing the distance between the text and the left and right label background borders
* If set, this setting overrides the horizontal part of {@link image_text_padding}
* Sets the text label vertical padding
* Value is in pixels, representing the distance between the text and the top and bottom label background borders
* If set, his setting overrides the vertical part of {@link image_text_padding}
* Sets the text alignment
* Value is a string, which can be either 'L', 'C' or 'R'
* This setting is relevant only if the text has several lines.
* Sets the text line spacing
* Value is an integer, in pixels
* This setting is relevant only if the text has several lines.
* 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
* Sets the space between the source image and its relection
* Value is an integer in pixels, which can be negative
* This setting is relevant only if {@link image_reflection_height} is set
* 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}
* 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
* This setting is relevant only if {@link image_reflection_height} is set
* Flips the image vertically or horizontally
* Value is either 'h' or 'v', as in horizontal and vertical
* Default value is null (no flip)
* Rotates the image by increments of 45 degrees
* Value is either 90, 180 or 270
* Default value is null (no rotation)
* 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)
* Crops an image, before an eventual resizing
* See {@link image_crop} for valid formats
* Default value is null (no cropping)
* 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)
* 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
* 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
* 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)
* Value is a color, in hexadecimal format.
* This setting is used only if {@link image_border} is set
* Default value is #FFFFFF
* 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)
* 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'
* 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_
* 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
* 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
* 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)
* 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)
* Default is a selection of safe mime-types, but you might want to change it
* Simple wildcards are allowed, such as image/* or application/*
* 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/*
* Array of translated error messages
* By default, the language is english (en_GB)
* Translations can be in separate files, in a lang/ subdirectory
* Language selected for the translations
* By default, the language is english ("en_GB")
* 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}
$this->file_overwrite =
false; // allows overwritting if the file already exists
$this->dir_auto_chmod =
true; // auto-chmod directory if not writeable
$this->dir_chmod =
0777; // default chmod to use
$this->mime_check =
true; // checks the mime type against the allowed list
$this->mime_fileinfo =
true; // MIME detection with Fileinfo PECL extension
$this->mime_file =
true; // MIME detection with UNIX file() command
$this->mime_magic =
true; // MIME detection with mime_magic (mime_content_type())
$this->no_script =
true; // turns scripts into test files
$this->image_convert =
''; // convert. values :''; 'png'; 'jpeg'; 'gif'; 'bmp'
$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->allowed =
array("application/arj",
"application/mspowerpoint",
"application/octet-stream",
"application/postscript",
"application/powerpoint",
"application/vnd.ms-excel",
"application/vnd.ms-excel.addin.macroEnabled.12",
"application/vnd.ms-excel.sheet.binary.macroEnabled.12",
"application/vnd.ms-excel.sheet.macroEnabled.12",
"application/vnd.ms-excel.template.macroEnabled.12",
"application/vnd.ms-office",
"application/vnd.ms-officetheme",
"application/vnd.ms-powerpoint",
"application/vnd.ms-powerpoint.addin.macroEnabled.12",
"application/vnd.ms-powerpoint.presentation.macroEnabled.12",
"application/vnd.ms-powerpoint.slide.macroEnabled.12",
"application/vnd.ms-powerpoint.slideshow.macroEnabled.12",
"application/vnd.ms-powerpoint.template.macroEnabled.12",
"application/vnd.ms-word",
"application/vnd.ms-word.document.macroEnabled.12",
"application/vnd.ms-word.template.macroEnabled.12",
"application/vnd.oasis.opendocument.chart",
"application/vnd.oasis.opendocument.database",
"application/vnd.oasis.opendocument.formula",
"application/vnd.oasis.opendocument.graphics",
"application/vnd.oasis.opendocument.graphics-template",
"application/vnd.oasis.opendocument.image",
"application/vnd.oasis.opendocument.presentation",
"application/vnd.oasis.opendocument.presentation-template",
"application/vnd.oasis.opendocument.spreadsheet",
"application/vnd.oasis.opendocument.spreadsheet-template",
"application/vnd.oasis.opendocument.text",
"application/vnd.oasis.opendocument.text-master",
"application/vnd.oasis.opendocument.text-template",
"application/vnd.oasis.opendocument.text-web",
"application/vnd.openofficeorg.extension",
"application/vnd.openxmlformats-officedocument.presentationml.presentation",
"application/vnd.openxmlformats-officedocument.presentationml.slide",
"application/vnd.openxmlformats-officedocument.presentationml.slideshow",
"application/vnd.openxmlformats-officedocument.presentationml.template",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"application/vnd.openxmlformats-officedocument.spreadsheetml.template",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"application/vnd.openxmlformats-officedocument.wordprocessingml.template",
"application/vocaltec-media-file",
"application/wordperfect",
"application/x-bittorrent",
"application/x-compressed",
"application/x-rar-compressed",
"application/x-shockwave-flash",
"application/x-troff-msvideo",
"application/x-zip-compressed",
* 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
* @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_pathname =
'';
$this->file_src_temp =
'';
$this->file_dst_path =
'';
$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->file_is_image =
false;
$mime_from_browser =
null;
$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_no_tmp_dir'] =
'File upload error (missing a temporary folder).';
$this->translation['uploaded_cant_write'] =
'File upload error (failed to write file to disk).';
$this->translation['uploaded_err_extension'] =
'File upload error (file upload stopped by extension).';
$this->translation['uploaded_unknown'] =
'File upload error (unknown error code).';
$this->translation['try_again'] =
'File upload error. Please try again.';
$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
include(dirname(__FILE__
).
'/lang/class.upload.' .
$lang .
'.php');
// determines the supported MIME types, and matching image format
$this->image_supported =
array();
$this->image_supported['image/gif'] =
'gif';
$this->image_supported['image/jpg'] =
'jpg';
$this->image_supported['image/jpeg'] =
'jpg';
$this->image_supported['image/pjpeg'] =
'jpg';
$this->image_supported['image/png'] =
'png';
$this->image_supported['image/x-png'] =
'png';
$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
$this->log .=
'<b>system information</b><br />';
$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;
$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 .=
'- class version : ' .
$this->version .
'<br />';
$this->log .=
'- operating system : ' .
PHP_OS .
'<br />';
$this->log .=
'- PHP version : ' .
PHP_VERSION .
'<br />';
$this->log .=
'- GD version : ' .
$gd .
'<br />';
$this->log .=
'- supported image types : ' .
(!empty($supported) ?
$supported :
'none') .
'<br />';
$this->log .=
'- open_basedir : ' .
(!empty($open_basedir) ?
$open_basedir :
'no restriction') .
'<br />';
$this->log .=
'- language : ' .
$this->lang .
'<br />';
$this->error =
$this->translate('file_error');
// check if we sent a local filename rather than a $_FILE element
$this->error =
$this->translate('file_error');
// this is a local filename, i.e.not uploaded
$this->log .=
'<b>' .
$this->translate("source is a local file") .
' ' .
$file .
'</b><br />';
$this->error =
$this->translate('local_file_missing');
$this->error =
$this->translate('local_file_not_readable');
$this->file_src_pathname =
$file;
$this->log .=
'- local file name OK<br />';
// this is an element from $_FILE, i.e. an uploaded file
$this->log .=
'<b>source is an uploaded file</b><br />';
$this->log .=
'- upload OK<br />';
case UPLOAD_ERR_INI_SIZE:
$this->error =
$this->translate('uploaded_too_big_ini');
case UPLOAD_ERR_FORM_SIZE:
$this->error =
$this->translate('uploaded_too_big_html');
$this->error =
$this->translate('uploaded_partial');
$this->error =
$this->translate('uploaded_missing');
case @UPLOAD_ERR_NO_TMP_DIR:
$this->error =
$this->translate('uploaded_no_tmp_dir');
case @UPLOAD_ERR_CANT_WRITE:
$this->error =
$this->translate('uploaded_cant_write');
case @UPLOAD_ERR_EXTENSION:
$this->error =
$this->translate('uploaded_err_extension');
$this->file_src_pathname =
$file['tmp_name'];
$this->error =
$this->translate('try_again');
$this->log .=
'- file name OK<br />';
$mime_from_browser =
$file['type'];
$this->log .=
'<b>determining MIME type</b><br />';
// checks MIME type with Fileinfo PECL extension
$this->log .=
'- Checking MIME type with Fileinfo PECL extension<br />';
if (getenv('MAGIC') ===
FALSE) {
if (substr(PHP_OS, 0, 3) ==
'WIN') {
$path =
'/usr/share/file/magic';
$this->log .=
' MAGIC path defaults to ' .
$path .
'<br />';
$this->log .=
' MAGIC path is set to ' .
$path .
' from MAGIC variable<br />';
$this->log .=
' MAGIC path is set to ' .
$path .
'<br />';
$this->log .=
' MAGIC path will not be used<br />';
$this->log .=
' MIME type detected as ' .
$this->file_src_mime .
' by Fileinfo PECL extension<br />';
$this->log .=
' Fileinfo PECL extension failed (finfo_open)<br />';
$f =
new finfo( FILEINFO_MIME );
$this->log .=
'- MIME type detected as ' .
$this->file_src_mime .
' by Fileinfo PECL extension<br />';
$this->log .=
' Fileinfo PECL extension failed (finfo)<br />';
$this->log .=
' Fileinfo PECL extension not available<br />';
$this->log .=
'- Fileinfo PECL extension deactivated<br />';
// checks MIME type with shell if unix access is authorized
$this->log .=
'- Checking MIME type with UNIX file() command<br />';
if (substr(PHP_OS, 0, 3) !=
'WIN') {
$this->log .=
' MIME type detected as ' .
$this->file_src_mime .
' by UNIX file() command<br />';
$this->log .=
' UNIX file() command failed<br />';
$this->log .=
' UNIX file() command not availabled<br />';
$this->log .=
'- UNIX file() command is deactivated<br />';
// checks MIME type with mime_magic
$this->log .=
'- Checking MIME type with mime.magic file (mime_content_type())<br />';
$this->log .=
' MIME type detected as ' .
$this->file_src_mime .
' by mime_content_type()<br />';
$this->log .=
' mime_content_type() is not available<br />';
$this->log .=
'- mime.magic file (mime_content_type()) is deactivated<br />';
// checks MIME type with getimagesize()
$this->log .=
'- Checking MIME type with getimagesize()<br />';
$this->log .=
' MIME empty, guessing from type<br />';
$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))));
$this->log .=
' MIME type detected as ' .
$this->file_src_mime .
' by PHP getimagesize() function<br />';
$this->log .=
' getimagesize() failed<br />';
$this->log .=
'- getimagesize() is deactivated<br />';
// default to MIME from browser (or Flash)
$this->log .=
'- MIME type detected as ' .
$this->file_src_mime .
' by browser<br />';
// we need to work some magic if we upload via Flash
if ($this->file_src_mime ==
'application/octet-stream') $this->log .=
'- Flash may be rewriting MIME as application/octet-stream<br />';
$this->file_src_mime =
'application/vnd.oasis.opendocument.text';
$this->file_src_mime =
'application/vnd.oasis.opendocument.text-template';
$this->file_src_mime =
'application/vnd.oasis.opendocument.text-web';
$this->file_src_mime =
'application/vnd.oasis.opendocument.text-master';
$this->file_src_mime =
'application/vnd.oasis.opendocument.graphics';
$this->file_src_mime =
'application/vnd.oasis.opendocument.graphics-template';
$this->file_src_mime =
'application/vnd.oasis.opendocument.presentation';
$this->file_src_mime =
'application/vnd.oasis.opendocument.presentation-template';
$this->file_src_mime =
'application/vnd.oasis.opendocument.spreadsheet';
$this->file_src_mime =
'application/vnd.oasis.opendocument.spreadsheet-template';
$this->file_src_mime =
'application/vnd.oasis.opendocument.chart';
$this->file_src_mime =
'application/vnd.oasis.opendocument.formula';
$this->file_src_mime =
'application/vnd.oasis.opendocument.database';
$this->file_src_mime =
'application/vnd.oasis.opendocument.image';
$this->file_src_mime =
'application/vnd.openofficeorg.extension';
$this->file_src_mime =
'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
$this->file_src_mime =
'application/vnd.ms-word.document.macroEnabled.12';
$this->file_src_mime =
'application/vnd.openxmlformats-officedocument.wordprocessingml.template';
$this->file_src_mime =
'application/vnd.ms-word.template.macroEnabled.12';
$this->file_src_mime =
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
$this->file_src_mime =
'application/vnd.ms-excel.sheet.macroEnabled.12';
$this->file_src_mime =
'application/vnd.openxmlformats-officedocument.spreadsheetml.template';
$this->file_src_mime =
'application/vnd.ms-excel.template.macroEnabled.12';
$this->file_src_mime =
'application/vnd.ms-excel.sheet.binary.macroEnabled.12';
$this->file_src_mime =
'application/vnd.ms-excel.addin.macroEnabled.12';
$this->file_src_mime =
'application/vnd.openxmlformats-officedocument.presentationml.presentation';
$this->file_src_mime =
'application/vnd.ms-powerpoint.presentation.macroEnabled.12';
$this->file_src_mime =
'application/vnd.openxmlformats-officedocument.presentationml.slideshow';
$this->file_src_mime =
'application/vnd.ms-powerpoint.slideshow.macroEnabled.12';
$this->file_src_mime =
'application/vnd.openxmlformats-officedocument.presentationml.template';
$this->file_src_mime =
'application/vnd.ms-powerpoint.template.macroEnabled.12';
$this->file_src_mime =
'application/vnd.ms-powerpoint.addin.macroEnabled.12';
$this->file_src_mime =
'application/vnd.openxmlformats-officedocument.presentationml.slide';
$this->file_src_mime =
'application/vnd.ms-powerpoint.slide.macroEnabled.12';
$this->log .=
'doesn\'t look like anything known<br />';
$this->log .=
'- MIME type couldn\'t be detected! (' . (string)
$this->file_src_mime .
')<br />';
// determine whether the file is an image
$this->file_is_image =
true;
$this->image_src_type =
$this->image_supported[$this->file_src_mime];
// if the file is an image, we gather some useful data
if ($this->file_is_image) {
if ($h =
fopen($this->file_src_pathname, 'r')) {
$this->image_src_x =
$info[0];
$this->image_src_y =
$info[1];
$this->image_dst_x =
$this->image_src_x;
$this->image_dst_y =
$this->image_src_y;
$this->image_src_pixels =
$this->image_src_x *
$this->image_src_y;
$this->file_is_image =
false;
$this->log .=
'- can\'t retrieve image information, image may have been tampered with<br />';
$this->error =
$this->translate('incorrect_file');
$this->log .=
'- can\'t read source file directly. open_basedir restriction in place?<br />';
$this->log .=
'<b>source variables</b><br />';
$this->log .=
'- You can use all these before calling process()<br />';
$this->log .=
' file_src_name : ' .
$this->file_src_name .
'<br />';
$this->log .=
' file_src_pathname : ' .
$this->file_src_pathname .
'<br />';
$this->log .=
' file_src_mime : ' .
$this->file_src_mime .
'<br />';
$this->log .=
' file_src_error : ' .
$this->file_src_error .
'<br />';
if ($this->file_is_image) {
$this->log .=
'- source file is an image<br />';
$this->log .=
' image_src_x : ' .
$this->image_src_x .
'<br />';
$this->log .=
' image_src_y : ' .
$this->image_src_y .
'<br />';
$this->log .=
' image_src_pixels : ' .
$this->image_src_pixels .
'<br />';
$this->log .=
' image_src_type : ' .
$this->image_src_type .
'<br />';
$this->log .=
' image_src_bits : ' .
$this->image_src_bits .
'<br />';
* Returns the version of GD
* @param boolean $full Optional flag to get precise version
* @return float GD version
static $gd_version =
null;
static $gd_full_version =
null;
if ($gd_version ===
null) {
if (function_exists('gd_info')) {
$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];
$gd_full_version =
'none';
* Creates directories recursively
* @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) );
* @param string $path Path to create
* @param integer $mode Optional permissions
* @return boolean Success
function _mkdir($path, $mode =
0777) {
$res =
@mkdir($path, $mode);
* Translate error messages
* @param string $str Message to translate
* @param array $tokens Optional token values
* @return string Translated string
function translate($str, $tokens =
array()) {
* @param string $color Color string
* @return array RGB colors
function getcolors($color) {
$r =
sscanf($color, "#%2x%2x%2x");
return array($red, $green, $blue);
* Creates a container image
* @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 ($x <
1) $x =
1; if ($y <
1) $y =
1;
// create a true color image
// this preserves transparency in PNGs, in true color
// creates a palette image
// preserves transparency for palette images, if the original image has transparency
// fills with background color if any is set
* Transfers an image from the container to the destination image
* @param resource $src_im Container image
* @param resource $dst_im Destination image
* @return resource Destination image
function imagetransfer($src_im, $dst_im) {
* If the output format is PNG, then we do it pixel per pixel to retain the alpha channel
* @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) {
for ($y =
$src_y; $y <
$src_h; $y++
) {
for ($x =
$src_x; $x <
$src_w; $x++
) {
if ($x +
$dst_x >=
0 &&
$x +
$dst_x <
$dst_w &&
$x +
$src_x >=
0 &&
$x +
$src_x <
$src_w
&&
$y +
$dst_y >=
0 &&
$y +
$dst_y <
$dst_h &&
$y +
$src_y >=
0 &&
$y +
$src_y <
$src_h) {
$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;
$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);
* 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)
* @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->translate('file_not_uploaded');
if (empty($server_path) ||
is_null($server_path)) {
$this->log .=
'<b>process file and return the content</b><br />';
if (substr($server_path, -
1, 1) !=
'\\') $server_path =
$server_path .
'\\';
if (substr($server_path, -
1, 1) !=
'/') $server_path =
$server_path .
'/';
$this->log .=
'<b>process file to ' .
$server_path .
'</b><br />';
$this->error =
$this->translate('file_too_big');
$this->log .=
'- file size OK<br />';
// turn dangerous scripts into text files
$this->error =
$this->translate('no_mime');
// check wether the mime type is allowed
foreach($this->allowed as $k =>
$v) {
if (($v1 ==
'*' &&
$v2 ==
'*') ||
($v1 ==
$m1 &&
($v2 ==
$m2 ||
$v2 ==
'*'))) {
// check wether the mime type is forbidden
if (($v1 ==
'*' &&
$v2 ==
'*') ||
($v1 ==
$m1 &&
($v2 ==
$m2 ||
$v2 ==
'*'))) {
$this->error =
$this->translate('incorrect_file');
// 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) {
$ratio =
$this->image_src_x /
$this->image_src_y;
$this->error =
$this->translate('image_too_wide');
$this->error =
$this->translate('image_too_narrow');
$this->error =
$this->translate('image_too_high');
$this->error =
$this->translate('image_too_short');
$this->error =
$this->translate('ratio_too_high');
$this->error =
$this->translate('ratio_too_low');
$this->error =
$this->translate('too_many_pixels');
$this->error =
$this->translate('not_enough_pixels');
$this->log .=
'- no image properties available, can\'t enforce dimension checks : ' .
$this->file_src_mime .
'<br />';
$this->file_dst_path =
$server_path;
// repopulate dst variables from src
$this->log .=
'- file name safe format<br />';
$this->log .=
'- destination variables<br />';
if (empty($this->file_dst_path) ||
is_null($this->file_dst_path)) {
$this->log .=
' file_dst_path : n/a<br />';
$this->log .=
' file_dst_path : ' .
$this->file_dst_path .
'<br />';
// do we do some image manipulation?
$image_manipulation =
($this->file_is_image &&
(
if ($image_manipulation) {
$this->log .=
'- image operation, keep extension<br />';
$this->log .=
'- image operation, change extension for conversion type<br />';
$this->log .=
'- no image operation, keep extension<br />';
$this->log .=
'- no auto_rename if same filename exists<br />';
$this->file_dst_pathname =
$this->file_dst_path .
$this->file_dst_name;
$this->log .=
'- checking for auto_rename<br />';
$this->file_dst_pathname =
$this->file_dst_path .
$this->file_dst_name;
$this->file_dst_pathname =
$this->file_dst_path .
$this->file_dst_name;
if ($cpt>
1) $this->log .=
' auto_rename to ' .
$this->file_dst_name .
'<br />';
$this->log .=
'- destination file details<br />';
$this->log .=
' file_dst_name : ' .
$this->file_dst_name .
'<br />';
$this->log .=
' file_dst_pathname : ' .
$this->file_dst_pathname .
'<br />';
$this->log .=
'- no overwrite checking<br />';
// 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<br />';
$this->file_src_pathname =
$this->file_src_temp;
$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()
$this->error =
$this->translate('source_missing');
// otherwise, if we don't check on uploaded files (local file for instance), we use file_exists()
$this->error =
$this->translate('source_missing');
// checks if the destination directory exists, and attempt to create it
$this->log .=
'- ' .
$this->file_dst_path .
' doesn\'t exist. Attempting creation:';
if (!$this->rmkdir($this->file_dst_path, $this->dir_chmod)) {
$this->log .=
' failed<br />';
$this->error =
$this->translate('destination_dir');
$this->log .=
' success<br />';
$this->error =
$this->translate('destination_dir_missing');
$this->error =
$this->translate('destination_path_not_dir');
// checks if the destination directory is writeable, and attempt to make it writeable
$this->log .=
'- ' .
$this->file_dst_path .
' is not writeable. Attempting chmod:';
$this->log .=
' failed<br />';
$this->error =
$this->translate('destination_dir_write');
$this->log .=
' success<br />';
$this->error =
$this->translate('destination_dir_write');
$this->error =
$this->translate('destination_path_write');
// 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)
$this->log .=
'- attempting to use a temp file:';
$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<br />';
$this->log .=
' temp file is: ' .
$this->file_src_temp .
'<br />';
$this->log .=
' failed<br />';
$this->error =
$this->translate('temp_file');
// we do a quick check to ensure the file is really an image
// we can do this only now, as it would have failed before in case of open_basedir
if ($image_manipulation &&
!@getimagesize($this->file_src_pathname)) {
$this->log .=
'- the file is not an image!<br />';
$image_manipulation =
false;
if ($image_manipulation) {
// checks if the source file is readable
if ($this->processed &&
!($f =
@fopen($this->file_src_pathname, 'r'))) {
$this->error =
$this->translate('source_not_readable');
// we now do all the image manipulations
$this->log .=
'- image resizing or conversion wanted<br />';
switch($this->image_src_type) {
$this->error =
$this->translate('no_create_support', array('JPEG'));
$this->error =
$this->translate('create_error', array('JPEG'));
$this->log .=
'- source image is JPEG<br />';
$this->error =
$this->translate('no_create_support', array('PNG'));
$this->error =
$this->translate('create_error', array('PNG'));
$this->log .=
'- source image is PNG<br />';
$this->error =
$this->translate('no_create_support', array('GIF'));
$this->error =
$this->translate('create_error', array('GIF'));
$this->log .=
'- source image is GIF<br />';
$this->error =
$this->translate('no_create_support', array('BMP'));
$this->error =
$this->translate('create_error', array('BMP'));
$this->log .=
'- source image is BMP<br />';
$this->error =
$this->translate('source_invalid');
$this->error =
$this->translate('gd_missing');
// we have to set image_convert if it is not already
// we set the default color to be the background color if we don't output in a transparent format
$this->image_src_x =
imagesx($image_src);
$this->image_src_y =
imagesy($image_src);
$this->log .=
'- image is detected as having a palette<br />';
$this->log .=
' palette image is detected as transparent<br />';
// if the image has a palette (GIF), we convert it to true color, preserving transparency
$this->log .=
' convert palette image to true color<br />';
for ($x =
0; $x <
$this->image_src_x; $x++
) {
for ($y =
0; $y <
$this->image_src_y; $y++
) {
imagesetpixel($true_color, $x, $y, ($rgb['alpha'] <<
24) |
($rgb['red'] <<
16) |
($rgb['green'] <<
8) |
$rgb['blue']);
$image_src =
$this->imagetransfer($true_color, $image_src);
$image_dst =
& $image_src;
// pre-crop image, before resizing
$ct =
$vars[0]; $cr =
$vars[1]; $cb =
$vars[2]; $cl =
$vars[3];
} else if (sizeof($vars) ==
2) {
$ct =
$vars[0]; $cr =
$vars[1]; $cb =
$vars[0]; $cl =
$vars[1];
$ct =
$vars[0]; $cr =
$vars[0]; $cb =
$vars[0]; $cl =
$vars[0];
$this->log .=
'- pre-crop image : ' .
$ct .
' ' .
$cr .
' ' .
$cb .
' ' .
$cl .
' <br />';
$this->image_src_x =
$this->image_src_x -
$cl -
$cr;
$this->image_src_y =
$this->image_src_y -
$ct -
$cb;
if ($this->image_src_x <
1) $this->image_src_x =
1;
if ($this->image_src_y <
1) $this->image_src_y =
1;
$tmp =
$this->imagecreatenew($this->image_src_x, $this->image_src_y);
// we copy the image into the recieving image
imagecopy($tmp, $image_dst, 0, 0, $cl, $ct, $this->image_src_x, $this->image_src_y);
// if we crop with negative margins, we have to make sure the extra bits are the right color, or transparent
if ($ct <
0 ||
$cr <
0 ||
$cb <
0 ||
$cl <
0 ) {
// use the background color if present
// fills eventual negative margins
if ($cr <
0) imagefilledrectangle($tmp, $this->image_src_x +
$cr, 0, $this->image_src_x, $this->image_src_y, $fill);
if ($cb <
0) imagefilledrectangle($tmp, 0, $this->image_src_y +
$cb, $this->image_src_x, $this->image_src_y, $fill);
// we transfert tmp into image_dst
$image_dst =
$this->imagetransfer($tmp, $image_dst);
// resize image (and move image_src_x, image_src_y dimensions into image_dst_x, image_dst_y)
$this->log .=
'- resizing...<br />';
$this->log .=
' calculate x size<br />';
$this->image_dst_x =
round(($this->image_src_x *
$this->image_y) /
$this->image_src_y);
$this->image_dst_y =
$this->image_y;
$this->log .=
' calculate y size<br />';
$this->image_dst_x =
$this->image_x;
$this->image_dst_y =
round(($this->image_src_y *
$this->image_x) /
$this->image_src_x);
$this->log .=
' calculate x/y size to match a number of pixels<br />';
$pixels =
$this->image_src_y *
$this->image_src_x;
$this->image_dst_x =
round($this->image_src_x *
$diff);
$this->image_dst_y =
round($this->image_src_y *
$diff);
$this->log .=
' check x/y sizes<br />';
$this->image_dst_x =
$this->image_x;
$this->image_dst_y =
$this->image_y;
if (($this->image_src_x/
$this->image_x) >
($this->image_src_y/
$this->image_y)) {
$this->image_dst_y =
$this->image_y;
$this->image_dst_x =
intval($this->image_src_x*
($this->image_y /
$this->image_src_y));
$ratio_crop['x'] =
$this->image_dst_x -
$this->image_x;
$ratio_crop['r'] =
$ratio_crop['x'];
$ratio_crop['l'] =
$ratio_crop['x'];
$ratio_crop['l'] =
round($ratio_crop['x']/
2);
$ratio_crop['r'] =
$ratio_crop['x'] -
$ratio_crop['l'];
$this->log .=
' ratio_crop_x : ' .
$ratio_crop['x'] .
' (' .
$ratio_crop['l'] .
';' .
$ratio_crop['r'] .
')<br />';
$this->image_dst_x =
$this->image_x;
$this->image_dst_y =
intval($this->image_src_y*
($this->image_x /
$this->image_src_x));
$ratio_crop['y'] =
$this->image_dst_y -
$this->image_y;
$ratio_crop['b'] =
$ratio_crop['y'];
$ratio_crop['t'] =
$ratio_crop['y'];
$ratio_crop['t'] =
round($ratio_crop['y']/
2);
$ratio_crop['b'] =
$ratio_crop['y'] -
$ratio_crop['t'];
$this->log .=
' ratio_crop_y : ' .
$ratio_crop['y'] .
' (' .
$ratio_crop['t'] .
';' .
$ratio_crop['b'] .
')<br />';
if (($this->image_src_x/
$this->image_x) <
($this->image_src_y/
$this->image_y)) {
$this->image_dst_y =
$this->image_y;
$this->image_dst_x =
intval($this->image_src_x*
($this->image_y /
$this->image_src_y));
$ratio_crop['x'] =
$this->image_dst_x -
$this->image_x;
$ratio_crop['r'] =
$ratio_crop['x'];
$ratio_crop['l'] =
$ratio_crop['x'];
$ratio_crop['l'] =
round($ratio_crop['x']/
2);
$ratio_crop['r'] =
$ratio_crop['x'] -
$ratio_crop['l'];
$this->log .=
' ratio_fill_x : ' .
$ratio_crop['x'] .
' (' .
$ratio_crop['l'] .
';' .
$ratio_crop['r'] .
')<br />';
$this->image_dst_x =
$this->image_x;
$this->image_dst_y =
intval($this->image_src_y*
($this->image_x /
$this->image_src_x));
$ratio_crop['y'] =
$this->image_dst_y -
$this->image_y;
$ratio_crop['b'] =
$ratio_crop['y'];
$ratio_crop['t'] =
$ratio_crop['y'];
$ratio_crop['t'] =
round($ratio_crop['y']/
2);
$ratio_crop['b'] =
$ratio_crop['y'] -
$ratio_crop['t'];
$this->log .=
' ratio_fill_y : ' .
$ratio_crop['y'] .
' (' .
$ratio_crop['t'] .
';' .
$ratio_crop['b'] .
')<br />';
if (($this->image_src_x/
$this->image_x) >
($this->image_src_y/
$this->image_y)) {
$this->image_dst_x =
$this->image_x;
$this->image_dst_y =
intval($this->image_src_y*
($this->image_x /
$this->image_src_x));
$this->image_dst_y =
$this->image_y;
$this->image_dst_x =
intval($this->image_src_x*
($this->image_y /
$this->image_src_y));
$this->log .=
' doesn\'t calculate x/y sizes<br />';
$this->image_dst_x =
$this->image_src_x;
$this->image_dst_y =
$this->image_src_y;
$this->log .=
' use plain sizes<br />';
$this->image_dst_x =
$this->image_x;
$this->image_dst_y =
$this->image_y;
if ($this->image_dst_x <
1) $this->image_dst_x =
1;
if ($this->image_dst_y <
1) $this->image_dst_y =
1;
$tmp =
$this->imagecreatenew($this->image_dst_x, $this->image_dst_y);
$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);
$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);
$this->log .=
' resized image object created<br />';
$this->log .=
' image_src_x y : ' .
$this->image_src_x .
' x ' .
$this->image_src_y .
'<br />';
$this->log .=
' image_dst_x y : ' .
$this->image_dst_x .
' x ' .
$this->image_dst_y .
'<br />';
// we transfert tmp into image_dst
$image_dst =
$this->imagetransfer($tmp, $image_dst);
$this->image_dst_x =
$this->image_src_x;
$this->image_dst_y =
$this->image_src_y;
// crop image (and also crops if image_ratio_crop is used)
$ct =
$vars[0]; $cr =
$vars[1]; $cb =
$vars[2]; $cl =
$vars[3];
} else if (sizeof($vars) ==
2) {
$ct =
$vars[0]; $cr =
$vars[1]; $cb =
$vars[0]; $cl =
$vars[1];
$ct =
$vars[0]; $cr =
$vars[0]; $cb =
$vars[0]; $cl =
$vars[0];
// we adjust the cropping if we use image_ratio_crop
$this->log .=
'- crop image : ' .
$ct .
' ' .
$cr .
' ' .
$cb .
' ' .
$cl .
' <br />';
$this->image_dst_x =
$this->image_dst_x -
$cl -
$cr;
$this->image_dst_y =
$this->image_dst_y -
$ct -
$cb;
if ($this->image_dst_x <
1) $this->image_dst_x =
1;
if ($this->image_dst_y <
1) $this->image_dst_y =
1;
$tmp =
$this->imagecreatenew($this->image_dst_x, $this->image_dst_y);
// we copy the image into the recieving image
imagecopy($tmp, $image_dst, 0, 0, $cl, $ct, $this->image_dst_x, $this->image_dst_y);
// if we crop with negative margins, we have to make sure the extra bits are the right color, or transparent
if ($ct <
0 ||
$cr <
0 ||
$cb <
0 ||
$cl <
0 ) {
// use the background color if present
// fills eventual negative margins
if ($cr <
0) imagefilledrectangle($tmp, $this->image_dst_x +
$cr, 0, $this->image_dst_x, $this->image_dst_y, $fill);
if ($cb <
0) imagefilledrectangle($tmp, 0, $this->image_dst_y +
$cb, $this->image_dst_x, $this->image_dst_y, $fill);
// we transfert tmp into image_dst
$image_dst =
$this->imagetransfer($tmp, $image_dst);
if ($gd_version >=
2 &&
!empty($this->image_flip)) {
$tmp =
$this->imagecreatenew($this->image_dst_x, $this->image_dst_y);
for ($x =
0; $x <
$this->image_dst_x; $x++
) {
for ($y =
0; $y <
$this->image_dst_y; $y++
){
imagecopy($tmp, $image_dst, $this->image_dst_x -
$x -
1, $y, $x, $y, 1, 1);
imagecopy($tmp, $image_dst, $x, $this->image_dst_y -
$y -
1, $x, $y, 1, 1);
// we transfert tmp into image_dst
$image_dst =
$this->imagetransfer($tmp, $image_dst);
$tmp =
$this->imagecreatenew($this->image_dst_y, $this->image_dst_x);
$tmp =
$this->imagecreatenew($this->image_dst_x, $this->image_dst_y);
for ($x =
0; $x <
$this->image_dst_x; $x++
) {
for ($y =
0; $y <
$this->image_dst_y; $y++
){
imagecopy($tmp, $image_dst, $y, $x, $x, $this->image_dst_y -
$y -
1, 1, 1);
imagecopy($tmp, $image_dst, $x, $y, $this->image_dst_x -
$x -
1, $this->image_dst_y -
$y -
1, 1, 1);
imagecopy($tmp, $image_dst, $y, $x, $this->image_dst_x -
$x -
1, $y, 1, 1);
imagecopy($tmp, $image_dst, $x, $y, $x, $y, 1, 1);
$this->image_dst_y =
$this->image_dst_x;
// we transfert tmp into image_dst
$image_dst =
$this->imagetransfer($tmp, $image_dst);
$this->log .=
'- apply color overlay<br />';
$this->imagecopymergealpha($image_dst, $filter, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, $this->image_overlay_percent);
// add brightness, contrast and tint, turns to greyscale and inverts colors
$this->log .=
'- apply tint, light, contrast correction, negative, greyscale and threshold<br />';
for($y=
0; $y <
$this->image_dst_y; $y++
) {
for($x=
0; $x <
$this->image_dst_x; $x++
) {
$r =
$g =
$b =
round((0.2125 *
$pixel['red']) +
(0.7154 *
$pixel['green']) +
(0.0721 *
$pixel['blue']));
$c =
(round($pixel['red'] +
$pixel['green'] +
$pixel['blue']) /
3) -
127;
$r =
min(round($tint_red *
$pixel['red'] /
169), 255);
$g =
min(round($tint_green *
$pixel['green'] /
169), 255);
$b =
min(round($tint_blue *
$pixel['blue'] /
169), 255);
$r =
round(255 -
$pixel['red']);
$g =
round(255 -
$pixel['green']);
$b =
round(255 -
$pixel['blue']);
$ct =
$vars[0]; $cr =
$vars[1]; $cb =
$vars[2]; $cl =
$vars[3];
} else if (sizeof($vars) ==
2) {
$ct =
$vars[0]; $cr =
$vars[1]; $cb =
$vars[0]; $cl =
$vars[1];
$ct =
$vars[0]; $cr =
$vars[0]; $cb =
$vars[0]; $cl =
$vars[0];
$this->image_dst_x =
$this->image_dst_x +
$cl +
$cr;
$this->image_dst_y =
$this->image_dst_y +
$ct +
$cb;
// we now create an image, that we fill with the border color
$tmp =
$this->imagecreatenew($this->image_dst_x, $this->image_dst_y);
// we then copy the source image into the new image, without merging so that only the border is actually kept
imagecopy($tmp, $image_dst, $cl, $ct, 0, 0, $this->image_dst_x -
$cr -
$cl, $this->image_dst_y -
$cb -
$ct);
// we transfert tmp into image_dst
$image_dst =
$this->imagetransfer($tmp, $image_dst);
$this->image_dst_x =
$this->image_dst_x +
($nb *
2);
$this->image_dst_y =
$this->image_dst_y +
($nb *
2);
$tmp =
$this->imagecreatenew($this->image_dst_x, $this->image_dst_y);
imagecopy($tmp, $image_dst, $nb, $nb, 0, 0, $this->image_dst_x -
($nb *
2), $this->image_dst_y -
($nb *
2));
for ($i=
0; $i<
$nb; $i++
) {
list
($red, $green, $blue) =
$this->getcolors($vars[$i]);
imageline($tmp, $i, $i, $this->image_dst_x -
$i -
1, $i, $c);
imageline($tmp, $this->image_dst_x -
$i -
1, $this->image_dst_y -
$i -
1, $this->image_dst_x -
$i -
1, $i, $c);
imageline($tmp, $this->image_dst_x -
$i -
1, $this->image_dst_y -
$i -
1, $i, $this->image_dst_y -
$i -
1, $c);
imageline($tmp, $i, $i, $i, $this->image_dst_y -
$i -
1, $c);
imageline($tmp, $i, $i, $this->image_dst_x -
$i -
1, $i, $c);
imageline($tmp, $this->image_dst_x -
$nb +
$i, $this->image_dst_y -
$nb +
$i, $this->image_dst_x -
$nb +
$i, $nb -
$i, $c);
imageline($tmp, $this->image_dst_x -
$nb +
$i, $this->image_dst_y -
$nb +
$i, $nb -
$i, $this->image_dst_y -
$nb +
$i, $c);
imageline($tmp, $i, $i, $i, $this->image_dst_y -
$i -
1, $c);
// we transfert tmp into image_dst
$image_dst =
$this->imagetransfer($tmp, $image_dst);
$tmp =
$this->imagecreatenew($this->image_dst_x, $this->image_dst_y);
imagecopy($tmp, $image_dst, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y);
imageline($tmp, $i, $i, $this->image_dst_x -
$i -
1, $i, $c1);
imageline($tmp, $this->image_dst_x -
$i -
1, $this->image_dst_y -
$i, $this->image_dst_x -
$i -
1, $i, $c2);
imageline($tmp, $this->image_dst_x -
$i -
1, $this->image_dst_y -
$i -
1, $i, $this->image_dst_y -
$i -
1, $c2);
imageline($tmp, $i, $i, $i, $this->image_dst_y -
$i -
1, $c1);
// we transfert tmp into image_dst
$image_dst =
$this->imagetransfer($tmp, $image_dst);
$this->log .=
'- add watermark<br />';
$watermark_type =
(array_key_exists(2, $watermark_info) ?
$watermark_info[2] :
null); // 1 = GIF, 2 = JPG, 3 = PNG
$watermark_checked =
false;
if ($watermark_type ==
IMAGETYPE_GIF) {
$this->error =
$this->translate('watermark_no_create_support', array('GIF'));
$this->error =
$this->translate('watermark_create_error', array('GIF'));
$this->log .=
' watermark source image is GIF<br />';
$watermark_checked =
true;
} else if ($watermark_type ==
IMAGETYPE_JPEG) {
$this->error =
$this->translate('watermark_no_create_support', array('JPEG'));
$this->error =
$this->translate('watermark_create_error', array('JPEG'));
$this->log .=
' watermark source image is JPEG<br />';
$watermark_checked =
true;
} else if ($watermark_type ==
IMAGETYPE_PNG) {
$this->error =
$this->translate('watermark_no_create_support', array('PNG'));
$this->error =
$this->translate('watermark_create_error', array('PNG'));
$this->log .=
' watermark source image is PNG<br />';
$watermark_checked =
true;
} else if ($watermark_type ==
IMAGETYPE_BMP) {
$this->error =
$this->translate('watermark_no_create_support', array('BMP'));
$this->error =
$this->translate('watermark_create_error', array('BMP'));
$this->log .=
' watermark source image is BMP<br />';
$watermark_checked =
true;
$this->error =
$this->translate('watermark_invalid');
if ($watermark_checked) {
$watermark_width =
imagesx($filter);
$watermark_height =
imagesy($filter);
$watermark_x =
$this->image_dst_x -
$watermark_width;
$watermark_x =
($this->image_dst_x -
$watermark_width) /
2;
$watermark_y =
$this->image_dst_y -
$watermark_height;
$watermark_y =
($this->image_dst_y -
$watermark_height) /
2;
imagecopyresampled ($image_dst, $filter, $watermark_x, $watermark_y, 0, 0, $watermark_width, $watermark_height, $watermark_width, $watermark_height);
$this->error =
$this->translate('watermark_invalid');
$this->log .=
'- add text<br />';
// calculate sizes in human readable format
$src_size_human =
($src_size >
1024 ?
$src_size_mb .
" MB" :
$src_size_kb .
" kb");
$this->file_src_pathname,
$this->file_dst_pathname,
(isset
($_SERVER['HTTP_HOST']) ?
$_SERVER['HTTP_HOST'] :
'n/a'),
(isset
($_SERVER['SERVER_NAME']) ?
$_SERVER['SERVER_NAME'] :
'n/a'),
(isset
($_SERVER['REMOTE_ADDR']) ?
$_SERVER['REMOTE_ADDR'] :
'n/a'),
// if the font is a string, we assume that we might want to load a font
$this->log .=
' try to load font ' .
$this->image_text_font .
'... ';
$this->log .=
'success<br />';
$this->log .=
'error<br />';
foreach ($text as $k =>
$v) {
$h =
($char_width *
strlen($v));
if ($h >
$text_height) $text_height =
$h;
$line_width =
$char_height;
$w =
($char_width *
strlen($v));
if ($w >
$text_width) $text_width =
$w;
$line_height =
$char_height;
$text_x =
$this->image_dst_x -
$text_width +
$this->image_text_x;
$text_x =
$this->image_dst_x -
$text_width;
$text_x =
($this->image_dst_x -
$text_width) /
2;
$text_y =
$this->image_dst_y -
$text_height +
$this->image_text_y;
$text_y =
$this->image_dst_y -
$text_height;
$text_y =
($this->image_dst_y -
$text_height) /
2;
// add a background, maybe transparent
imagefilledrectangle($image_dst, $text_x, $text_y, $text_x +
$text_width, $text_y +
$text_height, $background_color);
// add the text, maybe transparent
if ($t_width <
0) $t_width =
0;
if ($t_height <
0) $t_height =
0;
$filter =
$this->imagecreatenew($t_width, $t_height, false, true);
foreach ($text as $k =>
$v) {
$this->imagecopymergealpha($image_dst, $filter, $text_x, $text_y, 0, 0, $t_width, $t_height, $this->image_text_percent);
$text_color =
imageColorAllocate($image_dst ,$red, $green, $blue);
foreach ($text as $k =>
$v) {
// we decode image_reflection_height, which can be a integer, a string in pixels or percentage
if (strpos($image_reflection_height, '%')>
0) $image_reflection_height =
$this->image_dst_y *
(str_replace('%','',$image_reflection_height /
100));
if (strpos($image_reflection_height, 'px')>
0) $image_reflection_height =
str_replace('px','',$image_reflection_height);
$image_reflection_height = (int)
$image_reflection_height;
if ($image_reflection_height >
$this->image_dst_y) $image_reflection_height =
$this->image_dst_y;
// create the new destination image
$tmp =
$this->imagecreatenew($this->image_dst_x, $this->image_dst_y +
$image_reflection_height +
$this->image_reflection_space, true);
// copy the original image
// we have to make sure the extra bit is the right color, or transparent
// use the background color if present
// fill in from the edge of the extra bit
for ($y =
0; $y <
$image_reflection_height; $y++
) {
for ($x =
0; $x <
$this->image_dst_x; $x++
) {
$alpha_o =
1 -
($pixel_o['alpha'] /
127);
$alpha_b =
1 -
($pixel_b['alpha'] /
127);
$opacity =
$alpha_o *
$transparency /
100;
$red =
round((($pixel_o['red'] *
$opacity) +
($pixel_b['red'] ) *
$alpha_b) /
($alpha_b +
$opacity));
$green =
round((($pixel_o['green'] *
$opacity) +
($pixel_b['green']) *
$alpha_b) /
($alpha_b +
$opacity));
$blue =
round((($pixel_o['blue'] *
$opacity) +
($pixel_b['blue'] ) *
$alpha_b) /
($alpha_b +
$opacity));
$alpha =
($opacity +
$alpha_b);
if ($alpha >
1) $alpha =
1;
$alpha =
round((1 -
$alpha) *
127);
// copy the resulting image into the destination image
$image_dst =
$this->imagetransfer($tmp, $image_dst);
// reduce the JPEG image to a set desired size
// inspired by: JPEGReducer class version 1, 25 November 2004, Author: Huda M ElMatsani, justhuda at netscape dot net
$this->log .=
'- JPEG desired file size : ' .
$this->jpeg_size .
'<br />';
// calculate size of each image. 75%, 50%, and 25% quality
// calculate gradient of size reduction by quality
$mgrad1 =
25 /
($size50-
$size25);
$mgrad2 =
25 /
($size75-
$size50);
$mgrad3 =
50 /
($size75-
$size25);
$mgrad =
($mgrad1 +
$mgrad2 +
$mgrad3) /
3;
// result of approx. quality factor for expected size
} elseif ($q_factor>
100) {
$this->log .=
' JPEG quality factor set to ' .
$this->jpeg_quality .
'<br />';
// converts image from true color, and fix transparency if needed
$this->log .=
'- converting...<br />';
// if the image is true color, we convert it to a palette
$this->log .=
' true color to palette<br />';
// creates a black and white mask
for ($x =
0; $x <
$this->image_dst_x; $x++
) {
for ($y =
0; $y <
$this->image_dst_y; $y++
) {
$mask[$x][$y] =
$pixel['alpha'];
// first, we merge the image with the background color, so we know which colors we will have
for ($x =
0; $x <
$this->image_dst_x; $x++
) {
for ($y =
0; $y <
$this->image_dst_y; $y++
) {
// we have some transparency. we combine the color with the default color
$alpha =
($mask[$x][$y] /
127);
$pixel['red'] =
round(($pixel['red'] *
(1 -
$alpha) +
$red *
($alpha)));
$pixel['green'] =
round(($pixel['green'] *
(1 -
$alpha) +
$green *
($alpha)));
$pixel['blue'] =
round(($pixel['blue'] *
(1 -
$alpha) +
$blue *
($alpha)));
// transfrom the true color image into palette, with it merged default color in
// we will have the best color possible, including the background
// make the transparent areas transparent
for ($x =
0; $x <
$this->image_dst_x; $x++
) {
for ($y =
0; $y <
$this->image_dst_y; $y++
) {
// we test wether we have enough opacity to justify keeping the color
if ($mask[$x][$y] >
120) imagesetpixel($image_dst, $x, $y, $transparency);
// if the image doesn't support any transparency, then we merge it with the default color
$this->log .=
' fills in transparency with default color<br />';
// make the transaparent areas transparent
for ($x =
0; $x <
$this->image_dst_x; $x++
) {
for ($y =
0; $y <
$this->image_dst_y; $y++
) {
// we test wether we have some transparency, in which case we will merge the colors
$pixel =
array('red' =>
($rgba >>
16) & 0xFF,
'green' =>
($rgba >>
8) & 0xFF,
'alpha' =>
($rgba & 0x7F000000) >>
24);
if ($pixel['alpha'] ==
127) {
// we have full transparency. we make the pixel transparent
} else if ($pixel['alpha'] >
0) {
// we have some transparency. we combine the color with the default color
$alpha =
($pixel['alpha'] /
127);
$pixel['red'] =
round(($pixel['red'] *
(1 -
$alpha) +
$red *
($alpha)));
$pixel['green'] =
round(($pixel['green'] *
(1 -
$alpha) +
$green *
($alpha)));
$pixel['blue'] =
round(($pixel['blue'] *
(1 -
$alpha) +
$blue *
($alpha)));
$color =
imagecolorclosest($image_dst, $pixel['red'], $pixel['green'], $pixel['blue']);
$this->log .=
'- saving image...<br />';
$this->error =
$this->translate('file_create', array('JPEG'));
$this->log .=
' JPEG image created<br />';
$result =
@imagepng($image_dst, $this->file_dst_pathname);
$this->error =
$this->translate('file_create', array('PNG'));
$this->log .=
' PNG image created<br />';
$result =
@imagegif($image_dst, $this->file_dst_pathname);
$this->error =
$this->translate('file_create', array('GIF'));
$this->log .=
' GIF image created<br />';
$result =
$this->imagebmp($image_dst, $this->file_dst_pathname);
$this->error =
$this->translate('file_create', array('BMP'));
$this->log .=
' BMP image created<br />';
$this->error =
$this->translate('no_conversion_type');
$this->log .=
' image objects destroyed<br />';
$this->log .=
'- no image processing wanted<br />';
// copy the file to its final destination. we don't use move_uploaded_file here
// if we happen to have open_basedir restrictions, it is a temp file that we copy, not the original uploaded file
if (!copy($this->file_src_pathname, $this->file_dst_pathname)) {
$this->error =
$this->translate('copy_failed');
// returns the file, so that its content can be received by the caller
if ($return_content ===
FALSE) {
$this->error =
$this->translate('reading_failed');
$this->log .=
'- <b>process OK</b><br />';
$this->log .=
'- <b>error</b>: ' .
$this->error .
'<br />';
// we reinit all the vars
// we may return the image content
if ($return_mode) return $return_content;
* Deletes the uploaded file from its temporary location
* When PHP uploads a file, it stores it in a temporary location.
* When you {@link process} the file, you actually copy the resulting file to the given location, it doesn't alter the original file.
* Once you have processed the file as many times as you wanted, you can delete the uploaded file.
* If there is open_basedir restrictions, the uploaded file is in fact a temporary file
* You might want not to use this function if you work on local files, as it will delete the source file
$this->log .=
'<b>cleanup</b><br />';
$this->log .=
'- delete temp file ' .
$this->file_src_pathname .
'<br />';
@unlink($this->file_src_pathname);
* This function has been written by DHKold, and is used with permission of the author
if (! $f1 =
fopen($filename,"rb")) return false;
$file =
unpack("vfile_type/Vfile_size/Vreserved/Vbitmap_offset", fread($f1,14));
if ($file['file_type'] !=
19778) return false;
$bmp =
unpack('Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel'.
'/Vcompression/Vsize_bitmap/Vhoriz_resolution'.
'/Vvert_resolution/Vcolors_used/Vcolors_important', fread($f1,40));
$bmp['colors'] =
pow(2,$bmp['bits_per_pixel']);
if ($bmp['size_bitmap'] ==
0) $bmp['size_bitmap'] =
$file['file_size'] -
$file['bitmap_offset'];
$bmp['bytes_per_pixel'] =
$bmp['bits_per_pixel']/
8;
$bmp['bytes_per_pixel2'] =
ceil($bmp['bytes_per_pixel']);
$bmp['decal'] =
($bmp['width']*
$bmp['bytes_per_pixel']/
4);
$bmp['decal'] -=
floor($bmp['width']*
$bmp['bytes_per_pixel']/
4);
$bmp['decal'] =
4-
(4*
$bmp['decal']);
if ($bmp['decal'] ==
4) $bmp['decal'] =
0;
if ($bmp['colors'] <
16777216) {
$palette =
unpack('V'.
$bmp['colors'], fread($f1,$bmp['colors']*
4));
$im =
fread($f1,$bmp['size_bitmap']);
while ($X <
$bmp['width']) {
if ($bmp['bits_per_pixel'] ==
24)
elseif ($bmp['bits_per_pixel'] ==
16) {
$color[1] =
$palette[$color[1]+
1];
} elseif ($bmp['bits_per_pixel'] ==
8) {
$color[1] =
$palette[$color[1]+
1];
} elseif ($bmp['bits_per_pixel'] ==
4) {
if (($P*
2)%
2 ==
0) $color[1] =
($color[1] >>
4) ; else $color[1] =
($color[1] & 0x0F);
$color[1] =
$palette[$color[1]+
1];
} elseif ($bmp['bits_per_pixel'] ==
1) {
if (($P*
8)%
8 ==
0) $color[1] =
$color[1] >>
7;
elseif (($P*
8)%
8 ==
1) $color[1] =
($color[1] & 0x40)>>
6;
elseif (($P*
8)%
8 ==
2) $color[1] =
($color[1] & 0x20)>>
5;
elseif (($P*
8)%
8 ==
3) $color[1] =
($color[1] & 0x10)>>
4;
elseif (($P*
8)%
8 ==
4) $color[1] =
($color[1] & 0x8)>>
3;
elseif (($P*
8)%
8 ==
5) $color[1] =
($color[1] & 0x4)>>
2;
elseif (($P*
8)%
8 ==
6) $color[1] =
($color[1] & 0x2)>>
1;
elseif (($P*
8)%
8 ==
7) $color[1] =
($color[1] & 0x1);
$color[1] =
$palette[$color[1]+
1];
$P +=
$bmp['bytes_per_pixel'];
* This function has been published on the PHP website, and can be used freely
function imagebmp(&$im, $filename =
"") {
// if the image is not true color, we convert it first
$biStride =
($biBPLine +
3) & ~
3;
$biSizeImage =
$biStride *
$h;
$bfSize =
$bfOffBits +
$biSizeImage;
$result .=
substr('BM', 0, 2);
$result .=
pack ('VvvV', $bfSize, 0, 0, $bfOffBits);
$result .=
pack ('VVVvvVVVVVV', 40, $w, $h, 1, 24, 0, $biSizeImage, 0, 0, 0, 0);
$numpad =
$biStride -
$biBPLine;
for ($y =
$h -
1; $y >=
0; --
$y) {
for ($x =
0; $x <
$w; ++
$x) {
for ($i =
0; $i <
$numpad; ++
$i)
$result .=
pack ('C', 0);
$file =
fopen($filename, "wb");