url.go (1209B)
1 // Copyright 2018 The Ebiten Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package ebitenutil 16 17 import ( 18 "image" 19 "net/http" 20 21 "github.com/hajimehoshi/ebiten/v2" 22 ) 23 24 // NewImageFromURL creates a new ebiten.Image from the given URL. 25 // 26 // Image decoders must be imported when using NewImageFromURL. For example, 27 // if you want to load a PNG image, you'd need to add `_ "image/png"` to the import section. 28 func NewImageFromURL(url string) (*ebiten.Image, error) { 29 res, err := http.Get(url) 30 if err != nil { 31 return nil, err 32 } 33 defer res.Body.Close() 34 35 img, _, err := image.Decode(res.Body) 36 if err != nil { 37 return nil, err 38 } 39 40 eimg := ebiten.NewImageFromImage(img) 41 return eimg, nil 42 }