Esempio n. 1
0
func (self *Image) Resize(size string) {
	var err error

	url := to.String(self.Params.Get("url"))

	relPath := ImageRoot + tango.PS + size + tango.PS + checksum.String(fmt.Sprintf("%s/%s", size, url), crypto.SHA1) + ".png"
	fullPath := Root + tango.PS + relPath

	_, err = os.Stat(fullPath)

	if err == nil {

		app.Server.Context.Redirect("/" + relPath)

		app.Server.Context.HttpError(200)

		return

	} else {

		filePath, err := resource.Download(url)

		if err == nil {

			thumb := canvas.New()

			opened := thumb.Open(filePath)

			if opened == true {

				resize := strings.Split(size, "x")

				width, _ := strconv.Atoi(resize[0])
				height, _ := strconv.Atoi(resize[1])

				thumb.AutoOrientate()
				thumb.Thumbnail(uint(width), uint(height))

				os.MkdirAll(path.Dir(fullPath), os.ModeDir|0755)

				written := thumb.Write(fullPath)

				if written {
					app.Server.Context.Redirect("/" + relPath)
				} else {
					app.Server.Context.HttpError(500)
				}

				return
			}

		}

	}

	app.Server.Context.HttpError(404)
}
Esempio n. 2
0
func main() {
	image := canvas.New()

	// Opening some image from disk.
	err := image.Open("input/example.png")

	if err == nil {

		defer image.Destroy()

		// Photo auto orientation based on EXIF tags.
		image.AutoOrientate()

		// Creating a squared thumbnail
		image.Thumbnail(100, 100)

		// Saving the thumbnail to disk.
		image.Write("output/example-thumbnail.png")
	} else {
		fmt.Printf("Could not open image: %s\n", err)
	}
}