public function updateFileIdInOwnerModel($event) { $type_id = $this->owner->getPrimaryKey(); $configs = VariationHelper::getRawConfig(); foreach ($configs as $type => $config) { if (!in_array($type, $this->registeredTypes)) { continue; } if (isset($config['_insert'])) { $attribute = array_shift($config['_insert']); $files = Uploads::findByReference($type, $type_id); if (!is_null($files)) { $file = array_shift($files); //TODO replace with ActiveRecord::updateAttributes() to avoid loops if ($this->owner->getAttribute($attribute) !== $file->id) { $this->owner->setAttribute($attribute, $file->id); $this->owner->save(); // one more loop } } } } }
/** * @param $image * @param $variationName * @param $variationConfig * @return array * * Resize images by variation config */ public function makeVariation($imagine, $image, $variationName, $variationConfig) { $errors = []; if (!is_array($variationConfig)) { return ['Variation config must be an array']; } $config = VariationHelper::normalizeVariationConfig($variationConfig); // here because in normalizeVariationConfig we don't process variation name if ($variationName == '_thumb') { $config['mode'] = 'outbound'; } if ($config['mode'] == 'inset') { $mode = ImageInterface::THUMBNAIL_INSET; } else { $mode = ImageInterface::THUMBNAIL_OUTBOUND; } $image = $image->thumbnail(new Box($config['width'], $config['height']), $mode); // TODO order of watermark applying (before or after thumbnailing) if (isset($config['watermark'])) { $filter = new WatermarkFilter($imagine, $config['watermark']['path'], $config['watermark']['position'], $config['watermark']['margin']); $image = $filter->apply($image); } $options = array('quality' => $config['quality']); try { if (!$image->save($this->getUploadFilePath($variationName), $options)) { array_push($errors, 'Can not save generated image.'); } } catch (ErrorException $e) { array_push($errors, $e->getMessage()); } return $errors; }
private function fetchFiles($files, &$images, $name = 'file') { $errors = []; $type = Yii::$app->request->post('type'); $type_id = Yii::$app->request->post('type_id'); $hash = Yii::$app->request->post('hash'); // Check access. if `$type_id` is null, then access check must be only in ConnectFileSequence behaviour if (!is_null($type_id)) { $acl = VariationHelper::getAclOfType($type); if (!AccessControl::checkAccess($acl, $type_id)) { Yii::warning('Someone trying to upload file with no access.', 'file-processor'); return ['You have no access to perform this upload']; } } if (isset($files['tmp_name'])) { $file_temp_name = $files['tmp_name']; $file_real_name = basename($files['name']); if (is_uploaded_file($file_temp_name)) { $mime = FileHelper::getMimeType($file_temp_name); if (is_null($mime)) { $mime = FileHelper::getMimeTypeByExtension($file_real_name); } if (strpos($mime, 'image') !== false) { $file_dimensions = getimagesize($file_temp_name); } else { $file_dimensions = [null, null]; } // insert into db $model = new Uploads(); $model->type = $type; $model->type_id = $type_id; $model->hash = $hash; $model->ord = Uploads::getMaxOrderValue($type, $type_id, $hash) + 1; $model->filename = Uploads::generateBaseFileName($file_real_name); $model->original = $file_real_name; $model->mime = $mime; $model->size = filesize($file_temp_name); $model->width = $file_dimensions[0]; $model->height = $file_dimensions[1]; // save model, save file and fill response array if ($model->save()) { // load configuration $config = VariationHelper::getConfigOfType($model->type); $errors = array_merge($errors, $model->process($file_temp_name, $config)); // insert id of uploaded file into attribute in model (if needed) Uploads::updateConnectedModelAttribute($config, $model->type_id, $model->id); if (empty($errors)) { $images[$name] = ['width' => $model->width, 'height' => $model->height, 'mime' => $model->mime, 'size' => $model->size, 'id' => $model->id, 'type' => $model->type, 'type_id' => $model->type_id, 'hash' => $model->hash, 'errors' => null]; } else { $model->removeFile(); } } else { Yii::warning('file was unable to be saved. Errors: ' . VarDumper::dumpAsString($model->getErrors()), 'file-processor'); array_push($errors, 'File was unable to be saved.'); } } else { array_push($errors, 'File was unable to be uploaded.'); } } else { foreach ($files as $name => $file) { $errors = array_merge($errors, $this->fetchFiles($file, $images, $name)); } } return $errors; }