Skip to content

Instantly share code, notes, and snippets.

@Miri92
Created February 5, 2019 15:31
Show Gist options
  • Save Miri92/9e80f507307fff61dff319de9a3a3619 to your computer and use it in GitHub Desktop.
Save Miri92/9e80f507307fff61dff319de9a3a3619 to your computer and use it in GitHub Desktop.
Laravel gallery trait
<?php
namespace App\Traits;
use App\Models\Elan;
use App\Models\ElanField;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Response;
use Intervention\Image\Facades\Image;
trait GalleryTrait {
//your code here
public static function setFeaturedImage($post_id){
$get_first_gallery = ElanField::where([
['elan_id', '=', $post_id],
['field_key', '=', 'gallery']
])->first();
//dd($get_first_gallery);
if ($get_first_gallery && count($get_first_gallery) > 0) {
Elan::where('id', '=', $post_id)
->update([
'featured_image' => $get_first_gallery->field_value,
]);
}
}
public static function updateFeaturedImage($file, $post_id, $old_name){
$dir = public_path('/') . config('app.gallery_upload_path');
$get_width = config('app.image.large.width');
$get_height = config('app.image.large.height');
if (file_exists($dir)) {
$destinationPath = $dir;
} else {
echo "gallery folder not found - ". $dir;
return Response::json('error', 400);
}
//first delete old image
//$this->delete();
if (!empty($old_name)){
self::delete_file($old_name, $post_id);
}
//dd($file);
//$filename = $file->getClientOriginalName();
$image_name = time() . '_' . $post_id.'.jpg';
$location_temp = $file->getPathName();
$location = $dir.$image_name;
//$move = $file->move($destinationPath, $image_name);
$move = Image::make($location_temp)->fit($get_width, $get_height, function ($constraint) {
$constraint->upsize();
})->save($location);
if ($move) {
Elan::where('id', '=', $post_id)
->update([
'featured_image' => $image_name,
]);
return $post_id.'new success - '.$image_name;
//return $destinationPath . $image_name;
} else {
return Response::json('error', 400);
}
}
//store or update
public static function ImageGalleryStoreOrUpdate($files, $post_id) {
$dir = public_path('/') . config('app.gallery_upload_path');
if (file_exists($dir)) {
$destinationPath = $dir;
} else {
echo "gallery folder not found - ". $dir;
return Response::json('error', 400);
}
$get_width = config('app.image.large.width');
$get_height = config('app.image.large.height');
foreach ($files as $file) {
//dd($file->getPathName());
$filename = $file->getClientOriginalName();
$image_name = time() . '_' . $post_id . '.jpg';
//return $img->response('jpg');
//dd($img->response('jpg'));
// to do: fayl tipi yazmaq laizmdir
//$image_name = $temporary_id . time();
//echo print_r($image_name);
//$upload_success = $file->move($destinationPath, $image_name);
$location_temp = $file->getPathName();
$location = $dir.$image_name;
$upload_success = Image::make($location_temp)->fit($get_width, $get_height, function ($constraint) {
$constraint->upsize();
})->save($location);
if ($upload_success) {
$elan_field = [
'elan_id' => $post_id,
'field_key' => 'gallery',
'field_value' => $image_name,
];
$post = ElanField::create($elan_field);
//echo $post_id.'new success - '.$image_name;
//return $destinationPath . $image_name;
// if ($post) {
// }
//return 'test1';
$response = array('status'=> 'success', 'id' => $post->id);
echo json_encode($response);
} else {
return Response::json('error', 400);
}
}
}
/*
* @to-do: delete directory too
*/
public static function delete($id){
$find = ElanField::where(
'id', $id
)->first();
if (!$find){
return 'gallery not found';
}
$g_name = $find->field_value;
ElanField::where('id', $id)->delete();
self::delete_file($g_name, $id);
}
// delete file from only diroctory
public static function delete_file($g_name, $id){
$dir = public_path('/') . config('app.gallery_upload_path');
$file_on_hosting = $dir . $g_name;
if ( File::exists( $file_on_hosting ) )
{
File::delete( $file_on_hosting );
} else {
return 'file not found in folder';
}
return 'succesful deleted'.$id;
}
public static function update_galleries_id($post_id, $temporary_id){
$gallery = ElanField::where([
['elan_id', '=', $temporary_id],
['field_key', '=', 'gallery']
]);
if ($gallery && count($gallery) > 0) {
$gallery->update([
'elan_id' => $post_id,
'status' => 'ACTIVE'
]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment