Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Mirador Video
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
IIIF
Mirador
Mirador Video
Commits
0c3647b0
Commit
0c3647b0
authored
Jul 4, 2020
by
Chris Beer
Browse files
Options
Downloads
Patches
Plain Diff
Try a little harder to find a good level 0 thumbnail image
parent
ba575555
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
__tests__/src/lib/ThumbnailFactory.test.js
+17
-0
17 additions, 0 deletions
__tests__/src/lib/ThumbnailFactory.test.js
src/lib/ThumbnailFactory.js
+63
-11
63 additions, 11 deletions
src/lib/ThumbnailFactory.js
with
80 additions
and
11 deletions
__tests__/src/lib/ThumbnailFactory.test.js
+
17
−
0
View file @
0c3647b0
...
...
@@ -84,6 +84,23 @@ describe('getThumbnail', () => {
type
:
'
Image
'
,
})).
toMatchObject
({
url
:
'
xyz
'
});
});
it
(
'
uses embedded sizes to find an appropriate size
'
,
()
=>
{
const
sizes
=
[
{
height
:
25
,
width
:
25
},
{
height
:
100
,
width
:
100
},
{
height
:
125
,
width
:
125
},
{
height
:
1000
,
width
:
1000
},
];
const
obj
=
{
...(
iiifService
(
'
some-url
'
,
{},
{
profile
:
'
level0
'
,
sizes
})),
id
:
'
xyz
'
,
type
:
'
Image
'
,
};
expect
(
createImageSubject
(
obj
,
{
maxHeight
:
120
,
maxWidth
:
120
}))
.
toMatchObject
({
height
:
125
,
width
:
125
});
});
});
describe
(
'
with a IIIF service
'
,
()
=>
{
...
...
This diff is collapsed.
Click to expand it.
src/lib/ThumbnailFactory.js
+
63
−
11
View file @
0c3647b0
...
...
@@ -2,6 +2,12 @@ import { Utils } from 'manifesto.js';
import
MiradorManifest
from
'
./MiradorManifest
'
;
import
MiradorCanvas
from
'
./MiradorCanvas
'
;
/** */
function
asArray
(
value
)
{
if
(
value
===
undefined
)
return
[];
return
Array
.
isArray
(
value
)
?
value
:
[
value
];
}
/** */
function
isLevel0ImageProfile
(
service
)
{
const
profile
=
service
.
getProfile
();
...
...
@@ -32,7 +38,7 @@ function isLevel2ImageProfile(service) {
function
iiifv3ImageServiceType
(
service
)
{
const
type
=
service
.
getProperty
(
'
type
'
)
||
[];
return
(
Array
.
i
sArray
(
type
)
?
type
:
[
type
])
.
some
(
v
=>
v
.
startsWith
(
'
ImageService
'
));
return
a
sArray
(
type
).
some
(
v
=>
v
.
startsWith
(
'
ImageService
'
));
}
/** */
...
...
@@ -68,9 +74,9 @@ class ThumbnailFactory {
let
size
;
let
width
;
let
height
;
let
maxHeight
;
let
maxWidth
;
const
minDimension
=
120
;
let
maxHeight
=
minDimension
;
let
maxWidth
=
minDimension
;
const
{
maxHeight
:
requestedMaxHeight
,
maxWidth
:
requestedMaxWidth
}
=
this
.
iiifOpts
;
if
(
requestedMaxHeight
)
maxHeight
=
Math
.
max
(
requestedMaxHeight
,
minDimension
);
...
...
@@ -80,16 +86,62 @@ class ThumbnailFactory {
if
(
!
service
)
return
undefined
;
const
aspectRatio
=
resource
.
getWidth
()
&&
resource
.
getHeight
()
&&
(
resource
.
getWidth
()
/
resource
.
getHeight
());
// just bail to a static image, even though sizes might provide something better
if
(
isLevel0ImageProfile
(
service
))
{
return
ThumbnailFactory
.
staticImageUrl
(
resource
);
const
sizes
=
asArray
(
service
.
getProperty
(
'
sizes
'
));
const
serviceHeight
=
service
.
getProperty
(
'
height
'
);
const
serviceWidth
=
service
.
getProperty
(
'
width
'
);
const
target
=
(
requestedMaxWidth
&&
requestedMaxHeight
)
?
requestedMaxWidth
*
requestedMaxHeight
:
maxHeight
*
maxWidth
;
let
closestSize
=
{
default
:
true
,
height
:
serviceHeight
||
Number
.
MAX_SAFE_INTEGER
,
width
:
serviceWidth
||
Number
.
MAX_SAFE_INTEGER
,
};
/** Compare the total image area to our target */
const
imageFitness
=
(
test
)
=>
test
.
width
*
test
.
height
-
target
;
/** Look for the size that's just bigger than we prefer... */
closestSize
=
sizes
.
reduce
(
(
best
,
test
)
=>
{
const
score
=
imageFitness
(
test
);
if
(
score
<
0
)
return
best
;
return
Math
.
abs
(
score
)
<
Math
.
abs
(
imageFitness
(
best
))
?
test
:
best
;
},
closestSize
,
);
/** .... but not "too" big; we'd rather scale up an image than download too much */
if
(
closestSize
.
width
*
closestSize
.
height
>
target
*
6
)
{
closestSize
=
sizes
.
reduce
(
(
best
,
test
)
=>
(
Math
.
abs
(
imageFitness
(
test
))
<
Math
.
abs
(
imageFitness
(
best
))
?
test
:
best
),
closestSize
,
);
}
const
aspectRatio
=
resource
.
getWidth
()
&&
resource
.
getHeight
()
&&
(
resource
.
getWidth
()
/
resource
.
getHeight
());
/** Bail if the best available size is the full size.. maybe we'll get lucky with the @id */
if
(
closestSize
.
default
&&
!
serviceHeight
&&
!
serviceWidth
)
{
return
ThumbnailFactory
.
staticImageUrl
(
resource
);
}
if
(
maxHeight
&&
maxWidth
)
{
width
=
closestSize
.
width
;
height
=
closestSize
.
height
;
size
=
`
${
width
}
,
${
height
}
`
;
}
else
if
(
requestedMaxHeight
&&
requestedMaxWidth
)
{
// IIIF level 2, no problem.
if
(
isLevel2ImageProfile
(
service
))
{
size
=
`!
${
maxWidth
}
,
${
maxHeight
}
`
;
...
...
@@ -107,11 +159,11 @@ class ThumbnailFactory {
height
=
maxHeight
;
if
(
aspectRatio
)
width
=
Math
.
round
(
maxHeight
*
aspectRatio
);
}
}
else
if
(
m
axHeight
&&
!
m
axWidth
)
{
}
else
if
(
requestedM
axHeight
&&
!
requestedM
axWidth
)
{
size
=
`,
${
maxHeight
}
`
;
height
=
maxHeight
;
if
(
aspectRatio
)
width
=
Math
.
round
(
maxHeight
*
aspectRatio
);
}
else
if
(
!
m
axHeight
&&
m
axWidth
)
{
}
else
if
(
!
requestedM
axHeight
&&
requestedM
axWidth
)
{
size
=
`
${
maxWidth
}
,`
;
width
=
maxWidth
;
if
(
aspectRatio
)
height
=
Math
.
round
(
maxWidth
/
aspectRatio
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
sign in
to comment