mirror of
https://github.com/talgo-cloud/bimg.git
synced 2026-03-15 02:15:54 -07:00
feat(docs): add API and examples
This commit is contained in:
parent
3904953399
commit
0d13e86634
2 changed files with 101 additions and 17 deletions
105
README.md
105
README.md
|
|
@ -58,7 +58,7 @@ bimg performance tests coming soon!
|
||||||
|
|
||||||
## API
|
## API
|
||||||
|
|
||||||
### Example
|
### Examples
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
|
|
@ -66,18 +66,85 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"gopkg.in/h2non/bimg.v0"
|
"gopkg.in/h2non/bimg.v0"
|
||||||
)
|
)
|
||||||
|
```
|
||||||
|
|
||||||
options := bimg.Options{
|
#### Resize
|
||||||
Width: 800,
|
|
||||||
Height: 600,
|
|
||||||
Crop: true,
|
|
||||||
Quality: 95,
|
|
||||||
}
|
|
||||||
|
|
||||||
newImage, err := bimg.Resize(image, options)
|
```go
|
||||||
|
buffer, err := bimg.Read("image.jpg")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintln(os.Stderr, err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
newImage, err := bimg.NewImage(buffer).Resize(800, 600)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
size, err := bimg.NewImage(newImage).Size()
|
||||||
|
if size.Width == 400 && size.Height == 300 {
|
||||||
|
fmt.Println("The image size is valid")
|
||||||
|
}
|
||||||
|
|
||||||
|
bimg.Write("new.jpg", newImage)
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Rotate
|
||||||
|
|
||||||
|
```go
|
||||||
|
buffer, err := bimg.Read("image.jpg")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
newImage, err := bimg.NewImage(buffer).Rotate(90)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
bimg.Write("new.jpg", newImage)
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Convert
|
||||||
|
|
||||||
|
```go
|
||||||
|
buffer, err := bimg.Read("image.jpg")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
newImage, err := bimg.NewImage(buffer).Convert(bimg.PNG)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if bimg.NewImage(newImage).Type() == "png" {
|
||||||
|
fmt.Fprintln(os.Stderr, "The image was converted into png")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Process
|
||||||
|
|
||||||
|
```go
|
||||||
|
options := bimg.Options{
|
||||||
|
Width: 800,
|
||||||
|
Height: 600,
|
||||||
|
Crop: true,
|
||||||
|
Quality: 95,
|
||||||
|
Rotate: 180,
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer, err := bimg.Read("image.jpg")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
newImage, err := bimg.NewImage(buffer).Process(options)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
bimg.Write("new.jpg", newImage)
|
||||||
```
|
```
|
||||||
|
|
||||||
#### func DetermineImageTypeName
|
#### func DetermineImageTypeName
|
||||||
|
|
@ -86,6 +153,12 @@ if err != nil {
|
||||||
func DetermineImageTypeName(buf []byte) string
|
func DetermineImageTypeName(buf []byte) string
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### func Initialize
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Initialize()
|
||||||
|
```
|
||||||
|
|
||||||
#### func IsTypeNameSupported
|
#### func IsTypeNameSupported
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
|
@ -110,6 +183,12 @@ func Read(path string) ([]byte, error)
|
||||||
func Resize(buf []byte, o Options) ([]byte, error)
|
func Resize(buf []byte, o Options) ([]byte, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### func Shutdown
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Shutdown()
|
||||||
|
```
|
||||||
|
|
||||||
#### type Angle
|
#### type Angle
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
|
@ -291,7 +370,7 @@ const (
|
||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
#### func DetermineImageType
|
#### func DetermineImageType
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func DetermineImageType(buf []byte) ImageType
|
func DetermineImageType(buf []byte) ImageType
|
||||||
|
|
@ -340,14 +419,6 @@ type Options struct {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
#### type Vips
|
|
||||||
|
|
||||||
```go
|
|
||||||
type Vips struct {
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
MIT - Tomas Aparicio
|
MIT - Tomas Aparicio
|
||||||
|
|
|
||||||
|
|
@ -53,6 +53,19 @@ func TestImageConvert(t *testing.T) {
|
||||||
Write("fixtures/test_image_convert_out.png", buf)
|
Write("fixtures/test_image_convert_out.png", buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestImageMetadata(t *testing.T) {
|
||||||
|
data, err := initImage("test.png").Metadata(PNG)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Cannot process the image: %#v", err)
|
||||||
|
}
|
||||||
|
if data.Alpha != true {
|
||||||
|
t.Fatal("Invalid alpha channel")
|
||||||
|
}
|
||||||
|
if data.Size.Width != 400 {
|
||||||
|
t.Fatal("Invalid width size")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func initImage(file string) *Image {
|
func initImage(file string) *Image {
|
||||||
buf, _ := Read(path.Join("fixtures", file))
|
buf, _ := Read(path.Join("fixtures", file))
|
||||||
return NewImage(buf)
|
return NewImage(buf)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue