Files
wticreatorstudio/Modules/MediaManagerPlus/Traits/MediaStore.php
T
2024-02-12 22:54:20 -05:00

240 lines
11 KiB
PHP

<?php
namespace Modules\MediaManagerPlus\Traits;
use Illuminate\Support\Facades\Storage;
use File;
use App\Traits\BunnyCDN;
use Intervention\Image\Facades\Image;
use Carbon\Carbon;
trait MediaStore
{
public function mediaUpload($media, $height = null, $length = null) {
if (isset($media)) {
$meta = $this->getMediaMetaData($media);
if ($meta['type'] == 'image') {
return $this->uploadImage($media);
}
else if ($meta['type'] == 'video') {
return $this->uploadVideo($media, $meta['ext']);
}
}
}
public function uploadImage($image, $height = null, $length = null) {
if (isset($image)) {
$orginal_name = $image->getClientOriginalName();
$host = activeFileStorage();
$image_extention = str_replace('image/','',Image::make($image)->mime());
$img_size = getimagesize($image);
$img_in_kb = round(filesize($image)/1024);
if ($host == 'AmazonS3' || $host == 'DigitalOcean' || $host == 'GoogleDrive' || $host == 'Wasabi' || $host == 'Backblaze' || $host == 'Dropbox' || $host == 'GoogleCloud' || $host == 'BunnyCDN' || $host == 'Contabo') {
$img = Image::make($image);
if ($height != null && $length != null) {
$original_width = $img_size[0];
$original_height = $img_size[1];
if ($original_width > $original_height) {
// resize the image to a width of 300 and constrain aspect ratio (auto height)
$img->resize($length, null, function ($constraint) {
$constraint->aspectRatio();
});
}
elseif ($original_width < $original_height) {
// resize the image to a height of 200 and constrain aspect ratio (auto width)
$img->resize(null, $height, function ($constraint) {
$constraint->aspectRatio();
});
}
else {
if ($length > $height) {
$img->resize(null,$length, function($constraint){
$constraint->aspectRatio();
});
}
elseif ($length < $height) {
$img->resize($height,null, function($constraint){
$constraint->aspectRatio();
});
}
else {
$img->resize($height,null, function($constraint){
$constraint->aspectRatio();
});
}
}
}
if ($host == 'DigitalOcean') {
$img_name_for_db = 'images/'.uniqid().'.'.$image_extention;
Storage::disk('do')->put($img_name_for_db, $img->stream(), 'public');
$url = Storage::disk('do')->url($img_name_for_db);
$data['file_name'] = $url;
$data['storage'] = 'do';
}
elseif ($host == 'GoogleDrive') {
$img_name_for_db = uniqid().'.'.$image_extention;
Storage::disk('google')->put($img_name_for_db, $img->stream(), 'public');
$url = Storage::disk('google')->url($img_name_for_db);
$data['file_name'] = $url;
$data['storage'] = 'google';
}
elseif ($host == 'Wasabi') {
$img_name_for_db = 'images/'.uniqid().'.'.$image_extention;
Storage::disk('Wasabi')->put($img_name_for_db, $img->stream(), 'public');
$url = Storage::disk('Wasabi')->url($img_name_for_db);
$data['file_name'] = $url;
$data['storage'] = 's3';
}
elseif ($host == 'Backblaze') {
$img_name_for_db = 'images/'.uniqid().'.'.$image_extention;
Storage::disk('b2')->put($img_name_for_db, $img->stream(), 'public');
$url = 'https://'.env('BACKBLAZE_BUCKET_NAME').'.'.env('BACKBLAZE_END_POINT').'/'.$img_name_for_db;
$data['file_name'] = $url;
$data['storage'] = 'b2';
}
elseif ($host == 'Dropbox') {
$img_name_for_db = 'images/'.uniqid().'.'.$image_extention;
Storage::disk('dropbox')->put($img_name_for_db, $img->stream(), 'public');
$url = Storage::disk('dropbox')->url($img_name_for_db);
$data['file_name'] = $url;
$data['file_dropbox'] = $img_name_for_db;
$data['storage'] = 'dropbox';
}
elseif ($host == 'GoogleCloud') {
$img_name_for_db = 'images/'.uniqid().'.'.$image_extention;
Storage::disk('gcs')->put($img_name_for_db, $img->stream(), 'public');
$url = Storage::disk('gcs')->url($img_name_for_db);
$data['file_name'] = $url;
$data['storage'] = 'gcs';
}
elseif ($host == 'BunnyCDN') {
$bunnyCDNStorage = new BunnyCDN(env('BUNNY_STORAGE_ZONE_NAME'), env('BUNNY_API_ACCESS_KEY') , env('BUNNY_STORAGE_ZONE_REGION'));
$img_name_for_db = 'images/'.uniqid().'.'.$image_extention;
$bunnyCDNStorage->uploadFile($image , "/".env('BUNNY_STORAGE_ZONE_NAME')."/".$img_name_for_db);
$file = "https://".env('BUNNY_STORAGE_ZONE_NAME').".b-cdn.net/".$img_name_for_db;
$data['file_name'] = $file;
$data['storage'] = 'bunny';
}
elseif ($host == 'Contabo') {
$img_name_for_db = 'images/'.uniqid().'.'.$image_extention;
Storage::disk('contabo')->put($img_name_for_db, $img->stream(), 'public');
$url = Storage::disk('contabo')->url($img_name_for_db);
$data['file_name'] = $url;
$data['storage'] = 'contabo';
}
else{
$img_name_for_db = 'images/'.uniqid().'.'.$image_extention;
Storage::disk('s3')->put($img_name_for_db, $img->stream(), 'public');
$url = Storage::disk('s3')->url($img_name_for_db);
$data['file_name'] = $url;
$data['storage'] = 's3';
}
}
else {
if (!File::isDirectory(asset_path('uploads/all/'))) {
File::makeDirectory(asset_path('uploads/all/'), 0777, true, true);
}
$img = Image::make($image);
if ($height != null && $length != null) {
$original_width = $img_size[0];
$original_height = $img_size[1];
if ($original_width > $original_height) {
// resize the image to a width of 300 and constrain aspect ratio (auto height)
$img->resize($length, null, function ($constraint) {
$constraint->aspectRatio();
});
}
elseif ($original_width < $original_height) {
// resize the image to a height of 200 and constrain aspect ratio (auto width)
$img->resize(null, $height, function ($constraint) {
$constraint->aspectRatio();
});
}
else {
if ($length > $height) {
$img->resize(null,$length, function($constraint){
$constraint->aspectRatio();
});
}
elseif ($length < $height) {
$img->resize($height,null, function($constraint){
$constraint->aspectRatio();
});
}
else {
$img->resize($height,null, function($constraint){
$constraint->aspectRatio();
});
}
}
}
$img_name_for_db = 'uploads/all/'.uniqid().'.'.$image_extention;
$img_name_for_file = asset_path($img_name_for_db);
$img->save($img_name_for_file);
$data['file_name'] = $img_name_for_db;
$data['storage'] = 'local';
}
$data['orginal_name'] = $orginal_name;
$data['extension'] = $image_extention;
$data['type'] = 'image';
$data['size'] = $img_in_kb;
return $data;
}
return null;
}
public function uploadVideo($video, $video_extention) {
if (isset($video)) {
$orginal_name = $video->getClientOriginalName();
$host = activeFileStorage();
$vid_in_kb = round(filesize($video)/1024);
if ($host == 'AmazonS3' || $host == 'DigitalOcean' || $host == 'GoogleDrive' || $host == 'Wasabi' || $host == 'Backblaze' || $host == 'Dropbox' || $host == 'GoogleCloud' || $host == 'BunnyCDN' || $host == 'Contabo') {
}
else {
if (!File::isDirectory(asset_path('uploads/all/'))) {
File::makeDirectory(asset_path('uploads/all/'), 0777, true, true);
}
$filename = uniqid().'.'.$video_extention;
$vid_name_for_db = 'uploads/all/videos/'.$filename;
$vid_name_for_file = asset_path($vid_name_for_db);
$video->move('public/uploads/all/videos', $filename);
$data['file_name'] = $vid_name_for_db;
$data['storage'] = 'local';
}
$data['orginal_name'] = $orginal_name;
$data['extension'] = $video_extention;
$data['type'] = 'video';
$data['size'] = $vid_in_kb;
return $data;
}
return null;
}
public function getMediaMetaData($media) {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime_type = finfo_file($finfo, $media);
finfo_close($finfo);
$file_type = explode('/', $mime_type);
if (count($file_type) > 0) {
return [
'type' => $file_type[0],
'ext' => $file_type[1]
];
}
return null;
}
}