/** * To generate thumbnail from original image * @param string $sourceFileName * @param string $sourceFilePath * @param string $destinationPath */ public static function resizeImage($sourceFileName, $sourceFilePath, $destinationPath) { $fileHelper = new FileHelper(); $fileHelper->sourceFilename = $sourceFileName; $fileHelper->sourceFilepath = $sourceFilePath; $fileHelper->destinationPath = $destinationPath; $fileHelper->resizeImage('ticker'); }
/** * @inheritdoc */ public function beforeSave($insert) { $attributes = array_keys($this->getAttributes()); // ID if ($this->isNewRecord and empty($this->_id)) { $this->_id = uniqid(); } // SEO if (in_array('title', $attributes) and in_array('seo_url', $attributes) and empty($this->seo_url)) { $this->seo_url = StringHelper::asUrl($this->title); } if (in_array('title', $attributes) and in_array('seo_title', $attributes) and empty($this->seo_title)) { $this->seo_title = $this->title; } if (in_array('description', $attributes) and in_array('seo_desc', $attributes) and empty($this->seo_desc)) { $this->seo_desc = $this->description; } // Upload image if (in_array('image', $attributes)) { $image = \yii\web\UploadedFile::getInstance($this, 'image'); if (!empty($image)) { $this->image = \yii\web\UploadedFile::getInstance($this, 'image'); $ext = FileHelper::getExtention($this->image); if (!empty($ext)) { $fileDir = Yii::$app->controller->module->id . '/' . date('Y/m/d/'); if (property_exists($this, 'title')) { $title = $this->title; } elseif (property_exists($this, 'name')) { $title = $this->name; } else { $title = uniqid(); } $fileName = StringHelper::asUrl($title) . '.' . $ext; $folder = Yii::$app->params['uploadPath'] . '/' . Yii::$app->params['uploadDir'] . '/' . $fileDir; FileHelper::createDirectory($folder); $this->image->saveAs($folder . $fileName); $this->image = $fileDir . $fileName; } } else { $this->image = $this->image_old; } } // creator, editor and time $now = new MongoDate(); if (in_array('update_time', $attributes) and empty($this->update_time)) { $this->update_time = $now; } if (in_array('editor', $attributes) and !ClientHelper::isCommandLine()) { $this->editor = Yii::$app->user->id; } if ($this->isNewRecord) { if (in_array('creator', $attributes) and !ClientHelper::isCommandLine()) { $this->creator = Yii::$app->user->id; } if (in_array('create_time', $attributes) and $this->create_time == null) { $this->create_time = $now; } } return parent::beforeSave($insert); }
public function actionCreate() { $model = new Import(); if ($model->load(Yii::$app->request->post())) { $file_path = \yii\web\UploadedFile::getInstance($model, 'file_path'); if (!empty($file_path)) { $model->file_path = \yii\web\UploadedFile::getInstance($model, 'file_path'); $ext = FileHelper::getExtention($model->file_path); if (!empty($ext)) { $fileDir = Yii::$app->controller->module->id . '/' . date('Y/m/d/'); $fileName = uniqid() . StringHelper::asUrl(Yii::$app->controller->module->id) . '.' . $ext; $folder = Yii::$app->params['uploadPath'] . '/' . Yii::$app->params['uploadDir'] . '/' . $fileDir; FileHelper::createDirectory($folder); $model->file_path->saveAs($folder . $fileName); $model->file_path = $fileDir . $fileName; } } if ($model->save()) { return $this->redirect(['update', 'id' => (string) $model->_id]); } } Yii::$app->view->title = Yii::t($this->module->id, 'Create'); Yii::$app->view->params['breadcrumbs'][] = ['label' => Yii::t($this->module->id, 'Import'), 'url' => ['index']]; Yii::$app->view->params['breadcrumbs'][] = Yii::$app->view->title; return $this->render('create', ['model' => $model]); }
/** * Encode video * * @fixme проверить не осталось ли еще активных процессов на конвертацию от прошлой cron задачи, если осталось, то менять $threads для тещушего cron. * @return int * @throws \Exception * @throws \yii\web\UnsupportedMediaTypeHttpException */ public function actionEncode() { /** @var Video $video */ // get params $params = Yii::$app->params; $outputFormat = $params['video.encode.outputFormat']; $threads = $params['video.encode.threads']; $timeout = $params['video.encode.timeout']; $savePath = $params['path.video.converted']; // ffmpeg init $ffmpegConfig = ['ffmpeg.threads' => $threads, 'timeout' => $timeout]; // video processing $error = null; $videos = Video::getUnconvertedVideo($threads); foreach ($videos as $video) { $video->converted_filename = FileHelper::generateFileName($outputFormat, $savePath); $filePaths = $video->getPaths(); // video encode if (FfmpegHelper::convert($filePaths['upload'], $filePaths['converted'], $outputFormat, $ffmpegConfig)) { $meta = FfmpegHelper::getProperties($filePaths['converted']); $video->converted_video_bitrate = $meta['video_bitrate']; $video->converted_audio_bitrate = $meta['audio_bitrate']; $video->status = Video::STATUS_ENCODE_SUCCESS; } else { $video->status = Video::STATUS_ENCODE_ERROR; $error = true; } $video->save(false); } return $error === null ? Controller::EXIT_CODE_NORMAL : Controller::EXIT_CODE_ERROR; }
public function actionIndex() { $id = Yii::$app->request->get('id'); $modelName = Yii::$app->request->get('model'); if (!empty($id)) { $model = $modelName::findOne($id); if ($model) { // $model->gallery = \yii\web\UploadedFile::getInstance($model, 'gallery'); $image = \yii\web\UploadedFile::getInstance($model, 'gallery'); $ext = FileHelper::getExtention($image); if (!empty($ext)) { // Thêm validate ext ____________________________ $file_id = uniqid(); $fileDir = Yii::$app->controller->module->id . '/' . date('Y/m/d/'); $fileName = $file_id . '.' . $ext; $folder = Yii::$app->params['uploadPath'] . '/' . Yii::$app->params['uploadDir'] . '/' . $fileDir; FileHelper::createDirectory($folder); $image->saveAs($folder . $fileName); $output = ["out" => ['file_id' => $file_id, 'item_id' => Yii::$app->controller->module->id]]; if (empty($model->gallery)) { $model->gallery = $fileDir . $fileName; } else { $model->gallery .= ',' . $fileDir . $fileName; } $model->save(); echo json_encode($output); } } } }
public function beforeSave($insert) { $attributes = array_keys($this->getAttributes()); // ID if ($this->isNewRecord and empty($this->_id)) { $this->_id = uniqid(); } // SEO if (in_array('title', $attributes) and in_array('seo_url', $attributes) and empty($this->seo_url)) { $this->seo_url = StringHelper::asUrl($this->title); } if (in_array('title', $attributes) and in_array('seo_title', $attributes) and empty($this->seo_title)) { $this->seo_title = $this->title; } if (in_array('description', $attributes) and in_array('seo_desc', $attributes) and empty($this->seo_desc)) { $this->seo_desc = $this->description; } // Upload image $image = \yii\web\UploadedFile::getInstance($this, 'image'); if (!empty($image)) { $this->image = \yii\web\UploadedFile::getInstance($this, 'image'); $ext = FileHelper::getExtention($this->image); if (!empty($ext)) { $fileDir = LetHelper::getAvatarDir($this->primaryKey) . '/'; $fileName = $this->primaryKey . '.jpg'; //$fileName = $this->primaryKey . '.' . $ext; $folder = Yii::$app->params['uploadPath'] . '/' . Yii::$app->params['uploadDir'] . '/' . $fileDir; FileHelper::createDirectory($folder); $this->image->saveAs($folder . $fileName); $this->image = $fileDir . $fileName; } } else { $this->image = $this->image_old; } // creator, editor and time $now = new \MongoDate(); if (in_array('update_time', $attributes) and empty($this->update_time)) { $this->update_time = $now; } if (in_array('editor', $attributes) and !ClientHelper::isCommandLine()) { $this->editor = Yii::$app->user->id; } if ($this->isNewRecord) { if (in_array('creator', $attributes) and !ClientHelper::isCommandLine()) { $this->creator = Yii::$app->user->id; } if (in_array('create_time', $attributes) and $this->create_time == null) { $this->create_time = $now; } } if (!empty($this->password)) { $this->password_hash = Yii::$app->getSecurity()->generatePasswordHash($this->password); } if ($this->isNewRecord) { $this->auth_key = bin2hex(Yii::$app->getSecurity()->generateRandomKey()); $this->auth_key = substr($this->auth_key, 0, 32); } return true; }
public function beforeSave($insert) { if (parent::beforeSave($insert)) { FileHelper::saveFile($this, 'source_url', 'images/slide', $this->removeImage); return true; } return false; }
/** * Handle the event. * * @param PublicMessageFileWasUploaded $event * @return void */ public function handle(PublicMessageFileWasUploaded $event) { // update the file $uploadedFile = $event->uploadedFile; $directory = $event->publicMessageFile->directory; $file = FileHelper::upload($uploadedFile, $directory); // update the eloquent model $event->publicMessageFile->fill($file); }
public function save() { $model = parent::save(); if (strlen($this->upload_key)) { if ($this->upload_key == '__clear__') { $model->deleteImage(); $model->save(); } else { $path = \app\helpers\Upload::getTmpUploadPath($this->upload_key); if (is_file($path)) { $model->pullImage($path); $model->save(); FileHelper::unlink($path); } } } return $model; }
/** * Upload a file and save it in storage. * * @param UploadedFile $uploadedFile * @param string $directory * @return array */ static function upload(UploadedFile $uploadedFile, $directory) { $extension = $uploadedFile->getClientOriginalExtension(); if ($extension != '') { // get the filename $filename = $uploadedFile->getClientOriginalName(); // get the name of the file and the mime type $name = substr($filename, 0, strrpos($filename, '.')); $mime = $uploadedFile->getClientMimeType(); // clean up the filename $filename = FileHelper::serializeFilename($filename, true); // make sure the filename is unique $filename = FileHelper::incrementFilename($filename, $directory); // the full path to the file $filepath = $directory . $filename; // save new file to storage Storage::put($filepath, File::get($uploadedFile)); return array('name' => $name, 'mime' => $mime, 'filename' => $filename); } }
/** * @inheritdoc */ public function beforeSave($insert) { $attributes = array_keys($this->getAttributes()); if (property_exists($this, 'category_id')) { $this->isCategory(); } // Upload image if (in_array('image', $attributes)) { $this->image = \yii\web\UploadedFile::getInstance($this, 'image'); $ext = FileHelper::getExtention($this->image); if (!empty($ext)) { $fileDir = Yii::$app->controller->module->id . '/' . date('Y/m/d/'); $fileName = LetHelper::string_to_url($this->title) . '.' . $ext; $folder = Yii::$app->params['uploadPath'] . '/' . Yii::$app->params['uploadDir'] . '/' . $fileDir; FileHelper::createDirectory($folder); $this->image->saveAs($folder . $fileName); $this->image = $fileDir . $fileName; } } // creator, editor and time $now = date('Y-m-d H:i:s', time()); if (in_array('update_time', $attributes)) { $this->update_time = $now; } if (in_array('editor', $attributes)) { $this->editor = Yii::$app->user->id; } if ($this->isNewRecord) { if (in_array('creator', $attributes)) { $this->creator = Yii::$app->user->id; } if (in_array('create_time', $attributes)) { $this->create_time = $now; } } return parent::beforeSave($insert); }
/** * Upload Task Image. * * @param $file * @param $name * @param $householdId * @return string */ private function uploadImage($file, $name, $householdId) { $fileName = FileHelper::generateFileName($name, $file); $image = Image::make($file->getRealPath()); /* Prevent Image from possible upsizing and Maintain Aspect Ratio */ return $this->uploadRaw($image, $fileName, $householdId); }
protected function download($path, $display_if_can = true) { $pos = strrpos($path, '/'); $filename = substr($path, $pos + 1); if ($display_if_can && FileHelper::canShow($path)) { return Yii::$app->response->sendFile($path, $filename, ['inline' => true]); } return Yii::$app->response->sendFile($path, $filename); }
/** * Deletes a model * * @param $id * @throws ForbiddenHttpException * @throws ServerErrorHttpException * @throws \Exception */ public function actionDelete($id) { /** @var Video $videoModel */ $videoModel = $this->findModel($id); $this->checkAccess($this->action->id, $videoModel); // remove all related files $paths = $videoModel->getPaths(); FileHelper::removeFiles($paths); // remove model if ($videoModel->delete() === false) { throw new ServerErrorHttpException('Failed to delete the object for unknown reason.'); } Yii::$app->getResponse()->setStatusCode(204); }
/** * Check if file can be displayed in browser * @return array */ public function canShow() { return FileHelper::canShow($this->getPath()); }
public function pullImage($source) { $this->deleteImage(); $ex = FileHelper::getEx($source); $path = $this->path_to_source = static::getTmpPath('source', $ex); $absPath = static::getAbsPath($path); copy($source, $absPath); foreach (['big', 'mid'] as $type) { $path = $this->{"path_to_{$type}"} = static::getTmpPath($type, $ex); $absPath = static::getAbsPath($path); static::resizeImage($type, $source, $absPath); } $this->evalHash(); }
public static function resize($params) { $image_name = basename($params['source']); $image_ex = FileHelper::getEx($params['source']); $image_ex = strtolower($image_ex); $sizes = getimagesize($params['source']); $oldImage = self::from_file($params['source']); $crop = isset($params['crop']) ? $params['crop'] : false; if ($image_ex and $sizes and $oldImage) { list($oldWidth, $oldHeight) = $sizes; if ($params['width'] != "auto" and $params['height'] != "auto") { if ($crop) { $widthScale = $params['width'] / $oldWidth; $heightScale = $params['height'] / $oldHeight; $scale = min($widthScale, $heightScale); $transWidth = $params['width'] / $scale; $transHeight = $params['height'] / $scale; $dst_x = 0; $dst_y = 0; $src_x = $oldWidth / 2 - $transWidth / 2; $src_y = $oldHeight / 2 - $transHeight / 2; $dst_w = $params['width']; $dst_h = $params['height']; $src_w = $transWidth; $src_h = $transHeight; } else { $widthScale = $params['width'] / $oldWidth; $heightScale = $params['height'] / $oldHeight; $scale = min($widthScale, $heightScale); $width = $oldWidth * $scale; $height = $oldHeight * $scale; $dst_x = $params['width'] / 2 - $width / 2; $dst_y = $params['height'] / 2 - $height / 2; $src_x = 0; $src_y = 0; $dst_w = $width; $dst_h = $height; $src_w = $oldWidth; $src_h = $oldHeight; } } else { if ($params['height'] == "auto") { $scale = $params['width'] / $oldWidth; $params['height'] = $oldHeight * $scale; } else { $scale = $params['height'] / $oldHeight; $params['width'] = $oldWidth * $scale; } $dst_x = 0; $dst_y = 0; $src_x = 0; $src_y = 0; $dst_w = $params['width']; $dst_h = $params['height']; $src_w = $oldWidth; $src_h = $oldHeight; } $imageNew = imagecreatetruecolor($params['width'], $params['height']); if ($image_ex == "png") { imagealphablending($imageNew, false); imagesavealpha($imageNew, true); $white = imagecolorallocatealpha($imageNew, 255, 255, 255, 127); imagefilledrectangle($imageNew, 0, 0, $params['width'], $params['height'], $white); } else { $white = imagecolorallocate($imageNew, 255, 255, 255); imagefilledrectangle($imageNew, 0, 0, $params['width'], $params['height'], $white); } imagecopyresampled($imageNew, $oldImage, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); switch ($image_ex) { case "jpg": case "jpeg": $q = isset($params['quality']) ? $params['quality'] : 100; imagejpeg($imageNew, $params['dest'], $q); break; case "png": imagepng($imageNew, $params['dest']); break; case "gif": imagegif($imageNew, $params['dest']); break; } } }