Sufyan

Photo Upload CakePHP 3

Mar 6th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.73 KB | None | 0 0
  1. //Imagine Library Composer
  2. composer require imagine/imagine
  3.  
  4. //Recipes Table Add Fields
  5. photo(varchar), photo_dir(varchar), photo_type(varchar), photo_size (int)
  6. //Add.ctp
  7.       <?= $this->Form->create('Recipes', ['role'=>'form','type' => 'file']); ?>
  8.             <div class="col-12">
  9.               <div class="row d-flex justify-content-center">
  10.                 <div class="col-md-6 col-12 d-flex justify-content-center">
  11.                   <img class="img-circle elevation-2 user-profile-picture" id="uploader_image" src="https://placehold.it/128x128" alt="User Avatar">
  12.                 </div>
  13.                 <div class="col-md-6 col-12 d-flex justify-content-center align-items-center">
  14.                   <?php echo $this->Form->control('photo', ['type' => 'file','label'=>false,'id' => 'uploader']); ?>
  15.                 </div>
  16.               </div>
  17.             </div>
  18.  
  19. //Edit.ctp
  20.       <?= $this->Form->create('Recipes', ['role'=>'form','type' => 'file']); ?>
  21.  
  22.             <div class="col-12">
  23.               <div class="row d-flex justify-content-center">
  24.                 <div class="col-md-6 col-12 d-flex justify-content-center">
  25.                   <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">
  26.                 </div>
  27.                 <div class="col-md-6 col-12 d-flex justify-content-center align-items-center">
  28.                   <?php echo $this->Form->control('photo', ['type' => 'file','label'=>false,'id' => 'uploader']); ?>
  29.                 </div>
  30.               </div>
  31.             </div>
  32.  
  33. //RecipesTable.php
  34. initializeMethod()
  35.  
  36.       /**
  37.        * Upload Plugin Config
  38.        */
  39.       $this->addBehavior('Josegonzalez/Upload.Upload', [
  40.         // You can configure as many upload fields as possible,
  41.         // where the pattern is `field` => `config`
  42.         //
  43.         // Keep in mind that while this plugin does not have any limits in terms of
  44.         // number of files uploaded per request, you should keep this down in order
  45.         // to decrease the ability of your users to block other requests.
  46.         'photo' => [
  47.           // 'path' => 'webroot{DS}files{DS}{model}{DS}{field}{DS}{field-value:id}{DS}',
  48.           'fields' => [
  49.             'dir' => 'photo_dir',
  50.             'size' => 'photo_size',
  51.             'type' => 'photo_type'
  52.           ],
  53.           'nameCallback' => function ($table, $entity, $data, $field, $settings) {
  54.             return strtolower($data['name']); //Prefix Random Number in ImageName to avoid duplications
  55.           },
  56.           'transformer' =>  function ($table, $entity, $data, $field, $settings) {
  57.             $extension = pathinfo($data['name'], PATHINFO_EXTENSION);
  58.  
  59.             // Store the thumbnail in a temporary file
  60.             $tm_tmp = tempnam(sys_get_temp_dir(), 'upload') . '.' . $extension;
  61.             $sm_tmp = tempnam(sys_get_temp_dir(), 'upload') . '.' . $extension;
  62.             $md_tmp = tempnam(sys_get_temp_dir(), 'upload') . '.' . $extension;
  63.  
  64.             // Use the Imagine library to DO THE THING
  65.             $tm_size = new \Imagine\Image\Box(100, 100);
  66.             $sm_size = new \Imagine\Image\Box(320, 320);
  67.             $md_size = new \Imagine\Image\Box(600, 600);
  68.  
  69.             $mode = \Imagine\Image\ImageInterface::THUMBNAIL_INSET;
  70.             $imagine = new \Imagine\Gd\Imagine();
  71.  
  72.             // Save that modified file to our temp file
  73.             //Thumbnail
  74.             $imagine->open($data['tmp_name'])
  75.                 ->thumbnail($tm_size, $mode)
  76.                 ->save($tm_tmp);
  77.             //Small
  78.             $imagine->open($data['tmp_name'])
  79.                 ->thumbnail($sm_size, $mode)
  80.                 ->save($sm_tmp);
  81.  
  82.             //Medium
  83.             $imagine->open($data['tmp_name'])
  84.                 ->thumbnail($md_size, $mode)
  85.                 ->save($md_tmp);
  86.  
  87.  
  88.             // Now return the original *and* the thumbnail
  89.             return [
  90.                 $data['tmp_name'] => $data['name'],
  91.                 $tm_tmp => 'tm_' . $data['name'],
  92.                 $sm_tmp => 'sm_' . $data['name'],
  93.                 $md_tmp => 'md_' . $data['name']
  94.             ];
  95.           },
  96.           'deleteCallback' => function ($path, $entity, $field, $settings) {
  97.               // When deleting the entity, both the original and the thumbnail will be removed
  98.               // when keepFilesOnDelete is set to false
  99.               return [
  100.                   $path . $entity->{$field},
  101.                   $path . 'tm_' . $entity->{$field},
  102.                   $path . 'sm_' . $entity->{$field},
  103.                   $path . 'md_' . $entity->{$field}
  104.               ];
  105.           },
  106.           'keepFilesOnDelete' => false
  107.         ]
  108.       ]);
Add Comment
Please, Sign In to add comment