szajbus / uploadpack

Easy way to handle file uploads in CakePHP.
MIT License
101 stars 35 forks source link

Request for adding "afterMove" callback method in UploadBehavior #36

Closed Masato-Kato closed 10 years ago

Masato-Kato commented 10 years ago

There are three commits but the last one is effective. Need a sample code for using afterMove()?

Masato-Kato commented 10 years ago

Using this callback you can preprocess uploaded file prior to resizing - e.g. Exif orientation processing, color adjustment, watermarking, etc. (However you may want to add watermark after resizing, and want another callback method, like afterResize, to do so.)

Writing your own behavior, like MyUploadBehavior class which extends UploadBehavior and put it in accessible place, you can write your own process in this "afterMove" callback method.

App::uses('UploadBehavior', 'UploadPack.Model/Behavior');

class MyUploadBehavior extends UploadBehavior {
  protected function afterMove($file) {
    if ( ($exif=@exif_read_data($file)) && ($orientation=@$exif['Orientation']) ) {
      $tmpfile = tempnam(TMP, '_');
      $convert = '/usr/bin/convert';
      switch ( (int)$orientation ) {
      // (some lines omitted here)
      case 6:
        system("{$convert} -rotate 90 {$file} {$tmpfile}; mv {$tmpfile} {$file}");
        break;
      // (some lines omitted here)
      }
    }
  }
}

Using this behavior the uploaded file will be automatically rotated if it has an exif orientation data.

szajbus commented 10 years ago

Thanks!

Masato-Kato commented 10 years ago

I see new version 0.7.1. Thank you very much.