PageRenderTime 36ms CodeModel.GetById 22ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/app/Laravel/Services/ImageUploader.php

https://bitbucket.org/cityserv/techreportph
PHP | 142 lines | 78 code | 30 blank | 34 comment | 12 complexity | 0358862300d01e225dae059b2825b10d MD5 | raw file
  1<?php 
  2
  3namespace App\Laravel\Services;
  4
  5/*
  6*
  7* Models used for this class
  8*/
  9
 10/*
 11*
 12* Classes used for this class
 13*/
 14use Carbon, Str, File, Image, AzureStorage, URL;
 15
 16class ImageUploader {
 17
 18	/**
 19	*
 20	*@param App\Http\Requests\RequestRequest $request
 21	*@param string $request
 22	*
 23	*@return array
 24	*/
 25	public static function upload($file, $image_directory = "uploads"){
 26		
 27		$storage = env('IMAGE_STORAGE', "file");
 28
 29		switch (Str::lower($storage)) {
 30			case 'file':
 31				// $file = $request->file("file");
 32				$ext = $file->getClientOriginalExtension();
 33				$thumbnail = ['height' => 500, 'width' => 500];
 34				$path_directory = $image_directory."/".Carbon::now()->format('Ymd');
 35				$resized_directory = $image_directory."/".Carbon::now()->format('Ymd')."/resized";
 36				$thumb_directory = $image_directory."/".Carbon::now()->format('Ymd')."/thumbnails";
 37
 38				if (!File::exists($path_directory)){
 39					File::makeDirectory($path_directory, $mode = 0777, true, true);
 40				}
 41
 42				if (!File::exists($resized_directory)){
 43					File::makeDirectory($resized_directory, $mode = 0777, true, true);
 44				}
 45
 46				if (!File::exists($thumb_directory)){
 47					File::makeDirectory($thumb_directory, $mode = 0777, true, true);
 48				}
 49
 50				$filename = Helper::create_filename($ext);
 51
 52				$file->move($path_directory, $filename); 
 53				if(in_array($ext, ['jpg','png','jpeg','gif'])){
 54					Image::make("{$path_directory}/{$filename}")->save("{$resized_directory}/{$filename}",95);
 55					Image::make("{$path_directory}/{$filename}")->crop($thumbnail['width'],$thumbnail['height'])->save("{$thumb_directory}/{$filename}",95);
 56				}
 57
 58				return [ "path" => $image_directory, "directory" => URL::to($path_directory), "filename" => $filename ];
 59			break;
 60
 61			case 'azure':
 62				// $file = $request->file('file');
 63				$ext = $file->getClientOriginalExtension();
 64				$thumbnail = ['height' => 500, 'width' => 500];
 65
 66				$path_directory = "{$image_directory}/".Carbon::now()->format('Ymd');
 67				$resized_directory = "{$image_directory}/".Carbon::now()->format('Ymd')."/resized";
 68				$thumb_directory = "{$image_directory}/".Carbon::now()->format('Ymd')."/thumbnails";
 69
 70				if (!File::exists($path_directory)){
 71					File::makeDirectory($path_directory, $mode = 0777, true, true);
 72				}
 73
 74				if (!File::exists($resized_directory)){
 75					File::makeDirectory($resized_directory, $mode = 0777, true, true);
 76				}
 77
 78				if (!File::exists($thumb_directory)){
 79					File::makeDirectory($thumb_directory, $mode = 0777, true, true);
 80				}
 81
 82				$filename = Helper::create_filename($ext);
 83				$new_image_filename = $filename;
 84				$file->move($path_directory, $filename); 
 85
 86				// if(Image::make("{$path_directory}/{$filename}")->width() > Image::make("{$path_directory}/{$filename}")->height()){
 87				// 	Image::make("{$path_directory}/{$filename}")->resize(null, 512, function ($constraint) {
 88				// 	    $constraint->aspectRatio();
 89				// 	})->orientate()->save("{$resized_directory}/{$filename}",95);
 90				// 	Image::make("{$path_directory}/{$filename}")->resize(null, 256, function ($constraint) {
 91				// 	    $constraint->aspectRatio();
 92				// 	})->orientate()->save("{$thumb_directory}/{$filename}",90);
 93
 94				// }else{
 95				// 	Image::make("{$path_directory}/{$filename}")->resize(512, null, function ($constraint) {
 96				// 	    $constraint->aspectRatio();
 97				// 	})->orientate()->save("{$resized_directory}/{$filename}",95);
 98				// 	Image::make("{$path_directory}/{$filename}")->resize(256, null, function ($constraint) {
 99				// 	    $constraint->aspectRatio();
100				// 	})->orientate()->save("{$thumb_directory}/{$filename}",90);
101				// }
102
103				if(in_array($ext, ['jpg','png','jpeg','gif'])){
104					Image::make("{$path_directory}/{$filename}")->save("{$resized_directory}/{$filename}",95);
105					Image::make("{$path_directory}/{$filename}")->crop($thumbnail['width'],$thumbnail['height'])->save("{$thumb_directory}/{$filename}",95);
106				}
107				
108				$client = new AzureStorage(env('BLOB_STORAGE_URL'),env('BLOB_ACCOUNT_NAME'),env('BLOB_ACCESS_KEY'));
109				
110				$container= env('BLOB_CONTAINER');
111				$orig_container = env('BLOB_ORIG_CONTAINER');
112				$directory = env('BLOB_STORAGE_URL')."/".env('BLOB_CONTAINER');
113
114				// $new_image_directory = "{$directory}/{$path_directory}";
115				// $new_image_path = "{$path_directory}";
116
117				$new_image_directory = "{$directory}/".str_replace("uploads/", "", $path_directory); 
118				$new_image_path = str_replace("uploads/", "", $path_directory);
119				
120				$client->putBlob($orig_container, "{$new_image_path}/{$filename}", "{$path_directory}/{$filename}");
121				$client->putBlob($container, "{$new_image_path}/thumbnails/{$filename}", "{$path_directory}/thumbnails/{$filename}");
122				$client->putBlob($container, "{$new_image_path}/resized/{$filename}", "{$path_directory}/resized/{$filename}");
123			
124				if (File::exists("{$path_directory}/{$filename}")){
125					File::delete("{$path_directory}/{$filename}");
126				}
127				if (File::exists("{$path_directory}/thumbnails/{$filename}")){
128					File::delete("{$path_directory}/thumbnails/{$filename}");
129				}
130				if (File::exists("{$path_directory}/resized/{$filename}")){
131					File::delete("{$path_directory}/resized/{$filename}");
132				}
133
134				return [ "path" => $new_image_path, "directory" => $new_image_directory, "filename" => $new_image_filename ];
135			break;
136			
137			default:
138				return array();
139			break;
140		}
141	}
142}