unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Fetching an image from the net and inserting into the buffer
@ 2014-10-25  6:54 Tom
  2014-10-25  7:18 ` Eli Zaretskii
  0 siblings, 1 reply; 3+ messages in thread
From: Tom @ 2014-10-25  6:54 UTC (permalink / raw)
  To: help-gnu-emacs

I tried this code which when evaluated should insert the
image fetched from the net at point, but it inserts a rectangle
instead:

(insert-image
 (with-current-buffer 
     (url-retrieve-synchronously
"http://upload.wikimedia.org/wikipedia/commons/thumb/4/47/PNG_transparency_demonstration_1.png/280px-PNG_transparency_demonstration_1.png")
   (goto-char (point-min))
   (search-forward "\n\n") ;skip headers
   (create-image (buffer-substring (point) (point-max)) 'png)))

It's emacs 24.1.1 on windows. Image are supported. If I download
the image from the above URL to a file and use create-image on 
that file then it displays properly.

But why doesn't it work when I try to fetch it dynamically with
url retrieve?




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Fetching an image from the net and inserting into the buffer
  2014-10-25  6:54 Fetching an image from the net and inserting into the buffer Tom
@ 2014-10-25  7:18 ` Eli Zaretskii
  2014-10-25  7:31   ` Tom
  0 siblings, 1 reply; 3+ messages in thread
From: Eli Zaretskii @ 2014-10-25  7:18 UTC (permalink / raw)
  To: help-gnu-emacs

> From: Tom <adatgyujto@gmail.com>
> Date: Sat, 25 Oct 2014 06:54:14 +0000 (UTC)
> 
> I tried this code which when evaluated should insert the
> image fetched from the net at point, but it inserts a rectangle
> instead:
> 
> (insert-image
>  (with-current-buffer 
>      (url-retrieve-synchronously
> "http://upload.wikimedia.org/wikipedia/commons/thumb/4/47/PNG_transparency_demonstration_1.png/280px-PNG_transparency_demonstration_1.png")
>    (goto-char (point-min))
>    (search-forward "\n\n") ;skip headers
>    (create-image (buffer-substring (point) (point-max)) 'png)))
> 
> It's emacs 24.1.1 on windows. Image are supported. If I download
> the image from the above URL to a file and use create-image on 
> that file then it displays properly.
> 
> But why doesn't it work when I try to fetch it dynamically with
> url retrieve?

You need to pass an additional argument to create-image, like this:

  (create-image (buffer-substring (point) (point-max)) 'png t)

That 't' tells create-image it should create an image from the data
that is its 1st argument.  Please see the doc string of the function
for details.



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Fetching an image from the net and inserting into the buffer
  2014-10-25  7:18 ` Eli Zaretskii
@ 2014-10-25  7:31   ` Tom
  0 siblings, 0 replies; 3+ messages in thread
From: Tom @ 2014-10-25  7:31 UTC (permalink / raw)
  To: help-gnu-emacs

Eli Zaretskii <eliz <at> gnu.org> writes:
> 
> You need to pass an additional argument to create-image, like this:
> 
>   (create-image (buffer-substring (point) (point-max)) 'png t)
> 
> That 't' tells create-image it should create an image from the data
> that is its 1st argument.  Please see the doc string of the function
> for details.
> 

Ah, yes. Thanks. It's working now.

But it's a bit strange, because without the t I guess it interprets
the string as a file name and tries to read from it.

I could imagine some heuristics the other way around. That is if
the file is not found then it should try to read the string as 
data.

In practice it's very unlikely that a string can be both a valid
file name and valid image data. So if it's not a valid file
name then it could assume it's data and then there would be no
need for the data param.




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2014-10-25  7:31 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-10-25  6:54 Fetching an image from the net and inserting into the buffer Tom
2014-10-25  7:18 ` Eli Zaretskii
2014-10-25  7:31   ` Tom

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).