Support the ongoing development of Laravel.io →
posted 8 years ago
Database
Last updated 2 years ago.
0

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

Last updated 8 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

mcraz mcraz Joined 12 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.