ihp-imagemagick (empty) → 1.0.0
raw patch · 3 files changed
+132/−0 lines, 3 filesdep +basedep +bytestringdep +process
Dependencies added: base, bytestring, process, temporary, text, wai-extra
Files
- IHP/FileStorage/Preprocessor/ImageMagick.hs +65/−0
- LICENSE +21/−0
- ihp-imagemagick.cabal +46/−0
+ IHP/FileStorage/Preprocessor/ImageMagick.hs view
@@ -0,0 +1,65 @@+{-|+Module: IHP.FileStorage.Preprocessor.ImageMagick+Description: Preprocessor for images. Requires that you add @imagemagick@ to the @otherDeps@ inside the project's @default.nix.+Copyright: (c) digitally induced GmbH, 2021+-}+module IHP.FileStorage.Preprocessor.ImageMagick+( applyImageMagick+) where++import Prelude++import Data.Text (Text)+import qualified Data.Text as Text+import qualified Network.Wai.Parse as Wai+import qualified Data.ByteString.Lazy as LBS+import qualified System.IO.Temp as Temp+import qualified System.Process as Process+++-- | Converts the image to the specified output format and applies specified image magick transforms+--+-- __Example:__ Transform an uploaded image to a PNG file, resize it to 512x512 and strip meta data+--+-- > let uploadLogo = uploadToStorageWithOptions $ def+-- > { preprocess = applyImageMagick "png" ["-resize", "512x512^", "-gravity", "north", "-extent" , "512x512", "-quality", "100%", "-strip"] }+-- >+-- > company <- fetch companyId+-- > company+-- > |> fill @'["name"]+-- > |> uploadLogo #logoUrl+-- > >>= ifValid \case+-- > Left company -> render EditView { .. }+-- > Right company -> do+-- > company <- company |> updateRecord+-- > redirectTo EditCompanyAction { .. }+--+--+-- __Example:__ Transform an uploaded image to a JPEG file and strip meta data+--+-- > let uploadLogo = uploadToStorageWithOptions $ def+-- > { preprocess = applyImageMagick "jpg" ["-strip"] }+-- >+-- > company <- fetch companyId+-- > company+-- > |> fill @'["name"]+-- > |> uploadLogo #logoUrl+-- > >>= ifValid \case+-- > Left company -> render EditView { .. }+-- > Right company -> do+-- > company <- company |> updateRecord+-- > redirectTo EditCompanyAction { .. }+--+applyImageMagick :: Text -> [String] -> Wai.FileInfo LBS.LazyByteString -> IO (Wai.FileInfo LBS.LazyByteString)+applyImageMagick convertTo otherParams fileInfo = do+ Temp.withTempDirectory "/tmp" "ihp-upload" $ \tempPath -> do+ let tempFilePath = tempPath <> "/image"+ let convertedFilePath = tempPath <> "/converted." <> Text.unpack convertTo++ LBS.writeFile tempFilePath fileInfo.fileContent++ let params :: [String] = [tempFilePath] <> otherParams <> [convertedFilePath]+ Process.callProcess "magick" params++ newContent <- LBS.readFile convertedFilePath+ pure fileInfo { Wai.fileContent = newContent }
+ LICENSE view
@@ -0,0 +1,21 @@+The MIT License (MIT)++Copyright (c) 2020 digitally induced GmbH++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all+copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE+SOFTWARE.
+ ihp-imagemagick.cabal view
@@ -0,0 +1,46 @@+cabal-version: 2.2+name: ihp-imagemagick+version: 1.0.0+synopsis: ImageMagick preprocessing for IHP file uploads+description: Preprocess uploaded images with ImageMagick in IHP apps+license: MIT+license-file: LICENSE+author: digitally induced GmbH+maintainer: support@digitallyinduced.com+bug-reports: https://github.com/digitallyinduced/ihp/issues+category: Web+build-type: Simple++source-repository head+ type: git+ location: https://github.com/digitallyinduced/ihp.git++common ihp-std+ default-language: GHC2021+ default-extensions:+ NoImplicitPrelude+ OverloadedLabels+ OverloadedRecordDot+ DuplicateRecordFields+ DisambiguateRecordFields+ TypeApplications+ TemplateHaskell+ QuasiQuotes+ PackageImports+ ScopedTypeVariables+ BlockArguments+ OverloadedStrings+ ImplicitParams+ ghc-options: -Werror=incomplete-patterns -Werror=unused-imports -Werror=missing-fields++library+ import: ihp-std+ build-depends:+ base >= 4.17.0 && < 4.22+ , text+ , bytestring+ , wai-extra+ , process+ , temporary+ hs-source-dirs: .+ exposed-modules: IHP.FileStorage.Preprocessor.ImageMagick