Saturday, October 13, 2012

ImageMagick: Save for web (JPG)

Okay this is required in all web applications that allows people to upload images for viewing in browser:

convert input.jpg -profile AdobeRGB1998.icc -colorspace sRGB -auto-orient output.jpg

Notice that in above input.jpg need not to be jpg, it can be BMP/PNG... any format that imagemagick reads, it's perfect way to convert anything user uploads to the JPG.

I got a gaping wound as group of users had managed to upload bunch of JPG images with CMYK profiles! As a programmer it would be far easier if there were command like convertweb or something that would just converted any image to PNG or JPG that just works in browsers.

Friday, June 22, 2012

Scala cast if possible when getting from Map


object MyAsDefaultTest extends App {


  implicit def anyDefaultVal(theoption : Option[Any]) = {
    new {
      def asDefault[A](default: A) : A =
        try {
          default.getClass.cast(theoption.getOrElse(default))
        } catch {
          case _ => default
        }
    }
  }


  override def main(args: Array[String]) {
    val numbers = Map(1 -> 123, 2 -> 321.123, 3 -> "fail")


    println(numbers.get(1).asDefault(-1) * 3) // Returns 123
    println(numbers.get(2).asDefault(-1) * 3) // Returns -3
    println(numbers.get(3).asDefault(-1) * 3) // Returns -3
    println(numbers.get(999).asDefault(-1) * 3) // Returns -3


    println(numbers.get(3).asDefault("") + "test") // Returns "failtest"
    println(numbers.get(2).asDefault(0.0) + 100) // Returns 421.123
  }
}

Friday, January 13, 2012

iPad & iPhone targetting

/* iPad [portrait + landscape] */
@media only screen and (min-device-width: 768px) and (max-device-width: 1024px) {
 .selector-01 { margin: 10px; }
 .selector-02 { margin: 10px; }
 .selector-03 { margin: 10px; }
}

/* iPhone [portrait + landscape] */
@media only screen and (max-device-width: 480px) {
 .selector-01 { margin: 10px; }
 .selector-02 { margin: 10px; }
 .selector-03 { margin: 10px; }
}

/* == iPad/iPhone [portrait + landscape] == */
@media only screen and (min-device-width: 768px) and (max-device-width: 1024px), 
@media only screen and (max-device-width: 480px) {
 .selector-01 { margin: 10px; }
 .selector-02 { margin: 10px; }
 .selector-03 { margin: 10px; }
}
Stolen from Preishablepress