Beispiel #1
1
 public function upload($article_id)
 {
     $accessToken = Yii::$app->params['accessToken'];
     $dbxClient = new dbx\Client($accessToken, "PHP-Example/1.0");
     $accountInfo = $dbxClient->getAccountInfo();
     if ($this->validate()) {
         $image_name = Yii::$app->security->generateRandomString();
         $this->file->saveAs('uploads/' . $image_name . '.' . $this->file->extension);
         $file_name = 'uploads/' . $image_name . '.' . $this->file->extension;
         $f = fopen("{$file_name}", "rb");
         $result = $dbxClient->uploadFile("/{$image_name}" . '.' . $this->file->extension, dbx\WriteMode::add(), $f);
         fclose($f);
         $src = $dbxClient->createShareableLink($result['path']);
         $src[strlen($src) - 1] = '1';
         $image = new Image();
         $image->image_name = $image_name . '.' . $this->file->extension;
         $image->image_article = $article_id;
         $image->image_src_big = $src;
         //small
         $file_name = 'uploads/' . $image_name . '.' . $this->file->extension;
         $resizedFile = 'uploads/small_' . $image_name . '.' . $this->file->extension;
         smart_resize_image($file_name, null, 0, 100, true, $resizedFile, false, false, 100);
         $f = fopen("{$resizedFile}", "rb");
         $result = $dbxClient->uploadFile("/small_" . "{$image_name}" . '.' . $this->file->extension, dbx\WriteMode::add(), $f);
         fclose($f);
         $src = $dbxClient->createShareableLink($result['path']);
         $src[strlen($src) - 1] = '1';
         $image->image_src_small = $src;
         $image->save();
         unlink($file_name);
         unlink($resizedFile);
         return true;
     } else {
         return false;
     }
 }
 public function upload($form_data)
 {
     $validator = Validator::make($form_data, Image::$rules, Image::$messages);
     if ($validator->fails()) {
         return Response::json(['error' => true, 'message' => $validator->messages()->first(), 'code' => 400], 400);
     }
     $photo = $form_data['file'];
     $originalName = $photo->getClientOriginalName();
     $originalNameWithoutExt = substr($originalName, 0, strlen($originalName) - 4);
     $filename = $this->sanitize($originalNameWithoutExt);
     $allowed_filename = $this->createUniqueFilename($filename);
     $filenameExt = $form_data['id_auxiliar'] . $allowed_filename . '.jpg';
     $uploadSuccess1 = $this->original($photo, $filenameExt);
     $uploadSuccess2 = $this->icon($photo, $filenameExt);
     if (!$uploadSuccess1 || !$uploadSuccess2) {
         return Response::json(['error' => true, 'message' => 'Server error while uploading', 'code' => 500], 500);
     }
     $sessionImage = new Image();
     $sessionImage->filename = $form_data['id_auxiliar'] . $allowed_filename . '.jpg';
     $sessionImage->original_name = $originalName;
     $sessionImage->id_catalogo_fotografia = $form_data['id_catalogo_fotografia'];
     $sessionImage->id_usuario_servicio = $form_data['id_usuario_servicio'];
     $sessionImage->id_auxiliar = $form_data['id_auxiliar'];
     $sessionImage->estado_fotografia = 1;
     $sessionImage->save();
     return Response::json(['error' => false, 'code' => 200], 200);
 }
Beispiel #3
0
 /**
  * Calculate p-hash and save image
  * @param string $file
  * @throws Exception
  * @return Image
  */
 public static function importImage($file)
 {
     try {
         $fileInfo = pathinfo($file);
         $model = new Image();
         $model->md5 = md5_file($file);
         $model->phash = self::getPHash($file);
         $model->path = sprintf('%s/%s.%s', substr($model->md5, 0, 2), $model->md5, $fileInfo['extension']);
         $model->previewPath = sprintf('%s/%s_preview.%s', substr($model->md5, 0, 2), $model->md5, $fileInfo['extension']);
         if ($model->save()) {
             $dstImage = Yii::getAlias('@app/web/uploads') . '/' . $model->path;
             $dstPath = dirname($dstImage);
             if (!is_dir($dstPath)) {
                 mkdir($dstPath, 0777, true);
             }
             if (!file_exists($dstImage)) {
                 copy($file, $dstImage);
                 Imagine::thumbnail($file, 300, 300)->save(Yii::getAlias('@app/web/uploads') . '/' . $model->previewPath, ['format' => 'jpg', 'quality' => 90]);
             }
             return $model;
         } else {
             return null;
         }
     } catch (\Exception $e) {
     }
     return null;
 }
 public function upload($form_data)
 {
     $validator = Validator::make($form_data, Image::$rules, Image::$messages);
     if ($validator->fails()) {
         return Response::json(['error' => true, 'message' => $validator->messages()->first(), 'code' => 400], 400);
     }
     $photo = $form_data['file'];
     $originalName = $photo->getClientOriginalName();
     Log::info(__CLASS__ . "::" . __METHOD__ . "::" . "Original filename is obtained as  " . $originalName);
     $originalNameWithoutExt = substr($originalName, 0, strlen($originalName) - 4);
     Log::info(__CLASS__ . "::" . __METHOD__ . "::" . "Original filename without ext is  " . $originalNameWithoutExt);
     $filename = $this->sanitize($originalNameWithoutExt);
     Log::info(__CLASS__ . "::" . __METHOD__ . "::" . "Sanitized filename is  " . $filename);
     $allowed_filename = $this->createUniqueFilename($filename);
     Log::info(__CLASS__ . "::" . __METHOD__ . "::" . "Unique filename is   " . $allowed_filename);
     $filenameExt = $allowed_filename . '.jpg';
     $uploadSuccess1 = $this->original($photo, $filenameExt);
     if (!$uploadSuccess1) {
         return Response::json(['error' => true, 'message' => 'Server error while uploading', 'code' => 500], 500);
     }
     $sessionImage = new Image();
     //    $sessionImage->filename      = $allowed_filename;
     $sessionImage->path = $filenameExt;
     $sessionImage->save();
     return Response::json(['error' => false, 'code' => 200, 'message' => $filenameExt], 200);
 }
Beispiel #5
0
 /**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function store(Request $request)
 {
     $image = new Image();
     $imageName = substr(md5(rand()), 0, 7) . '.' . $request->file('image')->getClientOriginalExtension();
     $request->file('image')->move(base_path() . '/public/img/', $imageName);
     $image->file_name = $imageName;
     $image->save();
     return Redirect::route('image.index');
 }
 /**
  * Creates a new Image model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Image();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Beispiel #7
0
 public function attachImage($attach, $default = false)
 {
     $image = new Image();
     $image->attach = $attach;
     $image->related_model = $this->owner;
     $image->table = $this->owner->formName();
     $image->object_id = $this->owner->id;
     $image->save();
     $image->default = $default;
     return $image;
 }
Beispiel #8
0
 public static function saveByFile($uploadedFileInstance)
 {
     if ($uploadedFileInstance !== null) {
         $i = new Image();
         $rnd = rand(0, 99999);
         $fileName = $rnd . '_' . $uploadedFileInstance->name;
         $localPath = '/files/' . $fileName;
         $filePath = Yii::$app->basePath . $localPath;
         $uploadedFileInstance->saveAs($filePath);
         $i->source = $localPath;
         if ($i->save()) {
             return $i;
         }
     }
     return null;
 }
Beispiel #9
0
 protected function actionUploadImage()
 {
     if ($this->isPost()) {
         try {
             $file = new Uploader();
             $file->setUploadsDir('/home/rayman/domains/my-blog/App/file/');
             $file->format = ['jpg' => 'image/jpeg', 'png' => 'image/png', 'gif' => 'image/gif'];
             $path = $file->UploadFile();
             $image = new Image();
             $image->image = $path;
             var_dump($image->save());
         } catch (\Exception $e) {
             $this->view->error = $e;
         }
     }
     $this->view->display(__DIR__ . '/../Templates/Form.php');
 }
 public function update($id)
 {
     $property = Property::find($id);
     $property->name = $this->request->name;
     $property->location = $this->request->location;
     $bSuccess = $property->save();
     if ($bSuccess) {
         if ($this->request->hasFile('files')) {
             $userId = Auth::user()->id;
             foreach ($this->request->file('files') as $file) {
                 $fileName = uniqid() . '-' . $file->getClientOriginalName();
                 $image = new Image();
                 $image->filename = $fileName;
                 $image->propertyId = $property->id;
                 $image->userId = $userId;
                 $image->save();
                 $file->move(base_path() . '/public/uploads/', $fileName);
             }
         }
         return redirect('dashboard');
     }
 }
 public function upload($form_data)
 {
     $validator = Validator::make($form_data, Image::$rules, Image::$messages);
     if ($validator->fails()) {
         return Response::json(['error' => true, 'message' => $validator->messages()->first(), 'code' => 400], 400);
     }
     $photo = $form_data['file'];
     $originalName = $photo->getClientOriginalName();
     $extension = $photo->getClientOriginalExtension();
     $originalNameWithoutExt = substr($originalName, 0, strlen($originalName) - strlen($extension) - 1);
     $filename = $this->sanitize($originalNameWithoutExt);
     $allowed_filename = $this->createUniqueFilename($filename, $extension);
     $uploadSuccess1 = $this->original($photo, $allowed_filename);
     $uploadSuccess2 = $this->icon($photo, $allowed_filename);
     if (!$uploadSuccess1 || !$uploadSuccess2) {
         return Response::json(['error' => true, 'message' => 'Server error while uploading', 'code' => 500], 500);
     }
     $sessionImage = new Image();
     $sessionImage->filename = $allowed_filename;
     $sessionImage->original_name = $originalName;
     $sessionImage->save();
     return Response::json(['error' => false, 'code' => 200, 'filename' => $allowed_filename], 200);
 }
 public function postUpload()
 {
     $form_data = Input::all();
     $validator = Validator::make($form_data, Image::$rules, Image::$messages);
     if ($validator->fails()) {
         return Response::json(['status' => 'error', 'message' => $validator->messages()->first()], 200);
     }
     $photo = $form_data['img'];
     $original_name = $photo->getClientOriginalName();
     $original_name_without_ext = substr($original_name, 0, strlen($original_name) - 4);
     $filename = $this->sanitize($original_name_without_ext);
     $allowed_filename = $this->createUniqueFilename($filename);
     $filename_ext = $allowed_filename . '.jpg';
     $manager = new ImageManager();
     $image = $manager->make($photo)->encode('jpg')->save(env('UPLOAD_PATH') . $filename_ext);
     if (!$image) {
         return Response::json(['status' => 'error', 'message' => 'Server error while uploading'], 200);
     }
     $database_image = new Image();
     $database_image->filename = $allowed_filename;
     $database_image->original_name = $original_name;
     $database_image->save();
     return Response::json(['status' => 'success', 'url' => env('URL') . 'uploads/' . $filename_ext, 'width' => $image->width(), 'height' => $image->height()], 200);
 }
Beispiel #13
0
 public function store()
 {
     $csvPath = Input::file('file')->getRealPath();
     $collectionName = Input::file('file')->getClientOriginalName();
     $collectionName = substr($collectionName, 0, strlen($collectionName) - 4);
     $csv = new parseCSV();
     $csv->auto($csvPath);
     $data = $csv->data;
     // If collection exists, drop it
     if (Schema::hasCollection($collectionName)) {
         Schema::drop($collectionName);
     }
     switch ($collectionName) {
         case 'Species':
             Schema::create('Species', function ($collection) {
                 //                    $collection->unique('spID');
             });
             foreach ($data as $row) {
                 $keys = array_keys($row);
                 //
                 $newSpecies = new Species();
                 //
                 foreach ($keys as $key) {
                     if (is_numeric($row[$key])) {
                         $row[$key] = (int) $row[$key];
                     }
                     $newSpecies[$key] = $row[$key];
                 }
                 $newSpecies->save();
             }
             break;
         case 'Interactions':
             Schema::create('Interactions', function ($collection) {
                 //                    $collection->unique('spID');
             });
             foreach ($data as $row) {
                 $keys = array_keys($row);
                 //
                 $newInteraction = new Interaction();
                 //
                 foreach ($keys as $key) {
                     if (is_numeric($row[$key])) {
                         $row[$key] = (int) $row[$key];
                     }
                     $newInteraction[$key] = $row[$key];
                 }
                 $newInteraction->save();
             }
             break;
         case 'TaxaGroups':
             Schema::create('TaxaGroups', function ($collection) {
                 //                    $collection->unique('spID');
             });
             foreach ($data as $row) {
                 $keys = array_keys($row);
                 //
                 $newTaxaGroup = new TaxaGroup();
                 //
                 foreach ($keys as $key) {
                     if (is_numeric($row[$key])) {
                         $row[$key] = (int) $row[$key];
                     }
                     $newTaxaGroup[$key] = $row[$key];
                 }
                 $newTaxaGroup->save();
             }
             break;
         case 'LitSources':
             Schema::create('LitSources', function ($collection) {
                 //                    $collection->unique('spID');
             });
             foreach ($data as $row) {
                 $keys = array_keys($row);
                 //
                 $newLitSource = new LitSource();
                 //
                 foreach ($keys as $key) {
                     if (is_numeric($row[$key])) {
                         $row[$key] = (int) $row[$key];
                     }
                     $newLitSource[$key] = $row[$key];
                 }
                 $newLitSource->save();
             }
             break;
         case 'Images':
             Schema::create('Images', function ($collection) {
                 //                    $collection->unique('spID');
             });
             foreach ($data as $row) {
                 $keys = array_keys($row);
                 //
                 $newImage = new Image();
                 //
                 foreach ($keys as $key) {
                     if (is_numeric($row[$key])) {
                         $row[$key] = (int) $row[$key];
                     }
                     $newImage[$key] = $row[$key];
                 }
                 $newImage->save();
             }
             break;
         case 'Synonyms':
             Schema::create('Synonyms', function ($collection) {
                 //                    $collection->unique('spID');
             });
             foreach ($data as $row) {
                 $keys = array_keys($row);
                 //
                 $newSynonym = new Synonym();
                 //
                 foreach ($keys as $key) {
                     if (is_numeric($row[$key])) {
                         $row[$key] = (int) $row[$key];
                     }
                     $newSynonym[$key] = $row[$key];
                 }
                 $newSynonym->save();
             }
             break;
         case 'BioNotes':
             Schema::create('BioNotes', function ($collection) {
                 //                    $collection->unique('spID');
             });
             foreach ($data as $row) {
                 $keys = array_keys($row);
                 //
                 $newBioNote = new BioNote();
                 //
                 foreach ($keys as $key) {
                     if (is_numeric($row[$key])) {
                         $row[$key] = (int) $row[$key];
                     }
                     $newBioNote[$key] = $row[$key];
                 }
                 $newBioNote->save();
             }
             break;
         default:
             return "Nothing is done";
     }
     //$csv->data[4]['spID'];
     return 'success!';
 }
Beispiel #14
0
 /**
  * Process upload of thumbnail
  *
  * @return mixed the uploaded thumbnail instance
  */
 public function upload()
 {
     // get the uploaded file instance. for multiple file uploads
     // the following data will return an array (you may need to use
     // getInstances method)
     $thumbnails = UploadedFile::getInstances($this, 'thumbnail');
     $isThumbnail = true;
     // if no thumbnail was uploaded abort the upload
     if (empty($thumbnails)) {
         return false;
     }
     // generate store the source file name
     $storage_path = Yii::$app->params['uploadStoragesPath'];
     $path = implode("/", [$storage_path, Yii::$app->formatter->asQuoteTabelName(self::tableName())]);
     if (!is_dir($path)) {
         mkdir($path);
     }
     foreach ($thumbnails as $thumbnail) {
         $image = new Image();
         $image->target = Yii::$app->formatter->asQuoteTabelName(self::tableName());
         $image->target_id = $this->getId();
         $image->is_thumbnail = $isThumbnail ? Image::_IS_THUMBNAIL : Image::_NOT_THUMBNAIL;
         $image->type = $image->type;
         $image->status = Image::STATUS_ACTIVE;
         if ($image->save(false)) {
             $thumbnail->saveAs($storage_path . "/" . $image->getFullPath());
             $isThumbnail = false;
         }
     }
 }
 public function actionImagesUpload()
 {
     $out = [];
     $images = \yii\web\UploadedFile::getInstancesByName('images');
     $isThumbnail = true;
     $target = Yii::$app->request->post('target');
     $target_id = Yii::$app->request->post('target_id');
     if (empty($images)) {
         echo Json::encode(['error' => Yii::t('message', 'No files found for upload.')]);
         // or you can throw an exception
         return;
         // terminate
     }
     $storage_path = Yii::$app->params['uploadStoragesPath'];
     $path = implode("/", [$storage_path, $target]);
     if (!is_dir($path)) {
         mkdir($path);
     }
     foreach ($images as $image) {
         $img = new Image();
         $img->target = $target;
         $img->target_id = $target_id;
         $img->is_thumbnail = $isThumbnail ? Image::_IS_THUMBNAIL : Image::_NOT_THUMBNAIL;
         $img->type = $image->type;
         $img->status = Image::STATUS_ACTIVE;
         if ($img->save(false)) {
             $image->saveAs($storage_path . "/" . $img->getFullPath());
             $isThumbnail = false;
             echo Json::encode(['success' => Yii::t('message', 'Up files (:path) success !.')]);
         } else {
             echo Json::encode(['error' => Yii::t('message', 'Up files (:path) error !.')]);
         }
     }
 }