Skip to content

Commit d2c8832

Browse files
committed
Added getImageDimensions().
1 parent ad67093 commit d2c8832

File tree

2 files changed

+50
-14
lines changed

2 files changed

+50
-14
lines changed

src/app.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,8 @@ scriptEnvironmentGlobals = {
508508
-- html = img( url [, alt="", title ] )
509509
-- html = img( url [, alt="", useAltAsTitle ] )
510510
img = function(url, alt, title)
511+
-- @Incomplete: Do we want to add 'width' and 'height' attributes here if the URL looks like a local image path?
512+
-- Maybe we want a separate function for that, like imgLocal()? Not sure...
511513
if title then
512514
return F(
513515
'<img src="%s" alt="%s" title="%s">',
@@ -598,6 +600,10 @@ scriptEnvironmentGlobals = {
598600
end
599601
end,
600602

603+
getImageDimensions = function(sitePath)
604+
return getImageDimensions(sitePathToPath(sitePath))
605+
end,
606+
601607
-- Context functions.
602608
----------------------------------------------------------------
603609

src/functions.lua

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
isArgs
3939
isFile, isDirectory
4040
isStringMatchingAnyPattern
41+
loadImage, getImageDimensions
4142
markdownToHtml
4243
newDataFolderReader, isDataFolderReader, preloadData
4344
newPage
@@ -1558,12 +1559,6 @@ end
15581559

15591560
-- thumbnailInfo = createThumbnail( imagePathRelative, thumbWidth [, thumbHeight, errorLevel=1 )
15601561
do
1561-
local imageLoaders = {
1562-
["png"] = gd.createFromPng,
1563-
["jpg"] = gd.createFromJpeg,
1564-
["jpeg"] = gd.createFromJpeg,
1565-
["gif"] = gd.createFromGif,
1566-
}
15671562
local imageCreatorMethods = {
15681563
["png"] = "pngStr",
15691564
["jpg"] = "jpegStr",
@@ -1592,16 +1587,11 @@ do
15921587
local folder = pathImageRel:sub(1, #pathImageRel-#filename) -- Ending in "/".
15931588
local pathImage = DIR_CONTENT.."/"..pathImageRel
15941589

1595-
if not isFile(pathImage) then
1596-
errorf(errLevel, "File does not exist: %s", pathImage)
1590+
local image, err = loadImage(pathImageRel)
1591+
if not image then
1592+
error(err, errLevel)
15971593
end
15981594

1599-
local loadImage = imageLoaders[extLower]
1600-
or errorf(errLevel, "Unknown image file format '%'.", extLower)
1601-
1602-
local image = loadImage(pathImage)
1603-
or errorf(errLevel, "Could not load image '%s'. Maybe the image is corrupted?", pathImage)
1604-
16051595
local imageW, imageH = image:sizeXY()
16061596
assert(imageW > 0)
16071597
assert(imageH > 0)
@@ -2218,3 +2208,43 @@ function sort(t, ...)
22182208
end
22192209

22202210

2211+
2212+
do
2213+
local imageLoaders = {
2214+
["png"] = gd.createFromPng,
2215+
["jpg"] = gd.createFromJpeg,
2216+
["jpeg"] = gd.createFromJpeg,
2217+
["gif"] = gd.createFromGif,
2218+
}
2219+
2220+
function loadImage(pathImageRel)
2221+
local filename = getFilename(pathImageRel)
2222+
local extLower = getExtension(filename):lower()
2223+
local pathImage = DIR_CONTENT.."/"..pathImageRel
2224+
2225+
if not isFile(pathImage) then
2226+
return F("File does not exist: %s", pathImage)
2227+
end
2228+
2229+
local imageLoader = imageLoaders[extLower]
2230+
if not imageLoader then
2231+
return F("Unknown image file format '%'.", extLower)
2232+
end
2233+
2234+
local image = imageLoader(pathImage)
2235+
if not image then
2236+
return F("Could not load image '%s'. Maybe the image is corrupted?", pathImage)
2237+
end
2238+
2239+
return image
2240+
end
2241+
end
2242+
2243+
function getImageDimensions(pathImageRel)
2244+
local image, err = loadImage(pathImageRel)
2245+
if not image then return nil, err end
2246+
2247+
return image:sizeXY()
2248+
end
2249+
2250+

0 commit comments

Comments
 (0)