Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Imagine Library Composer
- composer require imagine/imagine
- //Recipes Table Add Fields
- photo(varchar), photo_dir(varchar), photo_type(varchar), photo_size (int)
- //Add.ctp
- <?= $this->Form->create('Recipes', ['role'=>'form','type' => 'file']); ?>
- <div class="col-12">
- <div class="row d-flex justify-content-center">
- <div class="col-md-6 col-12 d-flex justify-content-center">
- <img class="img-circle elevation-2 user-profile-picture" id="uploader_image" src="https://placehold.it/128x128" alt="User Avatar">
- </div>
- <div class="col-md-6 col-12 d-flex justify-content-center align-items-center">
- <?php echo $this->Form->control('photo', ['type' => 'file','label'=>false,'id' => 'uploader']); ?>
- </div>
- </div>
- </div>
- //Edit.ctp
- <?= $this->Form->create('Recipes', ['role'=>'form','type' => 'file']); ?>
- <div class="col-12">
- <div class="row d-flex justify-content-center">
- <div class="col-md-6 col-12 d-flex justify-content-center">
- <img class="img-circle elevation-2 user-profile-picture" id="uploader_image" src="<?= $base_image_url ?><?= $data['photo_dir'] ?>tm_<?= $data['photo'] ?>" alt="User Avatar">
- </div>
- <div class="col-md-6 col-12 d-flex justify-content-center align-items-center">
- <?php echo $this->Form->control('photo', ['type' => 'file','label'=>false,'id' => 'uploader']); ?>
- </div>
- </div>
- </div>
- //RecipesTable.php
- initializeMethod()
- /**
- * Upload Plugin Config
- */
- $this->addBehavior('Josegonzalez/Upload.Upload', [
- // You can configure as many upload fields as possible,
- // where the pattern is `field` => `config`
- //
- // Keep in mind that while this plugin does not have any limits in terms of
- // number of files uploaded per request, you should keep this down in order
- // to decrease the ability of your users to block other requests.
- 'photo' => [
- // 'path' => 'webroot{DS}files{DS}{model}{DS}{field}{DS}{field-value:id}{DS}',
- 'fields' => [
- 'dir' => 'photo_dir',
- 'size' => 'photo_size',
- 'type' => 'photo_type'
- ],
- 'nameCallback' => function ($table, $entity, $data, $field, $settings) {
- return strtolower($data['name']); //Prefix Random Number in ImageName to avoid duplications
- },
- 'transformer' => function ($table, $entity, $data, $field, $settings) {
- $extension = pathinfo($data['name'], PATHINFO_EXTENSION);
- // Store the thumbnail in a temporary file
- $tm_tmp = tempnam(sys_get_temp_dir(), 'upload') . '.' . $extension;
- $sm_tmp = tempnam(sys_get_temp_dir(), 'upload') . '.' . $extension;
- $md_tmp = tempnam(sys_get_temp_dir(), 'upload') . '.' . $extension;
- // Use the Imagine library to DO THE THING
- $tm_size = new \Imagine\Image\Box(100, 100);
- $sm_size = new \Imagine\Image\Box(320, 320);
- $md_size = new \Imagine\Image\Box(600, 600);
- $mode = \Imagine\Image\ImageInterface::THUMBNAIL_INSET;
- $imagine = new \Imagine\Gd\Imagine();
- // Save that modified file to our temp file
- //Thumbnail
- $imagine->open($data['tmp_name'])
- ->thumbnail($tm_size, $mode)
- ->save($tm_tmp);
- //Small
- $imagine->open($data['tmp_name'])
- ->thumbnail($sm_size, $mode)
- ->save($sm_tmp);
- //Medium
- $imagine->open($data['tmp_name'])
- ->thumbnail($md_size, $mode)
- ->save($md_tmp);
- // Now return the original *and* the thumbnail
- return [
- $data['tmp_name'] => $data['name'],
- $tm_tmp => 'tm_' . $data['name'],
- $sm_tmp => 'sm_' . $data['name'],
- $md_tmp => 'md_' . $data['name']
- ];
- },
- 'deleteCallback' => function ($path, $entity, $field, $settings) {
- // When deleting the entity, both the original and the thumbnail will be removed
- // when keepFilesOnDelete is set to false
- return [
- $path . $entity->{$field},
- $path . 'tm_' . $entity->{$field},
- $path . 'sm_' . $entity->{$field},
- $path . 'md_' . $entity->{$field}
- ];
- },
- 'keepFilesOnDelete' => false
- ]
- ]);
Add Comment
Please, Sign In to add comment