Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Safe image as sf::Image with sf::http  (Read 3804 times)

0 Members and 1 Guest are viewing this topic.

etixpp

  • Jr. Member
  • **
  • Posts: 82
    • View Profile
    • FoxFire Development Website
    • Email
Safe image as sf::Image with sf::http
« on: October 12, 2013, 03:47:08 am »
Hello, i think topic probably explains what i want to do, is it possible? I was able to safe the image as a string but that probably won´t help me much :/

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Safe image as sf::Image with sf::http
« Reply #1 on: October 12, 2013, 04:04:06 am »
Actually it's extremely unclear what you're asking for.

1) You keep saying "safe" when you appear to mean "save."
2) http is a protocol for exchanging information over a network, typically retrieving web pages.  It has absolutely nothing to do with file saving.  If you want to upload and download files over a network, you probably want ftp.
3) Assuming what you want is saving an sf::Image as a file on the machine running your program, there is a method for that.
4) If you want to save an image file to an sf::Image, that makes no sense because sf::Image is not a file format; it's just a class within SFML.  It has no meaning outside of an SFML program.
5) If you want to convert the data within an sf::Image to a std::string, then use this method to get the data, after which conversion to char or string types should be straightforward.
« Last Edit: October 12, 2013, 04:07:25 am by Ixrec »

etixpp

  • Jr. Member
  • **
  • Posts: 82
    • View Profile
    • FoxFire Development Website
    • Email
Re: Safe image as sf::Image with sf::http
« Reply #2 on: October 12, 2013, 04:06:48 am »
Ok ok example:
I want to download a picture from a strange site (no ftp access) at example
http://wpde.org/wp-content/themes/wpde-mainsite/img/icons/funktionen.png
and save it as a sprite or image in my programm so i can display it...

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Safe image as sf::Image with sf::http
« Reply #3 on: October 12, 2013, 04:09:17 am »
In that case:

1) Use HTTP to download the file from the internet to the computer running the program.  I'll let you read the tutorials and documentation for SFML's network module, since this is a very typical use for it.

2) Once downloaded, simply load the file into an sf::Image or sf::Texture with loadFromFile() just like any other file.

I'm not aware of any way to do this that doesn't involve temporarily saving the file to the computer the program's running on; even if there is it's probably far more effort than it could ever be worth.

etixpp

  • Jr. Member
  • **
  • Posts: 82
    • View Profile
    • FoxFire Development Website
    • Email
Re: Safe image as sf::Image with sf::http
« Reply #4 on: October 12, 2013, 04:13:35 am »
Well it´s late at night and i am kinda brainfucked so sorry for the probably dumb question but how can i download a image to my current directory? Just searching since 20 Minuten in the Documentation but sf::Http is only able to return response as String and i cannot find a download function or something like this without ftp :/

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Safe image as sf::Image with sf::http
« Reply #5 on: October 12, 2013, 04:16:55 am »
Pretty sure an HTTP GET will do it.

I don't know the details since I've never done this myself, but this stuff is very standard and not unique to SFML so I'm sure you can google all sorts of information on how GET requests work.

Edit: Just a guess, but have you tried using getBody() on the response and using the result to make an sf::Image?
« Last Edit: October 12, 2013, 04:20:05 am by Ixrec »

etixpp

  • Jr. Member
  • **
  • Posts: 82
    • View Profile
    • FoxFire Development Website
    • Email
Re: Safe image as sf::Image with sf::http
« Reply #6 on: October 12, 2013, 04:19:36 am »
i know well how a get request works but it can only give me a string back... and a string is not a downloaded picture xD

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Safe image as sf::Image with sf::http
« Reply #7 on: October 12, 2013, 04:22:22 am »
Oh well, sorry I don't know exactly how this works.

Do try a few things like loadFromMemory on getBody() though.  I'm sure it's something very simple that I just don't know off the top of my head.
« Last Edit: October 12, 2013, 04:24:31 am by Ixrec »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Safe image as sf::Image with sf::http
« Reply #8 on: October 12, 2013, 04:40:17 am »
To use loadFromMemory, take a look at this thread (and its first answer), it's old but probably still works the same way.

etixpp

  • Jr. Member
  • **
  • Posts: 82
    • View Profile
    • FoxFire Development Website
    • Email
Re: Safe image as sf::Image with sf::http
« Reply #9 on: October 12, 2013, 04:48:56 am »
solved problem this way:
   sf::Http http;
   http.setHost("http://riedbook.de/");
sf::Http::Request request;
request.setMethod(sf::Http::Request::Get);
request.setUri("/images/avatar.jpg");
request.setHttpVersion(1, 1); // HTTP 1.1
sf::Http::Response response = http.sendRequest(request);
std::string body = response.getBody();

sf::Texture test;
test.loadFromMemory(body.data(),body.size());
sf::Sprite testsprite;
testsprite.setTexture(test);

Without even saving the file on my Pc temporally, i think this is a nice solution :)

Thinks Ixrec for idea with loadmemory D: