UploadCrop.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * UploadCrop Widget
  4. *
  5. * @author Joseba Juaniz <joseba.juaniz@gmail.com>
  6. * @since 1.0
  7. */
  8. namespace app\components\admin\uploadcrop;
  9. use Yii;
  10. use yii\base\InvalidParamException;
  11. use yii\base\Widget;
  12. use yii\bootstrap4\Modal;
  13. use yii\helpers\Html;
  14. use yii\helpers\Json;
  15. use yii\widgets\ActiveForm;
  16. //use \dpodium\yii2\widget\upload\crop;
  17. class UploadCrop extends Widget
  18. {
  19. /** @var \yii\db\ActiveRecord */
  20. var $model;
  21. /** @var string */
  22. var $attribute;
  23. /* @var ActiveForm */
  24. var $form = NULL;
  25. /** @var boolean */
  26. var $enableClientValidation;
  27. /** @var array */
  28. var $imageOptions;
  29. /** @var array */
  30. var $jcropOptions = array();
  31. /** @var integer */
  32. var $maxSize = 400;
  33. var $title = 'Crop image';
  34. var $changePhotoTitle = '';
  35. var $imageSrc;
  36. /**
  37. * only call this method after a form closing and
  38. * when user hasn't used in the widget call the parameter $form
  39. * this adds to every form in the view the field validation.
  40. *
  41. * @param array $config
  42. * @return string
  43. * @throws \yii\base\InvalidConfigException
  44. */
  45. static function manualValidation($config = [])
  46. {
  47. if (!array_key_exists('model', $config) || !array_key_exists('attribute', $config)) {
  48. throw new InvalidParamException('Config array must have a model and attribute.');
  49. }
  50. $view = Yii::$app->getView();
  51. $field_id = Html::getInputId($config['model'], $config['attribute']);
  52. $view->registerJs('$("#' . $field_id . '").urlParser("launchValidation");');
  53. }
  54. /**
  55. * Renders the field.
  56. */
  57. public function run()
  58. {
  59. if (is_null($this->imageOptions))
  60. {
  61. $this->imageOptions = [
  62. 'alt' => 'Crop this image'
  63. ];
  64. }
  65. $this->imageOptions['id'] = Yii::$app->getSecurity()->generateRandomString(10);
  66. $inputField = Html::getInputId($this->model, $this->attribute, ['data-image_id' => $this->imageOptions['id']]);
  67. $default_jcropOptions = [
  68. 'dashed' => FALSE,
  69. 'rotatable' => FALSE];
  70. $this->jcropOptions = array_merge($default_jcropOptions, $this->jcropOptions);
  71. if (is_null($this->form))
  72. {
  73. $this->form = new ActiveForm();
  74. if (!is_null($this->enableClientValidation))
  75. {
  76. $this->form->enableClientValidation = $this->enableClientValidation;
  77. }
  78. }
  79. $view = $this->getView();
  80. $assets = \dpodium\yii2\widget\upload\crop\UploadCropAsset::register($view);
  81. echo Html::beginTag('div', ['class' => 'uploadcrop']);
  82. if ($this->imageSrc == '') {
  83. $img = $assets->baseUrl . '/img/nophoto.png';
  84. $pimg = '';
  85. } else {
  86. $img = $this->imageSrc;
  87. $pimg = $this->imageSrc;
  88. }
  89. echo $this->form->field($this->model, $this->attribute)->fileInput(['style'=>'cursor:pointer;'])->label(Html::img($img, ['width'=>300,'height'=>169]));
  90. echo Html::beginTag('span', ['class'=>'upload-title']);
  91. echo $this->changePhotoTitle;
  92. echo Html::endTag('span');
  93. echo Html::beginTag('div', ['id' => 'preview-pane']);
  94. echo Html::beginTag('div', ['class' => 'preview-container']);
  95. echo Html::img($pimg, ['class' => 'preview_image', 'style'=>'width:300px;']);
  96. echo Html::endTag('div');
  97. echo Html::endTag('div');
  98. ?>
  99. <div id="data"></div>
  100. <?
  101. Modal::begin([
  102. 'id' => 'cropper-modal-'. $this->imageOptions['id'],
  103. 'title' => '<h3>'.$this->title.'</h3>',
  104. 'closeButton' => [],
  105. 'footer' => Html::button('Cancel', ['id' => $this->imageOptions['id'].'_button_cancel', 'class' => 'btn btn-default', 'data-dismiss' => 'modal']) . Html::button('Crop & Save', ['id' => $this->imageOptions['id'].'_button_accept', 'class' => 'btn btn-success cropper-done']),
  106. 'size' => Modal::SIZE_LARGE,
  107. ]);
  108. $idh='cropper-modal-'. $this->imageOptions['id'];
  109. echo Html::beginTag('div', ['class' => 'spinner']);
  110. echo Html::beginTag('div', ['class' => 'bounce1']);
  111. echo Html::endTag('div');
  112. echo Html::beginTag('div', ['class' => 'bounce2']);
  113. echo Html::endTag('div');
  114. echo Html::beginTag('div', ['class' => 'bounce3']);
  115. echo Html::endTag('div');
  116. echo Html::endTag('div');
  117. echo Html::beginTag('div', ['id' => 'image-source']);
  118. echo Html::img('', $this->imageOptions);
  119. echo Html::endTag('div');
  120. echo Html::beginTag('div', ['style' => 'display: block;margin: 0 auto;text-align: center;']);
  121. echo Html::beginTag('button', ['title' => 'Zoom In', 'class' => 'btn btn-primary', 'type' => 'button', 'id'=>'zoom-in']);
  122. echo Html::beginTag('span', ['class' => 'fa fa-search-plus']);
  123. echo Html::endTag('span');
  124. echo Html::endTag('button');
  125. echo Html::beginTag('button', ['title' => 'Zoom Out', 'class' => 'btn btn-primary', 'type' => 'button', 'id'=>'zoom-out']);
  126. echo Html::beginTag('span', ['class' => 'fa fa-search-minus']);
  127. echo Html::endTag('span');
  128. echo Html::endTag('button');
  129. ?>
  130. <div class="btn-group">
  131. <button class="btn btn-primary" type="button" id="flip" onclick="$('#'+$('#<?=$idh?>').find('img').attr('id')).cropper('scaleY',-1)"><span class="fa fa-arrows-alt-h"></span></button>
  132. <button class="btn btn-primary" type="button" id="rotate" onclick="$('#'+$('#<?=$idh?>').find('img').attr('id')).cropper('scaleX', -1);"><span class="fa fa-arrows-alt-v"></span></button></div>
  133. <div class="btn-group">
  134. <button type="button" class="btn btn-primary" data-method="reset" title="Reset" onclick="$('#'+$('#<?=$idh?>').find('img').attr('id')).cropper('reset');">
  135. <span class="docs-tooltip" data-toggle="tooltip" data-animation="false">
  136. <span class="fa fa-sync-alt"></span>
  137. </span>
  138. </button>
  139. </div>
  140. <script>
  141. $('#<?='cropper-modal-'. $this->imageOptions['id']?>').on('hidden.bs.modal', function (e) {
  142. // alert(11);
  143. // console.log($(this).cropper('GetData'));
  144. })
  145. </script>
  146. <?
  147. echo Html::endTag('div');
  148. echo html::hiddenInput('cropping[x]', '', ['id' => $inputField.'-x']);
  149. echo html::hiddenInput('cropping[width]','', ['id' => $inputField.'-width']);
  150. echo html::hiddenInput('cropping[y]', '', ['id' => $inputField.'-y']);
  151. echo html::hiddenInput('cropping[height]','', ['id' => $inputField.'-height']);
  152. Modal::end();
  153. echo Html::endTag('div');
  154. $jcropOptions = ['inputField' => $inputField, 'jcropOptions' => $this->jcropOptions];
  155. $jcropOptions['maxSize'] = $this->maxSize;
  156. $jcropOptions['formId'] = $this->form->id;
  157. $jcropOptions = Json::encode($jcropOptions);
  158. $view->registerJs('jQuery("#'.$inputField.'").uploadCrop('.$jcropOptions.');');
  159. }
  160. }