hakyll-images 0.4.3 → 0.4.4
raw patch · 8 files changed
+117/−6 lines, 8 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Hakyll.Images: imageMetadata :: Item Image -> Compiler Metadatas
+ Hakyll.Images: metadata :: Image -> Metadatas
Files
- CHANGELOG.md +4/−0
- hakyll-images.cabal +5/−2
- library/Hakyll/Images.hs +4/−1
- library/Hakyll/Images/Common.hs +9/−2
- library/Hakyll/Images/Metadata.hs +45/−0
- package.yaml +1/−1
- tests/Hakyll/Images/Metadata/Tests.hs +47/−0
- tests/TestSuite.hs +2/−0
CHANGELOG.md view
@@ -1,5 +1,9 @@ # Change log +## Release 0.4.4 + +* Added the `imageMetadata` compiler, to extract metadata from images. + ## Release 0.4.3 * hakyll-images now handles file extensions in a case-insensitive manner (extension of PR #4).
hakyll-images.cabal view
@@ -4,10 +4,10 @@ -- -- see: https://github.com/sol/hpack ----- hash: ff6ba07ab7468ad9c999ec58ffab35f8ef4fafb3fe2d4788536a60ef35035d8c+-- hash: b8546296308b75bbf655dd856a069155995bfa86cef044e6b1366acc46a813aa name: hakyll-images-version: 0.4.3+version: 0.4.4 synopsis: Hakyll utilities to work with images description: hakyll-images is an add-on to the hakyll package. It adds utilities to work with images, including JPEG compression. category: Web@@ -38,6 +38,7 @@ Hakyll.Images.Resize other-modules: Hakyll.Images.Common+ Hakyll.Images.Metadata Paths_hakyll_images hs-source-dirs: library@@ -57,10 +58,12 @@ other-modules: Hakyll.Images.Common.Tests Hakyll.Images.CompressJpg.Tests+ Hakyll.Images.Metadata.Tests Hakyll.Images.Resize.Tests Hakyll.Images Hakyll.Images.Common Hakyll.Images.CompressJpg+ Hakyll.Images.Metadata Hakyll.Images.Resize Paths_hakyll_images hs-source-dirs:
library/Hakyll/Images.hs view
@@ -57,6 +57,8 @@ -- Basic types and functions Image , loadImage + -- Handling metadata + , module Hakyll.Images.Metadata -- Jpg compression , JpgQuality , compressJpg @@ -73,4 +75,5 @@ import Hakyll.Images.CompressJpg import Hakyll.Images.Resize -import Hakyll.Images.Common+import Hakyll.Images.Common +import Hakyll.Images.Metadata
library/Hakyll/Images/Common.hs view
@@ -20,6 +20,7 @@ import Prelude hiding (readFile) + import Codec.Picture.Types (DynamicImage) import Codec.Picture.Saving @@ -34,6 +35,7 @@ import Hakyll.Core.Item (Item(..)) import Hakyll.Core.Writable (Writable(..)) + -- Supported (i.e. encodable) image formats data ImageFormat = Jpeg @@ -42,27 +44,30 @@ | Tiff deriving (Eq, Generic) + -- Automatic derivation of Binary instances requires Generic instance Binary ImageFormat --- Polymorphic type only to get an instance of functor. --- Do not use this type. + data Image = Image { format :: ImageFormat , image :: ByteString } deriving (Typeable) + -- When writing to disk, we ignore the image format. -- Trusting users to route correctly. instance Writable Image where -- Write the bytestring content write fp item = write fp (image <$> item) + -- Binary instance looks similar to the binary instance for a Hakyll Item instance Binary Image where put (Image fmt content) = put fmt >> put content get = Image <$> get <*> get + -- | Load an image from a file. -- This function can be combined with other compilers. -- @@ -78,6 +83,7 @@ fmt <- fromExt <$> getUnderlyingExtension return $ (Image fmt) <$> content + -- | Translation between file extensions and image formats. -- It is important to keep track of image formats because Hakyll -- compilers provides raw bytestrings and filenames. @@ -93,6 +99,7 @@ fromExt' ".tif" = Tiff fromExt' ".tiff" = Tiff fromExt' ext' = error $ "Unsupported format: " <> ext' + -- Encode images based on file extension encode :: ImageFormat -> DynamicImage -> Image
+ library/Hakyll/Images/Metadata.hs view
@@ -0,0 +1,45 @@+ +{-| +Module : Hakyll.Images.Metadata +Description : Handling image metadata +Copyright : (c) Laurent P René de Cotret, 2019 +License : BSD3 +Maintainer : laurent.decotret@outlook.com +Stability : unstable +Portability : portable +-} +module Hakyll.Images.Metadata + ( module Codec.Picture.Metadata + , imageMetadata + , metadata + ) where + +import Codec.Picture (decodeImageWithMetadata) +import Codec.Picture.Metadata + +import Hakyll.Core.Compiler (Compiler) +import Hakyll.Core.Item (Item, itemBody) + +import Hakyll.Images.Common (Image(..)) + +-- | Extract metadata from an image. This function will throw an +-- error in case of a problem. +-- +-- This function is for testing purposes. +metadata :: Image -> Metadatas +metadata im = either error snd (decodeImageWithMetadata (image im)) + + +-- | Extract metadata from an image. +-- +-- @ +-- match "*.jpg" $ do +-- route idRoute +-- compile $ do +-- im <- loadImage +-- meta <- imageMetadata +-- doSomethingWithMetadata meta +-- ... +-- @ +imageMetadata :: Item Image -> Compiler Metadatas +imageMetadata = return . metadata . itemBody
package.yaml view
@@ -1,5 +1,5 @@ name: hakyll-images -version: '0.4.3' +version: '0.4.4' github: "LaurentRDC/hakyll-images" license: BSD3 author: "Laurent P. René de Cotret"
+ tests/Hakyll/Images/Metadata/Tests.hs view
@@ -0,0 +1,47 @@+{-# LANGUAGE OverloadedStrings #-} +-------------------------------------------------------------------------------- +module Hakyll.Images.Metadata.Tests + ( tests + ) where + + +-------------------------------------------------------------------------------- +import Test.Tasty (TestTree, testGroup) +import Test.Tasty.HUnit (Assertion, assertBool, testCase) + + +-------------------------------------------------------------------------------- +import qualified Data.ByteString as B +import Data.Maybe (fromMaybe) +import Hakyll.Images +import Hakyll.Images.Common + +import qualified Hakyll.Images.Metadata as M + +import Text.Printf (printf) + +fromAssertions :: String -- ^ Name + -> [Assertion] -- ^ Cases + -> [TestTree] -- ^ Result tests +fromAssertions name = + zipWith testCase [printf "[%2d] %s" n name | n <- [1 :: Int ..]] + +testJpg :: IO Image +testJpg = Image Jpeg <$> (B.readFile "tests/data/piccolo.jpg") + +testMetadata :: Assertion +testMetadata = do + im <- testJpg + let meta = metadata im + height = fromMaybe 0 (M.lookup M.Height meta) + width = fromMaybe 0 (M.lookup M.Width meta) + + -- The following values of (width, height) = (1170, 647) are + -- specific to the "piccolo.jpg" image + assertBool "Metadata was not decoded properly" (height == 647) + assertBool "Metadata was not decoded properly" (width == 1170) + +tests :: TestTree +tests = testGroup "Hakyll.Images.Metadata.Tests" $ concat + [ fromAssertions "metadata" [ testMetadata ] + ]
tests/TestSuite.hs view
@@ -13,6 +13,7 @@ import qualified Hakyll.Images.Common.Tests import qualified Hakyll.Images.CompressJpg.Tests import qualified Hakyll.Images.Resize.Tests +import qualified Hakyll.Images.Metadata.Tests -------------------------------------------------------------------------------- @@ -21,4 +22,5 @@ [ Hakyll.Images.Common.Tests.tests , Hakyll.Images.CompressJpg.Tests.tests , Hakyll.Images.Resize.Tests.tests + , Hakyll.Images.Metadata.Tests.tests ]