Skip to content

Commit

Permalink
Add #gtGetPng: [#2978]
Browse files Browse the repository at this point in the history
  • Loading branch information
chisandrei committed Jan 6, 2023
1 parent 9497e21 commit 42267c9
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/GToolkit-Extensions/ZnEasy.extension.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Extension { #name : #ZnEasy }

{ #category : #'*GToolkit-Extensions' }
ZnEasy class >> gtGetImageOfType: mimeType fromUrl: urlObject [
| client readStream pictureBytes |
(client := self client)
url: urlObject;
accept: mimeType;
enforceHttpSuccess: true;
enforceAcceptContentType: true;
get.

readStream := client entity readStream.
pictureBytes := readStream upToEnd.
readStream close.

"We create a SkiaImage and use it to get the form."
^ (SkiaImage
fromBuffer: pictureBytes
start: 0
end: pictureBytes size - 1) asForm
]

{ #category : #'*GToolkit-Extensions' }
ZnEasy class >> gtGetPng: urlObject [
"Use SkiaImage to convert bytes to a Form to avoid possible parsing errors in PNGs images."

"self getPng: 'http://pharo.org/files/pharo.png'."
"(self getPng: 'http://chart.googleapis.com/chart?cht=tx&chl=',
'a^2+b^2=c^2' urlEncoded) asMorph openInHand."

^ self
gtGetImageOfType: ZnMimeType imagePng
fromUrl: urlObject
]

0 comments on commit 42267c9

Please sign in to comment.