Skip to content

Commit 188ac2a

Browse files
committed
Version 1.3.
1 parent b0f870e commit 188ac2a

12 files changed

+186
-125
lines changed

autoload.md

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ First we start with the PHP:
1111
session_start();
1212

1313
// Let's say that you grab the user id from the session
14-
$user_id = $_SESSION['user_id'];
14+
$userId = $_SESSION['user_id'];
1515

1616
// Include ImgPicker.php class
17-
require dirname(__FILE__) . '/ImgPicker.php';
17+
require __DIR__ . '/ImgPicker.php';
1818

1919
// ImgPicker options
2020
$options = array(
2121
// Upload directory path
22-
'upload_dir' => dirname(__FILE__) . '/../files/',
22+
'upload_dir' => __DIR__ . '/../files/',
2323
// Upload directory url
2424
'upload_url' => 'files/',
2525
// Image versions
@@ -32,33 +32,28 @@ $options = array(
3232
)
3333
),
3434
// !!! Here you have to return the user image
35-
'load' => function($instance) {
36-
global $user_id;
35+
'load' => function () use ($userId) {
3736
// Make a query from database and return the filename:
3837
return '123.png';
3938
},
40-
4139
// Upload start callback
42-
'upload_start' => function($image, $instance) {
43-
global $user_id;
40+
'upload_start' => function ($image) use ($userId) {
4441
// Name the temp image as $user_id
4542
$image->name = '~'.$user_id.'.'.$image->type;
4643
},
4744
// Crop start callback
48-
'crop_start' => function($image, $instance) {
49-
global $user_id;
45+
'crop_start' => function ($image) use ($userId) {
5046
// Change the name of the image
5147
$image->name = $user_id.'.'.$image->type;
5248
},
5349
// Crop complete callback
54-
'crop_complete' => function($image, $instance) {
50+
'crop_complete' => function ($image) use ($userId) {
5551
// Save the image to database
5652
}
5753
);
5854

5955
// Create ImgPicker instance
6056
new ImgPicker($options);
61-
?>
6257
```
6358

6459
And the JavaScript code:

configuration.md

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
- [Options](#options)
44
- [Callback Options](#callback-options)
5+
- [Messages](#messages)
56

67
When you create a new instance of `ImgPicker` you can pass some options:
78

89
```php
910
<?php
1011
$options = array(
11-
'upload_dir' => dirname(__FILE__) . '/../files/',
12+
'upload_dir' => __DIR__ . '/../files/',
1213
'max_file_size' => 3000000
1314
)
1415
new ImgPicker($options);
15-
?>
1616
```
1717

1818
## Options
@@ -99,9 +99,9 @@ Image versions. Array with image versions to be created. Each element is another
9999
// This will create 2 image versions: the original one and a 200x200 one
100100
'avatar' => array(
101101
// Other upload directory path (Default: main upload_dir)
102-
'upload_dir' => dirname(__FILE__) . '/../files/avatar/',
102+
'upload_dir' => __DIR__ . '/../files/avatar/',
103103
// Other upload directory url (Default: main upload_url)
104-
'upload_url' => 'http://localhost/imgPicker/files/avatar/',
104+
'upload_url' => 'files/avatar/',
105105
// Create square image (Default: false)
106106
'crop' => true,
107107
// Max width (Default: n/a)
@@ -133,10 +133,9 @@ Called when autoloading image.
133133

134134
```php
135135
/**
136-
* @param ImgPicker $instance
137136
* @return string|array
138137
*/
139-
'load' => function($instance) {
138+
'load' => function () {
140139
// You can make a query from your database and return the image:
141140
// return '123.jpg'; | return array('1.jpg', '2.jpg');
142141
},
@@ -148,11 +147,10 @@ Called when trying to delete image.
148147

149148
```php
150149
/**
151-
* @param stdClass $filename
152-
* @param ImgPicker $instance
150+
* @param stdClass $filename
153151
* @return boolean
154152
*/
155-
'delete' => function($filename, $instance) {
153+
'delete' => function ($filename) {
156154
// Wether to allow file deletion
157155
return true;
158156
},
@@ -164,11 +162,10 @@ Called before the upload starts.
164162

165163
```php
166164
/**
167-
* @param stdClass $image
168-
* @param ImgPicker $instance
165+
* @param stdClass $image
169166
* @return void
170167
*/
171-
'upload_start' => function($image, $instance) {
168+
'upload_start' => function ($image) {
172169
},
173170
```
174171

@@ -178,11 +175,10 @@ Called after upload.
178175

179176
```php
180177
/**
181-
* @param stdClass $image
182-
* @param ImgPicker $instance
178+
* @param stdClass $image
183179
* @return void
184180
*/
185-
'upload_complete' => function($image, $instance) {
181+
'upload_complete' => function ($image) {
186182
},
187183
```
188184

@@ -192,11 +188,10 @@ Called before the crop starts.
192188

193189
```php
194190
/**
195-
* @param stdClass $image
196-
* @param ImgPicker $instance
191+
* @param stdClass $image
197192
* @return void
198193
*/
199-
'crop_start' => function($image, $instance) {
194+
'crop_start' => function ($image) {
200195
},
201196
```
202197

@@ -206,11 +201,10 @@ Called after cropping.
206201

207202
```php
208203
/**
209-
* @param stdClass $image
210-
* @param ImgPicker $instance
204+
* @param stdClass $image
211205
* @return void
212206
*/
213-
'crop_complete' => function($image, $instance) {
207+
'crop_complete' => function ($image) {
214208
},
215209
```
216210

@@ -226,3 +220,38 @@ In all callbacks the `$image` parameter is an `stdClass` object and has the foll
226220
- `versions` _array_ (available only in _upload_complete_ and _crop_complete_)
227221

228222
You can access them like this: `$image->name`, `$image->path`.
223+
224+
### Messages
225+
226+
If you wish to change the messages, pass an array with the messages:
227+
228+
```php
229+
<?php
230+
231+
$options = array();
232+
233+
$messages = array(
234+
1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini',
235+
2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
236+
3 => 'The uploaded file was only partially uploaded',
237+
4 => 'No file was uploaded',
238+
6 => 'Missing a temporary folder',
239+
7 => 'Failed to write file to disk',
240+
8 => 'A PHP extension stopped the file upload',
241+
'gd' => 'PHP GD library is NOT installed on your web server',
242+
'post_max_size' => 'The uploaded file exceeds the post_max_size directive in php.ini',
243+
'max_file_size' => 'File is too big',
244+
'min_file_size' => 'File is too small',
245+
'accept_file_types' => 'Filetype not allowed',
246+
'max_width' => 'Image exceeds maximum width of ',
247+
'min_width' => 'Image requires a minimum width of ',
248+
'max_height' => 'Image exceeds maximum height of ',
249+
'min_height' => 'Image requires a minimum height of ',
250+
'upload_failed' => 'Failed to upload the file',
251+
'move_failed' => 'Failed to upload the file',
252+
'invalid_image' => 'Invalid image',
253+
'image_resize' => 'Failed to resize image',
254+
'not_exists' => 'Failed to load the image'
255+
);
256+
new ImgPicker($options, $messages);
257+
```

database.md

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ This example will show to save and load an image from database using the some fu
44

55
Before continuing with this example make sure you have read the [Modal & Inline](modal-inline.md) example.
66

7-
For this we need some functions to connect to database, insert, update and select. The script comes with a Database class that can be found in `server/Database`.
7+
For this we need some functions to connect to database, insert, update and select. The script comes with a Database class that can be found in `server/DB`.
88

9-
First you need to edit `server/Database/config.php` and set you database connection details.
9+
First you need to edit `server/DB/config.php` and set you database connection details.
1010
You also need a table where you insert the images (or you can use your own table), but for this example I'll use the included SQL table: `example_images.sql`.
1111

1212
Now in `upload_avatar.php` include the Database class and use it as shown below:
@@ -16,19 +16,21 @@ Now in `upload_avatar.php` include the Database class and use it as shown below:
1616
<?php
1717
session_start();
1818

19-
// Let's say that you grab the user id from the session
20-
$user_id = $_SESSION['user_id'];
19+
// Include Database classs
20+
require __DIR__.'/DB/Database.php';
2121

22-
// Include ImgPicker.php class
23-
require dirname(__FILE__) . '/ImgPicker.php';
22+
Database::connect(require __DIR__.'/DB/config.php');
2423

25-
// Include Database classs
26-
require dirname(__FILE__) . '/Database/Database.php';
24+
// Include ImgPicker class
25+
require __DIR__ . '/ImgPicker.php';
26+
27+
// Let's say that you grab the user id from the session
28+
$userId = $_SESSION['user_id'];
2729

2830
// ImgPicker options
2931
$options = array(
3032
// Upload directory path
31-
'upload_dir' => dirname(__FILE__) . '/../files/',
33+
'upload_dir' => __DIR__ . '/../files/',
3234
// Upload directory url
3335
'upload_url' => 'files/',
3436
// Image versions
@@ -40,60 +42,57 @@ $options = array(
4042
'max_height' => 200
4143
)
4244
),
43-
'load' => function($instance) {
44-
global $user_id;
45+
'load' => function () use ($userId) {
4546
// Select the image for the current user
4647
$db = new Database;
4748
$results = $db->table('example_images')
48-
->where('user_id', $user_id)
49+
->where('user_id', $userId)
4950
->limit(1)
5051
->get();
51-
if ($results)
52+
53+
if ($results) {
5254
return $results[0]->image;
53-
else
55+
} else {
5456
return false;
57+
}
5558
},
56-
5759
// Upload start callback
58-
'upload_start' => function($image, $instance) {
59-
global $user_id;
60-
// Name the temp image as $user_id
61-
$image->name = '~'.$user_id.'.'.$image->type;
60+
'upload_start' => function ($image) use ($userId) {
61+
// Name the temp image as $userId
62+
$image->name = '~'.$userId.'.'.$image->type;
6263
},
6364
// Crop start callback
64-
'crop_start' => function($image, $instance) {
65-
global $user_id;
65+
'crop_start' => function ($image) use ($userId) {
6666
// Change the name of the image
67-
$image->name = $user_id.'.'.$image->type;
67+
$image->name = $userId.'.'.$image->type;
6868
},
6969
// Crop complete callback
70-
'crop_complete' => function($image, $instance) {
71-
global $user_id;
70+
'crop_complete' => function ($image) use ($userId) {
7271
// Save the image to database
7372
$data = array(
74-
'user_id' => $user_id,
73+
'user_id' => $userId,
7574
'image' => $image->name
7675
);
7776

7877
$db = new Database;
7978
// First check if the image exists
8079
$results = $db->table('example_images')
81-
->where('user_id', $user_id)
80+
->where('user_id', $userId)
8281
->limit(1)
8382
->get();
8483

8584
// If exists update, otherwise insert
86-
if ($results)
85+
if ($results) {
8786
$db->table('example_images')
88-
->where('user_id', $user_id)
87+
->where('user_id', $userId)
8988
->limit(1)
9089
->update($data);
91-
else
90+
} else {
9291
$db->table('example_images')->insert($data);
92+
}
9393
}
9494
);
9595

9696
// Create ImgPicker instance
9797
new ImgPicker($options);
98-
?>
9998
```

installation.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ This is how the PHP class options look like:
3232

3333
```php
3434
$options = array(
35-
'upload_dir' => dirname(__FILE__) . '/../files/',
36-
'upload_url' => 'http://localhost/imgPicker/files/',
35+
'upload_dir' => __DIR__ . '/../files/',
36+
'upload_url' => 'files/',
3737
'accept_file_types' => 'png|jpg|jpeg|gif',
3838
'versions' => array(
3939
'avatar' => array(
@@ -61,4 +61,6 @@ Image Picker works on all major browsers (including mobile) with some exceptions
6161
- Safari 5+
6262
- IE8+
6363

64+
Starting with version 47, Chrome requires a secure connection (SSL) in order to use the Webcam. Read more [here](https://sites.google.com/a/chromium.org/dev/Home/chromium-security/deprecating-powerful-features-on-insecure-origins).
65+
6466
In Safari 5, IE8 and IE9 only the upload works due to no [XHR](http://caniuse.com/#search=XMLHttpRequest) support.

modal-inline.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ The fist option is to have the ImagePicker as a modal. For this you have to defi
2222
2323
2424
<!-- Image -->
25-
<img src="assets/img/default-avatar.png" id="avatar" width="200" height="200">
25+
<img src="http://www.gravatar.com/avatar/0?d=mm&s=150" id="avatar" width="200" height="200">
2626
2727
<!-- Button trigger modal -->
2828
<button type="button" data-ip-modal="#myModal">Launch modal</button>
@@ -82,7 +82,7 @@ $(function() {
8282
aspectRatio: 1,
8383
deleteComplete: function() {
8484
// Restore default avatar
85-
$('#avatar').attr('src', 'assets/img/default-avatar.png');
85+
$('#avatar').attr('src', 'http://www.gravatar.com/avatar/0?d=mm&s=150');
8686
// Hide modal
8787
this.modal('hide');
8888
},
@@ -146,7 +146,7 @@ $(function() {
146146
url: 'server/upload_avatar.php',
147147
aspectRatio: 1,
148148
deleteComplete: function() {
149-
$('#avatar').attr('src', 'assets/img/default-avatar.png');
149+
$('#avatar').attr('src', 'http://www.gravatar.com/avatar/0?d=mm&s=150');
150150
this.modal('hide');
151151
},
152152
cropSuccess: function(image) {

0 commit comments

Comments
 (0)