Skip to content

huoxito/image_upload

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Image Upload Plugin For CakePHP

One more option for dealing with image uploads in CakePHP in a simple way.

The plugin has a behavior that lets you easily upload and generate as many images as you need. It also comes with a helper class to make rendering theses images pretty easy. The upload itself is processed using this class http://www.verot.net/php_class_upload.htm

User for CakePHP 2.x. Use branch 1.3 for CakePHP 1.3.x versions.

How to use

Download or clone the plugin into your app/plugins directory.

Heres the structure of the Upload behavior config that should be added on your model. On the configs key you can set whether you want to resize, crop or just adjust image size proportionally. On the fields key you can set as many fields as you want. The first two params are the main image width and height respectively. After that you can set as many thumb width and height as you need.

var $actsAs = array(
    'ImageUpload.Upload' => array(
        'configs' => array(
            'image_resize' => true,
            'image_ratio' => false,
            'image_ratio_crop' => true
        ),
        'fields' => array(
            'field_name' => array(
                300, 300, 50, 50, 30, 30
            )
        )
    )
);

The following example would upload the image, crop it to 500 x 500 and generate two thumbs, 100x50 and 30x50.

var $actsAs = array(
    'ImageUpload.Upload' => array(
         'fields' => array(
             'profile' => array(
                500,500, 100,50, 30,50
             ),
         )
    )
);

When a register is deleted all files related to that register are removed as well. To overwrite a file just upload a new image on an edit form, for example.

To use the Image helper just add it to the helpers var on the controller.

var $helpers = array('ImageUpload.Image');

It comes with two methods so far one for uploading the main image and other for thumbs. In case you set any on the Behaviors config.

To render the main image:

<?= $this->Image->show($field_value, $controller); ?>

Thumbs are enumerated. So it takes a $number parameter to render the exact thumb. To render the first thumb of the above example you should pass 0. 1 would render the second thumb and so on.

<?= $this->Image->thumb($field_value, $controller, $number); ?>

Troubleshooting

The plugin still doesn't show much helpful messages so far if any errors occurs. Below are two tips to avoid errors.

* Make sure to set write permissions to webroot/files/

* Don't forget to set you form correctly:

    <?php echo $this->Form->create('Model', array('type  => 'file')); ?>

About

Image Upload Plugin for CakePHP

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages