|
<?php |
|
|
|
namespace App\Traits\Image; |
|
|
|
use App\Models\ImageUpload; |
|
use Carbon\Carbon; |
|
use Illuminate\Support\Str; |
|
use Intervention\Image\Facades\Image; |
|
|
|
trait ImageTrait { |
|
|
|
|
|
public function uploadImage($photo, $dimensions = null){ |
|
|
|
|
|
$allowedfileExtension = ['jpg', 'png', 'bmp', 'jpeg']; |
|
|
|
$extension = $photo->getClientOriginalExtension(); |
|
|
|
$extension = strtolower($extension); |
|
|
|
$size = $photo->getClientSize(); |
|
|
|
if ($size > 600000) { |
|
return [false, 'Your passport must be of types : jpeg,bmp,png,jpg.']; |
|
} |
|
|
|
if($dimensions!==null){ |
|
try{ |
|
$height = Image::make($photo)->height(); |
|
$width = Image::make($photo)->width(); |
|
if($width!==$dimensions[0]){ |
|
return [false, 'Image width does not meet Specifications']; |
|
} |
|
|
|
if($height!==$dimensions[1]){ |
|
return [false, 'Image Heifht does not meet Specifications']; |
|
} |
|
}catch (\Exception $e){ |
|
|
|
} |
|
} |
|
|
|
$time = Carbon::now(); |
|
|
|
$check = in_array(strtolower($extension), $allowedfileExtension); |
|
|
|
$filename = Str::random(5) . date_format($time, 'd') . rand(1, 9) . date_format($time, 'h') . "." . $extension; |
|
|
|
if ($check) { |
|
|
|
$directory = 'data/uploads'; |
|
$url = $directory . '/' . $filename; |
|
|
|
$photo->move(public_path($directory),$filename); |
|
//store to thumb |
|
|
|
return [true,$url]; |
|
|
|
} else { |
|
|
|
return [false, 'Your passport must be of types : jpeg,bmp,png,jpg.']; |
|
|
|
} |
|
|
|
} |
|
|
|
public function quickUpload($photo){ |
|
$allowedfileExtension = ['jpg', 'png', 'bmp', 'jpeg']; |
|
|
|
$extension = $photo->getClientOriginalExtension(); |
|
|
|
$extension = strtolower($extension); |
|
|
|
$size = $photo->getSize(); |
|
$fileNameOnly = pathinfo($photo->getClientOriginalName(), PATHINFO_FILENAME); |
|
$fileName = strtolower($this->rename($fileNameOnly, $extension)); |
|
|
|
if ($size > 600000) { |
|
return [false, 'Your passport must be of types : jpeg,bmp,png,jpg.']; |
|
} |
|
|
|
$time = Carbon::now(); |
|
|
|
$check = in_array(strtolower($extension), $allowedfileExtension); |
|
|
|
$filename = Str::random(5) . date_format($time, 'd') . rand(1, 9) . date_format($time, 'h') . "." . $extension; |
|
|
|
if ($check) { |
|
|
|
//make thumb before moving file |
|
$des = "data/thumb/thumb_{$filename}"; |
|
// $thumb = $this->makeThumb($photo, $des, $this->setCP(20000, $size), $sizes[1], $sizes[0]); |
|
$thumb = $this->makeThumb($photo, $des, 40); |
|
|
|
$directory = 'data/uploads'; |
|
$url = $directory . '/' . $filename; |
|
|
|
// $photo->move(public_path($directory),$filename); |
|
$image_url = $this->makeThumb($photo, $url, 80); |
|
|
|
//store |
|
$uuid = (string)Str::uuid(); |
|
$imgData['url'] = $image_url; |
|
$imgData['thumb_url'] = $thumb; |
|
$imgData['uuid'] = $uuid; |
|
$imgData['ext'] = $extension; |
|
$imgData['original_name'] = $fileNameOnly; |
|
$imgData['name'] = $fileName; |
|
$imgData['size'] = $size; |
|
$imgData['valid'] = true; |
|
ImageUpload::create($imgData); |
|
|
|
|
|
return [true,$uuid]; |
|
|
|
} else { |
|
|
|
return [false, 'Your passport must be of types : jpeg,bmp,png,jpg.']; |
|
|
|
} |
|
} |
|
|
|
public function makeThumb($photo, $destination, $percentage){ |
|
|
|
try{ |
|
//requires ext- gd to work |
|
|
|
$info = getimagesize($photo); |
|
if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($photo); |
|
elseif ($info['mime'] == 'image/gif') $image = imagecreatefromgif($photo); |
|
elseif ($info['mime'] == 'image/png') $image = imagecreatefrompng($photo); |
|
imagejpeg($image, $destination, $percentage); |
|
return $destination; |
|
|
|
}catch (\Exception $err){ |
|
|
|
return null; |
|
} |
|
|
|
} |
|
|
|
public function rename($name, $ext){ |
|
$exist = ImageUpload::where('original_name', $name) |
|
->where('ext', $ext) |
|
->get(); |
|
|
|
if(count($exist) > 0){ |
|
$count = $exist->count(); |
|
$newName = $name."_$count.".$ext; |
|
return $newName; |
|
}else{ |
|
return $name.".".$ext; |
|
} |
|
|
|
//check number of filename and extension in folder |
|
} |
|
|
|
public function unlinkFile($url){ |
|
if(!empty($url)){ |
|
try{ |
|
unlink($url); |
|
}catch (\Exception $er){ |
|
|
|
} |
|
} |
|
} |
|
|
|
} |