I had the same task - get the image from MSSQL DB. And this post helped a little bit to get started. I ended up like this :)
public function getImageFromDB($userId) {
$storage_folder = 'emploees/';
$the_file = $storage_folder . $userId.'.jpg';
// check if we have an file
if (Storage::disk('local')->has($the_file)) {
// all we need is path, the rest will be done by Image lib
$file_content = $the_file;
} else {
// no file - try to get it from db
$db = DB::connection('somesqlconnection')->getPdo();
$stmt = $db->prepare("SELECT Photo FROM UsersTable WHERE userId = ?");
$stmt->execute([$userId]);
$stmt->bindColumn('Photo', $lob, $db::PARAM_LOB);
$stmt->fetch($db::FETCH_BOUND);
if (strlen($lob) > 0) {
Storage::disk('local')->put($the_file, $lob);
$file_content = $lob;
} else {
Log::debug('No image for #'. $userId );
// getting default image
$file_content = $storage_folder.'unknown.jpg';
}
}
// skips this if for some reason file content is empty
if ($file_content) {
// lets put Intervention/Image into work
// this should result in resized and cached thumbnail
$img = Image::cache(function($image) use ($file_content) {
$image->make($file_content)->resize(100, null, function ($constraint) {
$constraint->aspectRatio();
});
}, 10, true);
return Image::make($img)->response('jpg');
}
}
edit: actually Intervention/Image can make image even from some image path, so if we have an image on a filesystem and know exact place we can use it, there is no need to read the file and send the content
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community