/** * Upload the image */ public function actionUploadImage() { Yii::$app->response->format = Response::FORMAT_JSON; if (!empty($_FILES['file']['tmp_name'])) { if ($image = Image::load($_FILES['file']['tmp_name'], $this->_module->imageDriver)) { if (!empty($this->_module->maxImageResolution)) { $resolution = explode('x', strtolower($this->_module->maxImageResolution)); $width = empty($resolution[0]) ? null : $resolution[0]; $height = empty($resolution[1]) ? null : $resolution[1]; if ($image->width > $width && $image->height > $height) { $image->resize($width, $height, 'auto'); } } $extension = strtolower(image_type_to_extension($image->type, true)); if (!in_array($extension, ['.jpg', '.gif', '.png'])) { $extension = '.jpg'; } $filename = $this->uniqueRandomFilename($this->_module->imageUploadPath, $extension); if ($image->save("{$this->_module->imageUploadPath}/{$filename}{$extension}")) { return ['filelink' => "{$this->_module->imageBaseUrl}/{$filename}{$extension}"]; } } } return []; }
/** * Upload the images. * @param Event $event * @throws ErrorException */ public function uploadImages($event) { if (!empty($this->attributes)) { $modelName = array_reverse(explode('\\', $this->owner->className()))[0]; if (!empty($_FILES[$modelName]['tmp_name'])) { $save = false; $files =& $_FILES[$modelName]['tmp_name']; if (!$this->owner->hasMethod('getPrimaryKey', false)) { throw new ErrorException(sprintf('Cannot get primary key of class %s', $this->owner->className())); } /** @var int $primaryKey */ $primaryKey = call_user_func([$this->owner, 'getPrimaryKey']); $upload_dir = Yii::getAlias($this->upload_dir, true); foreach ($this->attributes as $attribute => $settings) { if (!empty($files[$attribute])) { $save = true; $settings = array_merge($this->defaultImagesSettings, $settings); $filename = $primaryKey . '-' . Yii::$app->security->generateRandomString(mt_rand(5, 12)); if (!in_array($attribute, $this->owner->attributes())) { throw new ErrorException(sprintf('Cannot get attribute %s of class %s', $attribute, $this->owner->className())); } foreach ($settings['sizes'] as $name => $options) { if (is_array($options)) { $options = array_merge($settings, $options); } else { $options = array_merge($settings, ['size' => $options]); } if ($image = Image::load($files[$attribute], $this->imageDriver)) { $path = $this->schemaTo($upload_dir, $attribute, $name); if (!empty($this->owner->{$attribute})) { @unlink("{$path}/{$this->owner->{$attribute}}.{$options['format']}"); } if ($options['size'] !== 'original') { $info = explode('x', strtolower($options['size'])); $width = empty($info[0]) ? null : $info[0]; $height = empty($info[1]) ? null : $info[1]; $image->resize($width, $height, $options['master'])->background($options['background']); } if ($image->save("{$path}/{$filename}.{$options['format']}", $options['quality'])) { $this->owner->{$attribute} = $filename; } else { $this->owner->{$attribute} = ''; } } } unset($files[$attribute]); } } if ($save && $event->name === BaseActiveRecord::EVENT_AFTER_UPDATE) { $this->owner->save(false); } } } }