Support the ongoing development of →
posted 8 years ago
Last updated 2 years ago.

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->bindColumn('Photo', $lob, $db::PARAM_LOB);

			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) {
			}, 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.

Sign in to participate in this thread!


Your banner here too?

mcraz mcraz Joined 12 Feb 2014


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

Your logo here?

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

© 2024 - All rights reserved.