mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-22 22:13:59 +00:00
147 lines
5.0 KiB
PHP
147 lines
5.0 KiB
PHP
<?php
|
|
|
|
namespace App\Classes\Modules\Documents\Services;
|
|
|
|
use Illuminate\Support\Str;
|
|
use Image;
|
|
|
|
class Convert64ToFile
|
|
{
|
|
/**
|
|
* @param array|null file_set
|
|
* @return array|bool
|
|
*/
|
|
public function convert($file_set = [], $path = 'file')
|
|
{
|
|
$file_info = [];
|
|
$folder_path = $this->generateFolder($path);
|
|
|
|
foreach ($file_set as $key => $row) {
|
|
|
|
$file_data = $row;
|
|
$filename = (string) Str::uuid();
|
|
|
|
$f = finfo_open();
|
|
$mime_type = finfo_file($f, $file_data, FILEINFO_MIME_TYPE);
|
|
|
|
$extension = '';
|
|
switch ($mime_type) {
|
|
case 'image/gif':
|
|
$extension = 'gif';
|
|
break;
|
|
case 'image/png':
|
|
$extension = 'png';
|
|
break;
|
|
case 'image/jpeg':
|
|
$extension = 'jpg';
|
|
break;
|
|
case 'application/pdf':
|
|
$extension = 'pdf';
|
|
break;
|
|
}
|
|
|
|
if (empty($extension)) {
|
|
return false;
|
|
}
|
|
|
|
$filename_with_ext = $filename . '.' . $extension;
|
|
|
|
if ($extension != 'pdf') {
|
|
$img = Image::make($file_data)->save($folder_path . '/' . $filename_with_ext);
|
|
$file = $this->generateImages($path, $filename_with_ext, $filename, $extension);
|
|
}
|
|
else {
|
|
$file_data = explode('base64,', $file_data);
|
|
$file_data = base64_decode($file_data[1]);
|
|
$file = $this->generatePdf($path, $file_data, $filename, $extension);
|
|
}
|
|
|
|
$file_info[] = [
|
|
'path' => $path,
|
|
'filename' => $filename_with_ext,
|
|
'mime_type' => $mime_type,
|
|
'extension' => $extension,
|
|
'file_info' => empty($file) ? [] : $file,
|
|
];
|
|
}
|
|
|
|
return $file_info;
|
|
}
|
|
|
|
/**
|
|
* @param string|null path
|
|
* @param string|null pdfdata
|
|
* @param string|null filename
|
|
* @param string|null extension
|
|
*/
|
|
public static function generatePdf($path = '', $pdfdata = '', $filename = '', $extension = '')
|
|
{
|
|
$file_info = [];
|
|
$file = \File::put(storage_path($path) . '/' . $filename . '.' . $extension, $pdfdata);
|
|
$file_info['original']['file'] = 'storage/' . $path . '/' . $filename . '.' . $extension;
|
|
$file_info['large']['file'] = 'storage/' . $path . '/' . $filename . '.' . $extension;
|
|
$file_info['medium']['file'] = 'storage/' . $path . '/' . $filename . '.' . $extension;
|
|
$file_info['small']['file'] = 'storage/' . $path . '/' . $filename . '.' . $extension;
|
|
|
|
return $file_info;
|
|
}
|
|
|
|
/**
|
|
* @param string|null path
|
|
* @param string|null file
|
|
* @param string|null filename
|
|
* @param string|null extension
|
|
*/
|
|
public function generateImages($path = '', $file = '', $filename = '', $extension = '')
|
|
{
|
|
$file_info = [];
|
|
|
|
$img = Image::make(storage_path($path) . '/' . $file);
|
|
$file_info['original']['file'] = 'storage/' . $path . '/' . $file;
|
|
$file_info['original']['width'] = $img->width();
|
|
$file_info['original']['height'] = $img->height();
|
|
|
|
$img->widen(800, function ($constraint) {
|
|
$constraint->upsize();
|
|
})->heighten(800, function ($constraint) {
|
|
$constraint->upsize();
|
|
});
|
|
$img->save(storage_path($path) . '/' . $filename . '_' . 'l.' . $extension);
|
|
$file_info['large']['file'] = 'storage/' . $path . '/' . $filename . '_' . 'l.' . $extension;
|
|
$file_info['large']['width'] = $img->width();
|
|
$file_info['large']['height'] = $img->height();
|
|
|
|
$img->widen(480, function ($constraint) {
|
|
$constraint->upsize();
|
|
})->heighten(480, function ($constraint) {
|
|
$constraint->upsize();
|
|
});
|
|
$img->save(storage_path($path) . '/' . $filename . '_' . 'm.' . $extension);
|
|
$file_info['medium']['file'] = 'storage/' . $path . '/' . $filename . '_' . 'm.' . $extension;
|
|
$file_info['medium']['width'] = $img->width();
|
|
$file_info['medium']['height'] = $img->height();
|
|
|
|
$img->widen(320, function ($constraint) {
|
|
$constraint->upsize();
|
|
})->heighten(320, function ($constraint) {
|
|
$constraint->upsize();
|
|
});
|
|
$img->save(storage_path($path) . '/' . $filename . '_' . 's.' . $extension);
|
|
$file_info['small']['file'] = 'storage/' . $path . '/' . $filename . '_' . 's.' . $extension;
|
|
$file_info['small']['width'] = $img->width();
|
|
$file_info['small']['height'] = $img->height();
|
|
|
|
return $file_info;
|
|
}
|
|
|
|
/**
|
|
* @param string|null set_path
|
|
*/
|
|
public function generateFolder($set_path = '')
|
|
{
|
|
$folder = storage_path($set_path);
|
|
|
|
\File::isDirectory($folder) or \File::makeDirectory($folder, 0777, true, true); //check folder is exist
|
|
return $folder;
|
|
}
|
|
} |