Created
December 29, 2023 05:54
-
-
Save jack2jm/fd2bfd14d38d0aa67863865903921be1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0. Import this to controller | |
use Intervention\Image\ImageManagerStatic as Image; | |
use Storage; | |
use File; | |
1. Use this function to convert to specific ratio | |
// path like /var/www/html/testproject/storage/app/public/item_images/64c100497075c4c6c1075b89 | |
// filename - jatin.png | |
// $width, $height - pass to required size | |
// $maintain_ratio - same image size is require or not - other wise it will give $height * $image | |
// $delete - true it will delete larger file | |
self::convertImageToSpecificRatio($row['path'], $row['filename'], $width, $height, true, true); | |
2. copy this function | |
public function convertImageToSpecificRatio($dest_path, $filename, $width, $height, $maintain_ratio = true, $delete_older_thumb_file = true){ | |
try { | |
$image_resize = Image::make($dest_path . '/' . $filename); | |
if($maintain_ratio == true){ | |
$image_resize->resize($width, $height, function ($constraint) { | |
$constraint->aspectRatio(); | |
}); | |
}else{ | |
$image_resize->resize($width, $height); | |
} | |
$extension = pathinfo($filename, PATHINFO_EXTENSION); | |
$tmp_filename = pathinfo($filename, PATHINFO_FILENAME); | |
$converted_file_name = $tmp_filename . ".jpeg"; | |
unset($tmp_filename); | |
$base_path_name = $dest_path . '/thumb_'; | |
if(!$width){ | |
$image_resize->save($base_path_name . $converted_file_name); | |
}else{ | |
//update base path due to file names width added | |
$base_path_name .= $width . "_"; | |
$image_resize->save($base_path_name . $converted_file_name); | |
} | |
if($delete_older_thumb_file && $delete_older_thumb_file == true){ | |
try { | |
if(File::exists($base_path_name . $filename)) { | |
File::delete($base_path_name . $filename); | |
echo "file deleted => " . $base_path_name . $filename; | |
}else{ | |
echo "file not exisr => " . $base_path_name . $filename; | |
} | |
} catch (\Throwable $th) {} | |
} | |
unset($image_resize); | |
unset($base_path_name); | |
unset($converted_file_name); | |
//echo "img converting -> width -> " . $width . " => " . $converted_file_name . "done.\n"; | |
//dd('processing done',$filename, $extension, $converted_file_name); | |
} catch (\Throwable $th) { | |
//throw $th; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment