Example #1
0
 /**
  * Resize and save image file
  *
  * @param \Symfony\Component\HttpFoundation\File\UploadedFile $file
  * @param                                                     $path
  * @param string                                              $prefix
  * @param int                                                 $width
  *
  * @return array|bool
  * @throws Exception
  */
 function save_image(Symfony\Component\HttpFoundation\File\UploadedFile $file, $path, $prefix = 'img', $width = 720)
 {
     if (!class_exists('Image')) {
         // We need intervention/image package
         throw new Exception('Intervention Image class required!');
     }
     $fileName = sprintf("%s_%d_%s", $prefix, time(), $file->getClientOriginalName());
     $savePath = "{$path}/{$fileName}";
     if (File::exists($savePath)) {
         throw new Exception('Image with a same name already exists!');
     }
     $image = Image::make($file->getRealPath());
     if ($image->width() > $width) {
         $image->resize($width, null, function ($constraint) {
             $constraint->aspectRatio();
         });
     }
     if ($image->save($savePath)) {
         return ['filename' => $fileName, 'size' => $file->getSize()];
     }
     return false;
 }
Example #2
0
 public static function upload($url, $gallery = NULL, $title = '')
 {
     $img_data = @file_get_contents($url);
     if (!$img_data) {
         return false;
     }
     $tmp_path = storage_path(md5($url));
     try {
         file_put_contents($tmp_path, $img_data);
     } catch (Exception $e) {
         echo 'Error #' . $e->getCode() . ':' . $e->getMessage() . "<br/>\n";
         echo 'In file: ' . $e->getFile() . ' (' . $e->getLine() . ')';
         die;
     }
     $file = new \Symfony\Component\HttpFoundation\File\UploadedFile($tmp_path, basename($url));
     ## Check upload & thumb dir
     $uploadPath = Config::get('site.galleries_photo_dir');
     $thumbsPath = Config::get('site.galleries_thumb_dir');
     if (!File::exists($uploadPath)) {
         File::makeDirectory($uploadPath, 0777, TRUE);
     }
     if (!File::exists($thumbsPath)) {
         File::makeDirectory($thumbsPath, 0777, TRUE);
     }
     ## Generate filename
     $fileName = time() . "_" . rand(1000, 1999) . '.' . $file->getClientOriginalExtension();
     #echo $fileName;
     ## Get images resize parameters from config
     $thumb_size = Config::get('site.galleries_thumb_size');
     $photo_size = Config::get('site.galleries_photo_size');
     ## Get image width & height
     $image = ImageManipulation::make($file->getRealPath());
     $w = $image->width();
     $h = $image->height();
     if ($thumb_size > 0) {
         ## Normal resize
         $thumb_resize_w = $thumb_size;
         $thumb_resize_h = $thumb_size;
     } else {
         ## Resize "by the smaller side"
         $thumb_size = abs($thumb_size);
         ## Resize thumb & full-size images "by the smaller side".
         ## Declared size will always be a minimum.
         $thumb_resize_w = $w > $h ? null : $thumb_size;
         $thumb_resize_h = $w > $h ? $thumb_size : null;
     }
     ## Resize thumb image
     $thumb_upload_success = ImageManipulation::make($file->getRealPath())->resize($thumb_resize_w, $thumb_resize_h, function ($constraint) {
         $constraint->aspectRatio();
         $constraint->upsize();
     })->save($thumbsPath . '/' . $fileName);
     if ($photo_size > 0) {
         ## Normal resize
         $image_resize_w = $photo_size;
         $image_resize_h = $photo_size;
     } else {
         ## Resize "by the smaller side"
         $photo_size = abs($photo_size);
         ## Resize full-size images "by the smaller side".
         ## Declared size will always be a minimum.
         $image_resize_w = $w > $h ? null : $photo_size;
         $image_resize_h = $w > $h ? $photo_size : null;
     }
     ## Resize full-size image
     $image_upload_success = ImageManipulation::make($file->getRealPath())->resize($image_resize_w, $image_resize_h, function ($constraint) {
         $constraint->aspectRatio();
         $constraint->upsize();
     })->save($uploadPath . '/' . $fileName);
     ## Delete original file
     unlink($file->getRealPath());
     ## Gallery - none, existed, new
     $gallery_id = 0;
     if (is_int($gallery) && $gallery > 0) {
         $gallery_id = $gallery;
     } elseif (is_string($gallery)) {
         $gal = Gallery::create(['name' => $gallery]);
         $gallery_id = $gal->id;
     }
     ## Create MySQL record
     $photo = Photo::create(array('name' => $fileName, 'gallery_id' => $gallery_id, 'title' => $title));
     return $photo;
 }
 /**
  * VALIDATION RULE.
  *
  * Carga un documento XML de un archivo, devuelve FALSE en caso de error.
  *
  * @access public
  * @param  string  $attribute
  * @param  Symfony\Component\HttpFoundation\File\UploadedFile  $value
  * @param  array  $parameters
  * @return boolean
  *
  * @throws ErrorException
  */
 public function fileisxml($attribute, $value, $parameters)
 {
     $carga = $this->xml()->isXML($value->getRealPath());
     return $carga;
 }