Wallogit.com
2017 © Pedro Peláez
Images upload
Homepage : http://www.verot.net/php_class_upload.htm, (*1)
Demo : http://www.verot.net/php_class_upload_samples.htm, (*2)
Commercial use: http://www.verot.net/php_class_upload_license.htm, (*3)
composer require "egorryaroslavl/classupload":"1.0.0"
This class 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., (*4)
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., (*5)
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 Flash uploaders and XMLHttpRequest., (*6)
The class works with PHP 4, 5 and 7, and its error messages can be localized at will., (*7)
Edit your composer.json file to include the following:, (*8)
{
"require": {
"verot/class.upload.php": "dev-master"
}
}
Check out the test/ directory, which you can load in your browser. You can test the class and its different ways to instantiate it, see some code examples, and run some tests., (*9)
Create a simple HTML file, with a form such as:, (*10)
<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"> </form>
Create a file called upload.php (into which you have first loaded the class):, (*11)
$handle = new upload($_FILES['image_field']);
if ($handle->uploaded) {
$handle->file_new_name_body = 'image_resized';
$handle->image_resize = true;
$handle->image_x = 100;
$handle->image_ratio_y = true;
$handle->process('/home/user/files/');
if ($handle->processed) {
echo 'image resized';
$handle->clean();
} else {
echo 'error : ' . $handle->error;
}
}
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)., (*12)
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., (*13)
Then you call the function 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., (*14)
You can repeat setting up a new set of processing variables, and calling process() again as many times as you want. When you have finished, you can call clean() to delete the original uploaded file., (*15)
If you don't set any processing parameters and call process() just after instanciating the class. The uploaded file will be simply copied to the given location without any alteration or checks., (*16)
Don't forget to add enctype="multipart/form-data" in your form tag <form> if you want your form to upload the file., (*17)
Instantiate the class with the local filename, as following:, (*18)
$handle = new upload('/home/user/myfile.jpg');
Instantiate the class with the special php: keyword, as following:, (*19)
$handle = new upload('php:'.$_SERVER['HTTP_X_FILE_NAME']);
Prefixing the argument with php: tells the class to retrieve the uploaded data in php://input, and the rest is the stream's filename, which is generally in $_SERVER['HTTP_X_FILE_NAME']. But you can use any other name you see fit:, (*20)
$handle = new upload('php:mycustomname.ext');
Instantiate the class with the special data: keyword, as following:, (*21)
$handle = new upload('data:'.$file_contents);
If your data is base64-encoded, the class provides a simple base64: keyword, which will decode your data prior to using it:, (*22)
$handle = new upload('base64:'.$base64_file_contents);
Instantiate the class with a second argument being the language code:, (*23)
$handle = new upload($_FILES['image_field'], 'fr_FR');
$handle = new upload('/home/user/myfile.jpg', 'fr_FR');
Simply call process() without an argument (or with null as first argument):, (*24)
$handle = new upload($_FILES['image_field']);
header('Content-type: ' . $handle->file_src_mime);
echo $handle->Process();
die();
Or if you want to force the download of the file:, (*25)
$handle = new upload($_FILES['image_field']);
header('Content-type: ' . $handle->file_src_mime);
header("Content-Disposition: attachment; filename=".rawurlencode($handle->file_src_name).";");
echo $handle->Process();
die();
If the class doesn't do what you want it to do, you can display the log, in order to see in details what the class does. To obtain the log, just add this line at the end of your code:, (*26)
echo $handle->log;
Your problem may have been already discussed in the Frequently Asked Questions : http://www.verot.net/php_class_upload_faq.htm, (*27)
Failing that, you can search in the forums, and ask a question there: http://www.verot.net/php_class_upload_forum.htm. Please don't use Github issues to ask for help., (*28)
Note: all the parameters in this section are reset after each process., (*29)
$handle->file_new_name_body = 'new name';
$handle->file_name_body_add = '_uploaded';
$handle->file_name_body_pre = 'thumb_';
$handle->file_new_name_ext = 'txt';
$handle->file_safe_name = true;
$handle->file_force_extension = true;
$handle->file_overwrite = true;
$handle->file_auto_rename = true;
$handle->dir_auto_create = true;
$handle->dir_auto_chmod = true;
$handle->dir_chmod = 0777;
$handle->file_max_size = '1024'; // 1KB
allowed list (default: true)$handle->mime_check = true;
$handle->no_script = false;
init())$handle->allowed = array('application/pdf','application/msword', 'image/*');
init())$handle->forbidden = array('application/*');
$handle->image_convert = 'jpg';
$handle->image_background_color = '#FF00FF';
$handle->image_default_color = '#FF00FF';
$handle->png_compression = 9;
$handle->jpeg_quality = 50;
jpeg_quality so the output image fits within the size (default: null)$handle->jpeg_size = 3072;
$handle->image_interlace = true;
The following eight settings can be used to invalidate an upload if the file is an image (note that open_basedir restrictions prevent the use of these settings), (*30)
$handle->image_max_width = 200;
$handle->image_max_height = 100;
$handle->image_max_pixels = 50000;
$handle->image_max_ratio = 1.5;
$handle->image_min_width = 100;
$handle->image_min_height = 500;
$handle->image_min_pixels = 20000;
$handle->image_min_ratio = 0.5;
$handle->image_resize = true;
The following variables are used only if image_resize == true, (*31)
$handle->image_x = 100;
$handle->image_y = 200;
Use either one of the following, (*32)
image_x AND image_y as max sizes if true (default: false)$handle->image_ratio = true;
image_x AND 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)$handle->image_ratio_crop = true;
image_x AND 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)$handle->image_ratio_fill = true;
image_x from image_y and conserving the original sizes ratio (default: false)$handle->image_ratio_x = true;
image_y from image_x and conserving the original sizes ratio (default: false)$handle->image_ratio_y = true;
image_y and image_x to match a the number of pixels (default: false)$handle->image_ratio_pixels = 25000;
And eventually prevent enlarging or shrinking images, (*33)
$handle->image_no_enlarging = true;
$handle->image_no_shrinking = true;
The following image manipulations require GD2+, (*34)
$handle->image_brightness = 40;
$handle->image_contrast = 50;
$handle->image_opacity = 50;
$handle->image_tint_color = '#FF0000';
$handle->image_overlay_color = '#FF0000';
image_overlay_color is set, determines the opacity (default: 50)$handle->image_overlay_opacity = 20;
$handle->image_negative = true;
$handle->image_greyscale = true;
$handle->image_threshold = 20;
$handle->image_pixelate = 10;
$handle->image_unsharp = true;
$handle->image_unsharp_amount = 120;
$handle->image_unsharp_radius = 1;
$handle->image_unsharp_threshold = 0;
$handle->image_text = 'test';
$handle->image_text_direction = 'v';
$handle->image_text_color = '#FF0000';
$handle->image_text_opacity = 50;
$handle->image_text_background = '#FFFFFF';
$handle->image_text_background_opacity = 50;
$handle->image_text_font = 4; // or './font.gdf' or './font.ttf'
$handle->image_text_size = 24;
$handle->image_text_angle = 45;
$handle->image_text_x = 5;
$handle->image_text_y = 5;
$handle->image_text_position = 'LR';
image_text_padding_x and image_text_padding_y (default: 0)$handle->image_text_padding = 5;
$handle->image_text_padding_x = 2;
$handle->image_text_padding_y = 10;
$handle->image_text_alignment = 'R';
$handle->image_text_line_spacing = 3;
$handle->image_auto_rotate = false;
$handle->image_flip = 'h';
$handle->image_rotate = 90;
$handle->image_crop = array(50,40,30,20); OR '-20 20%'...
$handle->image_precrop = array(50,40,30,20); OR '-20 20%'...
$handle->image_bevel = 20;
$handle->image_bevel_color1 = '#FFFFFF';
$handle->image_bevel_color2 = '#000000';
$handle->image_border = '3px'; OR '-20 20%' OR array(3,2)...
$handle->image_border_color = '#FFFFFF';
$handle->image_border_opacity = 50;
$handle->image_border_transparent = '3px'; OR '-20 20%' OR array(3,2)...
$handle->image_frame = 2;
$handle->image_frame_colors = array('#999999', '#FF0000', '#666666', '#333333', '#000000');
$handle->image_frame_opacity = 50;
$handle->image_watermark = 'watermark.png';
$handle->image_watermark_x = 5;
$handle->image_watermark_y = 5;
$handle->image_watermark_position = 'LR';
$handle->image_watermark_no_zoom_in = false;
$handle->image_watermark_no_zoom_out = true;
$handle->image_reflection_height = '25%';
$handle->image_reflection_space = 3;
image_default_color (default: #FFFFFF)$handle->image_default_color = '#000000';
$handle->image_reflection_opacity = 60;
process()
If the file is a supported image type (and open_basedir restrictions allow it), (*35)
process()
If the file is a supported image type, (*36)
Most of the image operations require GD. GD2 is greatly recommended, (*37)
The class requires PHP 4.3+, and is compatible with PHP 5 and PHP 7, (*38)
dev, (*39)
image_no_enlarging and image_no_shrinking to replace image_ratio_no_zoom_in and image_ratio_no_zoom_out
v 0.33 16/07/2016, (*40)
v 0.32 15/01/2013, (*41)
image_pixelate
image_interlace
png_compression to change PNG compressoin levelexec() if Suhosin is enabledv 0.31 11/04/2011, (*42)
exec() and ini_get_all() function are not disabled if we want to use themallowed and forbidden can now accept stringsfile_force_extension to allow extension-less files if neededfile_max_size and jpeg_size
image_opacity to change picture opacityimage_border_opacity to allow semi-transparent bordersimage_frame_opacity to allow semi-transparent framesimage_border_transparent to allow borders fading to transparentimage_overlay_percent into image_overlay_opacity
image_text_percent into image_text_opacity
image_text_background_percent into image_text_background_opacity
v 0.30 05/09/2010, (*43)
image_unsharp is true. added image_unsharp_amount, image_unsharp_radius, and image_unsharp_threshold
no_script is activated and several process() are calledimage_ratio_fill top and left fillingimage_watermark_no_zoom_in and image_watermark_no_zoom_out to allow the watermark to be resized down (or up) to fit in the image. By default, the watermark may be resized down, but not up.v 0.29 03/02/2010, (*44)
split() with explode()
image_dst_x/y with image_src_x/y
mime_fileinfo, mime_file, mime_magic and mime_getimagesize from the docs since they are used before process
v 0.28 10/08/2009, (*45)
file_name_body_pre to prepend a string to the file namemime_fileinfo, mime_file, mime_magic and mime_getimagesize so that it is possible to deactivate some MIME type checking methodexec() rather than shell_exec(), to play better with safe modeprocessed wasn't propagated properlyv 0.27 14/05/2009, (*46)
file_auto_rename if file_overwrite is setimage_precrop to crop the image before an eventual resizingv 0.26 13/11/2008, (*47)
file() command, MIME magic, and getimagesize(), in that orderv 0.25 17/11/2007, (*48)
forbidden to set an array of forbidden MIME typesallowed and forbidden, such as image/*file_is_image to determine if the file is a supported image typeprocess(). Available are image_src_x, image_src_y and the newly introduced image_src_bits, image_src_pixels and image_src_type. Note that this will not work if open_basedir restrictions are in placeimage_max_width, image_max_height, image_max_pixels, image_max_ratio, image_min_width, image_min_height, image_min_pixels and image_min_ratio
image_ratio_pixels to resize an image to a number of pixels, keeping aspect ratioimage_is_palette and image_is_transparent and image_transparent_color for GIF imagesimage_default_color to define a fallback color for non alpha-transparent output formats, such as JPEG or BMPimage_background_color, which now forces transparent areas to be paintedimage_reflection_color is now deprecated in favour of image_default_color
preserve_transparency is deprecatedimage_default_color
image_default_color
process() if the function is called with an empty or null argument, or no argumentv 0.24 25/05/2007, (*49)
image_background_color, to set the default background color of an imageimage_ratio_fill in order to fit an image within some dimensions and color the remaining space. Very similar to image_ratio_crop
v 0.23 23/12/2006, (*50)
process(). This file will be used for subsequent processes, and will be deleted upon calling clean()
v 0.22 16/12/2006, (*51)
image_ratio_crop so it can accept one or more from string 'TBLR', determining which side of the image is kept while croppingimage_text_line_spacing which allow to set the space between several lines of textimage_text_alignment which allow to set the alignment when text has several linesimage_text_font can now be set to the path of a GDF font to load external fontsimage_reflection_height to create a reflection of the source image, which height is in pixels or percentageimage_reflection_space to set the space in pixels between the source image and the reflectionimage_reflection_color to set the reflection background colorimage_reflection_opacity to set the initial level of opacity of the reflectionv 0.21 30/09/2006, (*52)
image_ratio_crop which resizes within image_x and image_y, keeping ratio, but filling the space by cropping excedent of imagemime_check, which default is true, to set checks against allowed MIME listimage_text_color, and related text transparency bug fixedgd_version() now uses gd_info(), or else phpinfo()
v 0.20 11/08/2006, (*53)
image_brightness and image_contrast to be between -127 and 127dir_auto_create to automatically and recursively create destination directory if missing.dir_auto_chmod to automatically chmod the destination directory if not writeable.dir_chmod to set the default chmod to use.image_crop to crop imagesimage_negative to invert the colors on the imageimage_greyscale to turn the image into greyscaleimage_threshold to apply a threshold filter on the imageimage_bevel, image_bevel_color1 and image_bevel_color2 to add a bevel borderimage_border and image_border_color to add a single color borderimage_frame and image_frame_colors to add a multicolored framev 0.19 29/03/2006, (*54)
file_safe_name has been improved a bit.image_brightness, image_contrast, image_tint_color, image_overlay_color and image_overlay_percent to do color manipulation on the images.image_text and all derivated settings to add a text label on the image.image_watermark and all derivated settings to add a watermark image on the image.image_flip and image_rotate for more image manipulationsjpeg_size to calculate the JPG compression quality in order to fit within one filesize.v 0.18 02/02/2006, (*55)
no_script to turn dangerous scripts into text files.mime_magic_check to set the class to use mime_magic.preserve_transparency experimental. Thanks Gregor.image_convert.file_max_size to upload_max_filesize from php.ini. Thanks Edwardv 0.17 28/05/2005, (*56)
v 0.16 19/05/2005, (*57)
file_auto_rename automatic file renaming if the same filename already exists.file_safe_name safe formatting of the filename (spaces to underscores so far).v 0.15 16/04/2005, (*58)
v 0.14 14/03/2005, (*59)
v 0.13 07/03/2005, (*60)
image_ratio. Thanks Justin.image_ratio_no_zoom_in and image_ratio_no_zoom_out
v 0.12 21/01/2005, (*61)
image_ratio to resize within max values, keeping image ratiov 0.11 22/08/2003, (*62)
imageresized() into imagecopyresampled() and imagecreate() into imagecreatetruecolor())