Image upload Code centerlize using Image Intervention lib to resize
Copy Below Code
View As A Text File
Show Text Only
Show API
Edit Code
/* Image Uploading Centerlized Code Starts */
function pb_upload_featured_img($imageArr,$folder=''){
if($folder==''){
$folder='common';
}
$originalPath= public_path('uploads/'.$folder.'/original/');
$thumbPath= public_path('uploads/'.$folder.'/thumb/');
$mediumPath= public_path('uploads/'.$folder.'/');
$returnImgArr=[];
foreach($imageArr as $value){
// dd($value);
if( ! File::isDirectory($originalPath) ) {
File::makeDirectory($originalPath, 493, true);
} // Create Directory if not exist
// Make Original Copy
File::copy('uploads/ajax_images/'.$value, $originalPath.''.$value);
$uploadedImagePath = $originalPath.$value;
$imageName = $value;
// Make Medium & Thumb Image Copy
$dimension=get_image_dimension();
$thumbD=$dimension['thumb'];
$mediumD=$dimension['medium'];
$thumbArr=['width'=>$thumbD[0],'height'=>$thumbD[1],'path'=>$thumbPath];
$mediumArr=['width'=>$mediumD[0],'height'=>$mediumD[1],'path'=>$mediumPath];
$returnImgArr[]=ajax_make_image_resize($imageName,$uploadedImagePath,$thumbArr,$mediumArr);
}
return $returnImgArr;
}
if (!function_exists('ajax_make_image_resize')) {
function ajax_make_image_resize($imageName, $oldImage, $thumbArr, $mediumArr)
{
if (!empty($imageName)) {
if( ! File::isDirectory($thumbArr['path']) ) {
File::makeDirectory($thumbArr['path'], 493, true);
} // Create Directory if not exist
// Create Thumb
$img = $thumbArr['path'] . $imageName;
Image::make($oldImage)->resize($thumbArr['width'], $thumbArr['height'])->save($img);
// Create Medium Copy
$img = $mediumArr['path'] . $imageName;
Image::make($oldImage)->resize($mediumArr['width'], $mediumArr['height'])->save($img);
return $imageName;
}
return false;
}
}
if (!function_exists('get_image_dimension')) {
function get_image_dimension()
{
$imageSize = Metadata::where('data_key', 'thumb_image_dimension')->first();
$thumb = $imageSize->val1;
$imageSize = Metadata::where('data_key', 'medium_image_dimension')->first();
$medium = $imageSize->val1;
return array('thumb' => explode(',', $thumb), 'medium' => explode(',', $medium));
}
}
if (!function_exists('pb_unlink_featured_img')){
function pb_unlink_featured_img($imageArr,$folder=''){
if($folder==''){
$folder='common';
}
if(!empty($imageArr)){
foreach($imageArr as $value){
if(!empty($value)){
$originalPath= public_path('uploads/'.$folder.'/original/').$value;
$thumbPath= public_path('uploads/'.$folder.'/thumb/').$value;
$mediumPath= public_path('uploads/'.$folder.'/').$value;
if(isset($originalPath) || isset($thumbPath) || isset($mediumPath)){
unlink($originalPath);
unlink($thumbPath);
unlink($mediumPath);
}
}
}
}
}
}
/* Image Uploading Centerlized Code Ends */