Oct 2, 2024
0
Level 1
A specific image not uploading using Intervention Image
Here is the link of image, which I am trying to upload it been redirect to me HTTP 500 and not working. Instead of throwing any error or exception.
Here is my code you can check.
public static function fileUpload($file,$path,$thumb,$allowExtension){
try{
$extension = $file->extension();
$orgName = $file->getClientOriginalName();
$length = strlen($orgName);
// Define how many characters you want from the end (including extension)
$charactersFromEnd = 10;
// Get the substring from the end
if ($length > $charactersFromEnd) {
$shortName = substr($orgName, -$charactersFromEnd);
} else {
$shortName = $orgName;
}
$shortName = str_replace(array('\'',',',' ','%','/','(',')',':','-'),'_',$shortName);
$shortName = str_replace('___','_',$shortName);
$shortName = str_replace('__','_',$shortName);
$newNameImage = rand().str_replace(' ','_',$shortName);
// $newNameImage = rand().str_replace(' ','_',$orgName);
$response = array();
if(in_array($extension,$allowExtension)){
$errorMsg = '';
$getFile = $file->getPathName();
if(in_array($extension,array('jpg','png','gif','bmp','webp','JPG','PNG','GIF','BMP','WebP','jpehg'))){
if(!File::exists($path)){ File::makeDirectory($path, 0775, true, true); }
try {
$img = Image::make($getFile)->orientate()->save($path.$newNameImage);
prd( $img);
} catch (\Throwable $e) {
prd($e->getMessage());
$errorMsg = str_replace($path,'',$e->getMessage());
$response['error'] = $errorMsg;
return $response;
}catch (\NotReadableException $e) {
return response()->json(['error' => 'Image file could not be read.']);
} catch (\InvalidArgumentException $e) {
return response()->json(['error' => 'Invalid argument provided.']);
} catch (\NotSupportedException $e) {
return response()->json(['error' => 'Image format not supported.']);
} catch (\RuntimeException $e) {
return response()->json(['error' => 'A runtime error occurred.']);
}
if(!empty($thumb)){
foreach ($thumb as $t => $ratio) {
if(!empty($ratio['w']) && !empty($ratio['h'])){
$savePath = $path.$ratio['w'].'X'.$ratio['h'];
if(!File::exists($savePath)){ File::makeDirectory($savePath, 0775, true, true); }
$imageDir = $savePath.'/'.$newNameImage;
try {
Image::make($getFile)->resize($ratio['w'], $ratio['h'])->orientate()->save($imageDir);
} catch (\Exception $e) {
$errorMsg = str_replace($path,'',$e->getMessage());
}
}
}
}
}
else
{
if(!File::exists($path)){ File::makeDirectory($path, 0775, true, true); }
$isUpload = $file->move($path,$newNameImage);
}
$response['orgName'] = $orgName;
$response['mediaPath'] = $newNameImage;
$response['error'] = (!empty($errorMsg))?$errorMsg:'';
}
else{
$response['error'] = 'Invalid extension file. Allowed extension is '.implode(',',$allowExtension);
}
return $response;
}catch(Exception $e){
prd($e->getMessage());
}
}
Please or to participate in this conversation.