| | 5 | * This behaviour is based on Chris Partridge's upload behaviour (http://bin.cakephp.org/saved/17539) |
|---|
| | 6 | * @author Tane Piper (digitalspaghetti@gmail.com) |
|---|
| | 7 | * @link http://www.digitalspaghetti.me.uk |
|---|
| | 8 | * @filesource http://bakery.cakephp.org/articles/view/improved-upload-behaviour-with-thumbnails-and-name-correction |
|---|
| | 9 | * @version 1.1 |
|---|
| | 10 | * @modifiedby $LastChangedBy:$ |
|---|
| | 11 | * @lastmodified $Date:$ |
|---|
| | 12 | * @svn $Id:$ |
|---|
| | 13 | * |
|---|
| | 14 | * Useage: |
|---|
| | 15 | * 1) Download this behaviour and place it in your models/behaviours/upload.php |
|---|
| | 16 | * 2) If you require thumbnails for image generation, download Nate's phpThumb Component (http://bakery.cakephp.org/articles/view/phpthumb-component) |
|---|
| | 17 | * 3) Insert the following SQL into your database. This is a basic model you can expand on: |
|---|
| | 18 | * CREATE TABLE `images` ( |
|---|
| | 19 | * `id` int(8) unsigned NOT NULL auto_increment, |
|---|
| | 20 | * `filename` varchar(255) default NULL, |
|---|
| | 21 | * `dir` varchar(255) default NULL, |
|---|
| | 22 | * `mimetype` varchar(255) NULL, |
|---|
| | 23 | * `filesize` int(11) unsigned default NULL, |
|---|
| | 24 | * `created` datetime default NULL, |
|---|
| | 25 | * `modified` datetime default NULL, |
|---|
| | 26 | * PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; |
|---|
| | 27 | * 4) In your model that you want to have the upload behavior work, place the below code. This example is for an Image model: |
|---|
| | 28 | * |
|---|
| | 29 | * var $actsAs = array('Upload' => array( |
|---|
| | 30 | * 'filename' => array( |
|---|
| | 31 | * 'dir' => 'files/images', |
|---|
| | 32 | * 'overwrite_existing' => false, |
|---|
| | 33 | * 'create_directory' => false, |
|---|
| | 34 | * 'allowed_mime' => array('image/jpeg', 'image/pjpeg', 'image/gif', 'image/png'), |
|---|
| | 35 | * 'allowed_ext' => array('.jpg', '.jpeg', '.png', '.gif'), |
|---|
| | 36 | * 'thumbsizes' => array( |
|---|
| | 37 | * 'small' => array('width'=>100, 'height'=>100), |
|---|
| | 38 | * 'medium'=> array('width'=>220, 'height'=>220), |
|---|
| | 39 | * 'large' => array('width'=>800, 'height'=>600) |
|---|
| | 40 | * ) |
|---|
| | 41 | * ) |
|---|
| | 42 | * ) |
|---|
| | 43 | * ); |
|---|
| | 44 | * The above code will save the uploaded file's name in the 'filename' field in database, |
|---|
| | 45 | * it will not overwrite existing files, instead it will create a random filename if |
|---|
| | 46 | * it already exists. |
|---|
| | 47 | * Allowed Mimetypes and extentions should be pretty explanitory |
|---|
| | 48 | * For thumbnails, when the file is uploaded, it will create 3 thumbnail sizes and prepend the name |
|---|
| | 49 | * to the thumbfiles (i.e. image_001.jpg will produced thumb.small.image_001.jpg, thumb.medium.image_001.jpg, etc) |
|---|
| | 50 | * |
|---|
| | 51 | * 5) Create your upload view, make sure it's a multipart/form-data form, and the filename field is of type $form->file |
|---|
| | 52 | * 6) Make sure your directory is at least CHMOD 775, also check your php.ini MAX_UPLOAD_SIZE is enough to support the filesizes you are uploading |
|---|
| | 53 | * |
|---|
| | 54 | * Version Details |
|---|
| | 55 | * |
|---|
| | 56 | * 1.1 |
|---|
| | 57 | * + Improved Image scaling code |
|---|
| | 58 | * + Fixed check to see if file exists and rename file to unique name |
|---|
| | 59 | * + Improved model actsAs to allow more thumbnail sizes |
|---|
| | 60 | * |
|---|
| | 61 | * 1.0 |
|---|
| | 62 | * + Initial release with thumbnail code. |
|---|