media_cache/commit

Added the options to 'fit' a thumbnail to an aspect ratio.

authorChris Dijkshoorn
Mon Jun 3 10:23:00 2013 +0200
committerChris Dijkshoorn
Mon Jun 3 10:23:00 2013 +0200
commite38e44fe7013b6ede7c7b867f2207ef88d877779
tree8ce86215706e43aa0c416f704b002062182ce5e6
parenta759568f33929c43e7431f0835760222b227cbdf
Diff style: patch stat
diff --git a/api/media_caching.pl b/api/media_caching.pl
index 2e3d262..50b7c7e 100644
--- a/api/media_caching.pl
+++ b/api/media_caching.pl
@@ -11,6 +11,10 @@
 		http_thumbnail,  [spawn(media)]).
 :- http_handler(root(cache/medium),
 		http_mediumscale,  [spawn(media)]).
+:- http_handler(root(cache/fit),
+		http_fit_thumbnail,  [spawn(media)]).
+:- http_handler(root(cache/fitmedium),
+		http_medium_fit,  [spawn(media)]).
 
 %%      original(+Request)
 %
@@ -39,6 +43,21 @@ do_http_thumbnail(Size, Request) :-
         uri_thumbnail(URI, ThumbnailFile, Size),
         http_reply_file(ThumbnailFile, [unsafe(true)], Request).
 
+/* http_pan_scan(+Request)
+* provides a pan and scanned version of the image fitting given dimensions
+*/
+http_fit_thumbnail(Request)  :- do_http_fit(thumbnail_size, Request).
+http_medium_fit(Request)  :- do_http_fit(medium_size, Request).
+
+do_http_fit(Size, Request) :-
+        http_parameters(Request,
+                        [ uri(URI, [])
+                        ]),
+        debug(thumbnail, 'Do fit for ~w', [URI]),
+        uri_fit_thumbnail(URI, ThumbnailFile, Size),
+        http_reply_file(ThumbnailFile, [unsafe(true)], Request).
+
+
 %%	map_uri(+URIin, -URIout) is det.
 %
 %	Hook to map media URIs to different URIs to work around known
@@ -46,3 +65,5 @@ do_http_thumbnail(Size, Request) :-
 %
 map_uri(U,U).
 
+
+
diff --git a/lib/thumbnail.pl b/lib/thumbnail.pl
index 180e8c8..639af50 100644
--- a/lib/thumbnail.pl
+++ b/lib/thumbnail.pl
@@ -22,7 +22,8 @@
 */
 
 :- module(thumbnail,
-	  [ uri_thumbnail/3		% +URI, -File
+	  [ uri_thumbnail/3, % +URI, -File
+	    uri_fit_thumbnail/3
 	  ]).
 :- use_module(library(settings)).
 :- use_module(library(http/url_cache)).
@@ -35,14 +36,16 @@
 % Windows: 'cmd.exe /C convert.exe'
 :- setting(convert_program, atom, convert,
 	   'ImageMagic convert used to create thumbnails').
-:- setting(thumbnail_size, any, size(105,105),
+:- setting(thumbnail_size, any, size(350,300),
 	   'Term size(W,H) into which thumbnails are scaled').
-:- setting(medium_size, any, size(800,800),
+:- setting(medium_size, any, size(1280,1024),
 	   'Term size(W,H) into which medium sizes are scaled').
 :- setting(cache_directory, atom, 'cache/thumbnails',
 	   'Directory for caching thumbnails').
 :- setting(mcache_directory, atom, 'cache/mediums',
 	   'Directory for caching medium sized images').
+:- setting(fit_directory, atom, 'cache/fit',
+	   'Directory for images fitted to size').
 
 %%	uri_thumbnail(+URI, -File)
 %
@@ -60,6 +63,24 @@ uri_thumbnail(URI, File, Size) :-
 	    make_thumbnail(URI, File, Size)
 	).
 
+%%	uri_pan_scan(+URI, -File)
+%
+%       Return the panned and scanned for image at URI
+%
+%	TBD: Error recovery
+
+uri_fit_thumbnail(URI, File, Size) :-
+	setting(fit_directory, Dir0),
+	absolute_file_name(Dir0, Dir),
+	ensure_directory(Dir),
+	url_cache_file(URI, Dir, jpeg, File),
+	thread_self(Self),
+	(   exists_file(File)
+	->  debug(thumbnail, '[~w] CACHE: ~w', [Self, File])
+	;   debug(thumbnail, '[~w] Fit for ~w', [Self, File]),
+	    make_fit(URI, File, Size)
+	).
+
 %%	thumbnail_dir(-AbsDir, Size)
 %
 %	Directory for caching thunbnails.  Create if it doesn't exist.
@@ -102,6 +123,31 @@ scale(Full, File, Size) :-
 	;   format(user_error, 'FAILED: ~w', [Cmd])
 	).
 
+%%	make_pan_scan(+URI, +File) is det.
+%
+%	Create a panscan for an image located at URI in the file
+%	File.
+make_fit(URI, File, Size) :-
+	local_file_for_uri(URI, Full), !,
+	debug(thumbnail, 'Creating panscan from ~w', [Full]),
+	fit(Full, File, Size).
+make_fit(URI, File, Size) :-
+	url_cache(URI, Full, _Mime),
+	fit(Full, File, Size).
+
+fit(Full, File, Size) :-
+	setting(Size, size(W, H)),
+	setting(convert_program, Prog),
+	os_relative_path(Full, OSFull),
+	os_relative_path(File, OSFile),
+	format(string(Cmd),
+	       '"~w" "~w" -thumbnail ~wx~w^ -gravity center -extent ~wx~w "~w"',
+	       [Prog, OSFull, W, H, W, H, OSFile, W, H]),
+	debug(thumbnail, "Panscan command~p", [Cmd]),
+	(   run(Cmd)
+	->  true
+	;   format(user_error, 'FAILED: ~w', [Cmd])
+	).
 
 %%	run(+Command) is det.
 %