Skip to content

Instantly share code, notes, and snippets.

@scf37
scf37 / imageOrientation.ts
Last active September 5, 2023 16:43
Rotate image preview to compensate for EXIF orientation (Javascript / Typescript)
// Based on: https://stackoverflow.com/a/46814952/283851
// Based on: https://gist.github.com/mindplay-dk/72f47c1a570e870a375bd3dbcb9328fb
/**
* Create a Base64 Image URL, with rotation applied to compensate for EXIF orientation, if needed.
*
* Optionally resize to a smaller maximum width - to improve performance for larger image thumbnails.
*/
export function getImageUrl(file: File, maxWidth: number|undefined): Promise<string> {
return readOrientation(file).then(orientation => {
const options = {
// background color
bgColor: 'rgba(0,0,0,0.9)',
// fade-in and fade-out duration
fadeDurationMs: 400,
// hide scroll when showing fullscreen image
hideScroll: true,
@scf37
scf37 / DigCrypt.scala
Last active March 13, 2019 12:04
Encryption in field [0..10**n) based on Feistel network.
package digcrypt
import java.nio.ByteBuffer
import java.util.Random
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec
/**
* Digit encryption.
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.JsonDeserializer
import scala.reflect.runtime.universe._
class ScalaEnumDeserializer[T](implicit tag: TypeTag[T]) extends JsonDeserializer[T] {
private[this] val values: Map[String, T] = {
val knownSubclasses = tag.tpe.typeSymbol.asClass.knownDirectSubclasses.filter(_.isModuleClass)
@scf37
scf37 / simulate_scanned_PDF.md
Created April 11, 2018 11:49
Simulate a scanned PDF with ImageMagick

Simulate a scanned PDF with ImageMagick

  1. Download and install Ghostscript and ImageMagick + Convert Module
  2. Execute the following command in the console (change the filename)
$ convert -density 200 INPUT.pdf -rotate 0.3 +noise Multiplicative -format pdf  -quality 85 -compress JPEG -colorspace gray OUTPUT.pdf

Description of the options

  • -density: set the input DPI to 200
  • -rotate: set Page rotation to 0.3 degrees
@scf37
scf37 / Bootstrap.scala
Created September 24, 2017 11:34
Bootstrap4 as scala dsl
package me.scf37.finecss
import me.scf37.finecss.dsl.Dsl
trait TestModule20 extends Dsl {
val lead = rule("lead")
.style("font-size", "1.25rem")
.style("font-weight", "300")
val display_1 = rule("display-1")
object EitherUtils {
implicit class EitherWithFilter[L, R](either: Either[L, R]) {
def withFilter(filter: R => Boolean): Either[L, R] = either.flatMap { rv =>
try {
if (filter(rv)) Right(rv) else throw new RuntimeException("withFilter returned false")
} catch {
case e: EitherException[_] => Left(e.value.asInstanceOf[L])
}
}