0

How do I detect if new images have been uploaded to a gallery from the backend form, so I could manipulate them before saving.

I've tried the following, but it didn't work:

<?php namespace Author\Plugin\Models;

use Model;

class ModelName extends Model
{
    public $attachMany = [
        'gallery' => 'System\Models\File',
    ];

    public function beforeSave()
    {
        if (Input::hasFile('gallery')) {
            trace_log('new files');
        } else {
            trace_log('no new files');
        }
    }
}

-- it keeps giving me no new files message, regardless whether I upload new files or not.

qwaz
  • 1,285
  • 4
  • 23
  • 47
  • do you want it globally or just specific to your model – Hardik Satasiya Sep 21 '20 at 08:15
  • I guess specific to this model. But I'd consider both ways. – qwaz Sep 21 '20 at 08:28
  • @HardikSatasiya do you have a solution? Or at least something to point me in the right direction? What I'm trying to accomplish is that whenever new images are uploaded to the gallery, I want to resize them, so they'd not take up unnecessary amount of space, since they're quite large (8-16mb), and I can't have a user resize them manually before uploading. I'm aware of you other answer about resizing, but it doesn't deal with detecting new images that are being uploaded that I'd need to resize. – qwaz Sep 21 '20 at 08:59
  • i added answer please check it if it works or not, make sure to replace values for your model name and attribute name etc.. – Hardik Satasiya Sep 21 '20 at 09:41

1 Answers1

1

You can use this code to resize image of your model

Its little tricky as it is using differed binding so.

you can use this code in your plugin's plugin.php's boot method

use October\Rain\Database\Attach\Resizer;
// .. other code ...

public function boot() {

  \Hardik\SoTest\Models\Item::extend(function($model) {
    // for create time only
      $model->bindEvent('model.beforeCreate', function() use ($model) {

        $records = \October\Rain\Database\Models\DeferredBinding::where([
          'master_type' => 'Hardik\SoTest\Models\Item', // <- REPLACE WITH YOUR MODEL(ModelName)
          "master_field" => "picture", // <- REPLACE WITH ATTACHEMNT MODEL (gallery)
          "slave_type" => "System\Models\File",
          "session_key" => post('_session_key')
        ])->get();

        foreach($records as $record) {
          $fileRecord = \System\Models\File::find($record->slave_id);

          // code to resize image
          $width = 100;
          $height = 100;
          $options = []; // or ['mode' => 'crop']

          // just in place resize image
          Resizer::open($fileRecord->getLocalPath()) // create from real path
                    ->resize($width, $height, $options)
                    ->save($fileRecord->getLocalPath());
        }
      });
  });      
}

if any doubt please comment.

Hardik Satasiya
  • 9,547
  • 3
  • 22
  • 40
  • The FileUpload form widget has an unload handler: *https://octobercms.com/docs/api/backend/formwidgets/fileupload#onupload*. If I place the following code `Resizer::open($uploadedFile->getPathName())->resize(500, 300, ['mode' => 'crop'])->save($uploadedFile->getPathName());` right after the *line 431* of the widget php file: *~/modules/backend/formwidgets/FileUpload.php*, it works well. Is there a way to hook to that method in my plugin instead of modifying the core file? – qwaz Sep 21 '20 at 11:24
  • The functionality of the line of code demonstrated in my comment above works exactly as I want it, the only thing is that I don't know how to access that `onUpload()` method from my plugin, so that it persisted. I'd like to explore this option before I accept and upvote your solution. Do you have any idea as to how to access `onUpload()` method of FileUpload form widget from within the backend of my plugin? – qwaz Sep 21 '20 at 12:25
  • you can not do much there because there is no events OR extension features in that method `onUpload` so no luck there, either change core file `this not the way to do things` or make your own widget which is more work there is only these 2 options with this circumstances. – Hardik Satasiya Sep 21 '20 at 12:50