When you create a new instance of ImgPicker
you can pass some options:
<?php
$options = array(
'upload_dir' => __DIR__ . '/../files/',
'max_file_size' => 3000000
)
new ImgPicker($options);
Upload directory path
- Type: string
- Default:
server/files/
Upload directory url
- Type: string
- Default:
full url to server/files/
Accepted file types.
- Type: string
- Default:
png|jpg|jpeg|gif
-
Directory mode.
-
Type: integer
-
Default:
0777
File max size restriction in bytes (e.g. 5000000 = 5MB).
- Type: integer
- Default:
null
File min size restriction in bytes (e.g. 1 = 1B).
- Type: integer
- Default:
1
Image max width restriction (e.g. 100 = 100 pixels).
- Type: integer
- Default:
null
Image max height restriction (e.g. 100 = 100 pixels).
- Type: integer
- Default:
null
Image min width restriction (e.g. 100 = 100 pixels).
- Type: integer
- Default:
1
Image min height restriction (e.g. 100 = 100 pixels).
- Type: integer
- Default:
1
Image versions. Array with image versions to be created. Each element is another array which has can have other options.
- Type: array
- Default:
n/a
'versions' => array(
// This will create 2 image versions: the original one and a 200x200 one
'avatar' => array(
// Other upload directory path (Default: main upload_dir)
'upload_dir' => __DIR__ . '/../files/avatar/',
// Other upload directory url (Default: main upload_url)
'upload_url' => 'files/avatar/',
// Create square image (Default: false)
'crop' => true,
// Max width (Default: n/a)
'max_width' => 200,
// Max height (Default: n/a)
'max_height' => 200
),
)
If you want to have only one image create a version with an empty name:
'versions' => array(
// This will create only one image
'' => array(
// The same options as above can be used here
),
)
The callbacks are functions that will be called once an action is completed. In all of these callbacks you can retrive data sent from the Client Side using $_POST['data']`` or
$_GET['data']`` for the load callback. To send data from the Client Side check out Options > data
Called when autoloading image.
/**
* @return string|array
*/
'load' => function () {
// You can make a query from your database and return the image:
// return '123.jpg'; | return array('1.jpg', '2.jpg');
},
Called when trying to delete image.
/**
* @param stdClass $filename
* @return boolean
*/
'delete' => function ($filename) {
// Wether to allow file deletion
return true;
},
Called before the upload starts.
/**
* @param stdClass $image
* @return void
*/
'upload_start' => function ($image) {
},
Called after upload.
/**
* @param stdClass $image
* @return void
*/
'upload_complete' => function ($image) {
},
Called before the crop starts.
/**
* @param stdClass $image
* @return void
*/
'crop_start' => function ($image) {
},
Called after cropping.
/**
* @param stdClass $image
* @return void
*/
'crop_complete' => function ($image) {
},
In all callbacks the $image
parameter is an stdClass
object and has the following proprieties:
name
stringtype
stringsize
integer (available only in upload_start)path
stringurl
stringwidth
integerheight
integerversions
array (available only in upload_complete and crop_complete)
You can access them like this: $image->name
, $image->path
.
If you wish to change the messages, pass an array with the messages:
<?php
$options = array();
$messages = array(
1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini',
2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
3 => 'The uploaded file was only partially uploaded',
4 => 'No file was uploaded',
6 => 'Missing a temporary folder',
7 => 'Failed to write file to disk',
8 => 'A PHP extension stopped the file upload',
'gd' => 'PHP GD library is NOT installed on your web server',
'post_max_size' => 'The uploaded file exceeds the post_max_size directive in php.ini',
'max_file_size' => 'File is too big',
'min_file_size' => 'File is too small',
'accept_file_types' => 'Filetype not allowed',
'max_width' => 'Image exceeds maximum width of ',
'min_width' => 'Image requires a minimum width of ',
'max_height' => 'Image exceeds maximum height of ',
'min_height' => 'Image requires a minimum height of ',
'upload_failed' => 'Failed to upload the file',
'move_failed' => 'Failed to upload the file',
'invalid_image' => 'Invalid image',
'image_resize' => 'Failed to resize image',
'not_exists' => 'Failed to load the image'
);
new ImgPicker($options, $messages);