

[Golang] Resize Image From Web
source link: http://siongui.github.io/2017/01/13/go-resize-image-from-web/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

[Golang] Resize Image From Web
Updated: March 15, 2017
This post gives an example that read an png image from website (use Google log as example) and resize it in half.
Install the image resizing library (github.com/nfnt/resize) first:
$ go get github.com/nfnt/resize
The full example:
web.go | repository | view raw
package resize import ( "github.com/nfnt/resize" "image/png" "io" "os" ) func ResizePng(r io.Reader, filepath string) { // decode png into image.Image img, err := png.Decode(r) if err != nil { panic(err) } // resize to width 60 using Lanczos resampling // and preserve aspect ratio m := resize.Resize(60, 0, img, resize.Lanczos3) out, err := os.Create(filepath) if err != nil { panic(err) } defer out.Close() // write new image to file png.Encode(out, m) }
web_test.go | repository | view raw
package resize import ( "net/http" "testing" ) func TestResizePng(t *testing.T) { // open image on web resp, err := http.Get("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png") if err != nil { t.Error(err) return } defer resp.Body.Close() ResizePng(resp.Body, "test_resized.png") }
For more usage of github.com/nfnt/resize, visit its GitHub page. If your image is of JPEG format, import image/jpeg and modify the code correspondingly.
Source code tested on: Ubuntu Linux 16.10, Go 1.8.
References:
[3]Go Resizing Images - Stack Overflow
[4]GitHub - fawick/speedtest-resize: Compare various Image resize algorithms for the Go language
[5]Guess Metadata from HTML and Converted to reStructuredText
[6]Creative coding in Go : golang
[7]Small microservice to crop images on-the-fly : golang
[8]Building a Cloudinary clone microservice. Would Go be a good choice? : golang
Recommend
-
23
How to resize a SwiftUI Image and keep its aspect ratio 17 Mar 2021 ⋅ 3 min read ⋅ SwiftUI
-
34
How to resize an image view to fit a container view in SwiftUI 14 Apr 2021 ⋅ 3 min read ⋅ SwiftUI
-
23
How to resize and position an image in UIImageView using contentMode 29 Mar 2021 ⋅ 3 min read ⋅ UIKit
-
17
Upload and resize an image natively with ASP.NET Core ?️ Written by Thomas Ardal, June 22, 2021 I have blogged about upload and resizing with ASP.NET Core...
-
5
How to Resize and Crop an Image Using Microsoft Paint By Nolen Jonker Published 1 day ago Microsoft Paint may be incredibly basic...
-
11
XDA Basics: How to resize an image on your Windows PC or MacWe’ve all been there: Trying to upload an image, sometimes as part of a big form, and the website where you need to upload it to has an annoying limit on file size. It can b...
-
9
It is very easy to show resized or cropped version of an image on a website using CSS. However, this doesn't change the original image data. What if you want to create an actual cropped or resized version of image for your visitors or clients...
-
4
Laravel 8 - Image Resize Before Upload 1076 views 6 months ago Laravel I am able to explain one by one article...
-
4
Learn How to Use Lambda Layers by Building an Image Resize Function Using SharpSwapnil M ManeMay 02, 2022Lambda Layer is a true gem in the Lambda function realm. It provides a conveni...
-
6
How to Resize an Image in Premiere Pro By Danny Maiorca Published 12 hours ago Working on video projects in Premiere Pro...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK