api code global jain

This commit is contained in:
Abhishek Mali
2025-11-05 10:37:10 +05:30
commit 52fe7e2bec
2834 changed files with 1784903 additions and 0 deletions

148
app/Traits/ImageUpload.php Normal file
View File

@@ -0,0 +1,148 @@
<?php
namespace App\Traits;
use App\Constant\Constant;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Log;
/**
*
* All the image related functions are in this trait
*
* Trait ImageUpload
* @package App\Traits
*/
trait ImageUpload
{
/**
* @param $file
* @param $fileName
* @return string
* @throws \ImagickException
*/
protected function fixImageOrientation($file, $fileName): string
{
$tempLocation = sys_get_temp_dir();
$tempFilePath = $tempLocation . '/' . $fileName;
$image = new \Imagick($file->getRealpath());
$this->autoRotateImage($image);
$image->writeImage($tempFilePath);
return $tempFilePath;
}
/**
* @param $file
* @param bool $isBase64Image
* @param bool $orientation
* @param null $uploadPath
* @param null $fileName
* @return string
*/
protected function uploadFile($file, $isBase64Image = Constant::STATUS_TRUE, $orientation = Constant::STATUS_TRUE, $uploadPath = Constant::NULL, $fileName = Constant::NULL): string
{
try {
//check dir exist or not , if dir not exist then make new directory.
//$this->dirExists($uploadPath);
if ($fileName === Constant::NULL) {
$fileName = $this->createFileName($isBase64Image, $file);
}
if ($orientation) {
$tempFile = $this->fixImageOrientation($file, $fileName);
$image = \Image::make($tempFile);
} else {
$image = \Image::make($file);
}
//Save File on Bucket - It can be storage folder or s3 bucket or any mentioned source.
saveFileOnBucket($uploadPath, $uploadPath . $fileName, $image->stream());
if ($orientation && !isset($tempFile) && !empty($tempFile)) { // Remove temp file which is created for image rotation
unlink($tempFile);
}
} catch (\Exception $ex) {
Log::error($ex);
}
return $fileName;
}
/**
* @param $file
* @param bool $isBase64Image
* @param bool $orientation
* @param null $uploadPath
* @param null $fileName
* @return string
*/
protected function uploadDocument($file, $isBase64Image = Constant::STATUS_TRUE,
$orientation = Constant::STATUS_TRUE, $uploadPath = Constant::NULL,
$fileName = Constant::NULL): string
{
try {
//check dir exist or not , if dir not exist then make new directory.
//$this->dirExists($uploadPath);
if ($fileName === Constant::NULL) {
$fileName = $this->createFileName($isBase64Image, $file);
}
$fileExtension = explode('.', $fileName)[1];
if ($fileExtension === 'pdf') {
//Save File on Bucket - It can be storage folder or s3 bucket or any mentioned source.
saveFileOnBucket($uploadPath, $uploadPath . $fileName, file_get_contents($file));
} else {
if ($orientation) {
$tempFile = $this->fixImageOrientation($file, $fileName);
$image = \Image::make($tempFile);
} else {
$image = \Image::make($file);
}
//Save File on Bucket - It can be storage folder or s3 bucket or any mentioned source.
saveFileOnBucket($uploadPath, $uploadPath . $fileName, $image->stream());
}
if ($orientation && !isset($tempFile) && !empty($tempFile)) { // Remove temp file which is created for image rotation
unlink($tempFile);
}
} catch (\Exception $ex) {
Log::error($ex);
}
return $fileName;
}
/**
* @param $isBase64Image
* @param $file
* @return string
*/
public function createFileName($isBase64Image, $file): string
{
$fileName = '';
try {
$fileName = rand(1, 1000) . time();
if ($isBase64Image) {
// get file extension
$img = explode(',', $file);
$ini = substr($img[0], 11);
$imageExtension = explode(';', $ini)[0] ?? 'png';
$fileName .= '.' . $imageExtension;
} else {
// set filename with extension from the file object
$fileName .= '.' . $file->getClientOriginalExtension();
}
} catch (\Exception $ex) {
Log::error($ex);
}
return $fileName;
}
}