Jump to content

Saving Image from URL through API


DaveP
 Share

Recommended Posts

Simple problem - I'm trying to save an image* from an external API and PW's API names in for its URL. The derived filename is too long for my filesystem. Is there any way (an appropriate hook would be lovely) to rename it before it is initially saved? (Kinda like 'Save As')

*The image is a static map thumbnail from http://www.mapquestapi.com/staticmap/#getplacemap and, with a real API key inserted and the file path tacked on the front, the path+filename chokes my dev machine.

Link to comment
Share on other sites

@adrian, I did try Custom Upload Names, but it didn't seem to work on image files saved purely through the API. If it should do, then I will persevere with it some more. Any tips would be very welcome.

Link to comment
Share on other sites

@adrian, I did try Custom Upload Names, but it didn't seem to work on image files saved purely through the API. If it should do, then I will persevere with it some more. Any tips would be very welcome.

It should definitely work through the API - let me take a look to confirm that I didn't break that functionality at some point, or that there isn't an issue with it and PW 3.x or anything else amiss. Will get back to you shortly.

Link to comment
Share on other sites

Hi Dave - I just tested CUN again with an API upload and it's working fine - can you please post your API code so I can take a look.

EDIT: This is what I am using:

$page->of(false);
$page->images->add("http://domain.com/the_image.png");
$page->save("images");
Link to comment
Share on other sites

Adrian, I'm using a single image field so it's

$p->of(false);
$p->map_thumbnail = 'http://verylongurl.com?lotsofparameters=toolongfilename';
$p->save();

so it's slightly different. But the error is that PW can't move the file, so at least that suggests that it would work with a shorter file name. (Limit is ~260 characters.)

Give me a moment to install the module again and I'll get back to you.

Link to comment
Share on other sites

Ok, I just checked with a filename over 260 characters and it uploaded ok, but didn't get renamed by CUN. Not because of the length, but because the filename didn't end in .jpg, .png etc

I just used this is a test:

http://domain.com/image_name.png?test=565675765675675675625625625672567256256254672567256256256725167256526725672567253625679267829647218546152412346725876651266781624582678512676567247625875267456255269759766786736738673678467836782357653675438762354675375

I can take a look at making CUN support things like this if it would help.

PS - it was actually renamed, but the "test=1267686....." remained on the end.

Link to comment
Share on other sites

Nope, still not working, with your $page->images->add() style coding. This is the error

Error: Exception: Unable to copy: http://www.mapquestapi.com/staticmap/v4/getplacemap?key=myAPIKey&location=PR7%202EJ&size=250,250&type=map&zoom=15&imagetype=gif&showicon=purple_1 => D:/wamp/www/ss/site/assets/files/1026/getplacemap_key_myAPIKey_location_L4_0TH_size_250-250_type_map_zoom_15_imagetype_gif_showicon_purple_1.com_staticmap_v4_getplacemap_key_myAPIKey_location_L4_0TH_size_250_250_type_map_zoom_15_imagetype_gif_showicon_purple_1 (in D:\wamp\www\ss\wire\core\Pagefile.php line 117)

As far as I can tell I set up your module correctly - enabled just for map_thumbnail field and renaming to just mapthumbnail as it is a singular field and there will only ever be one image.

PW 2.7.2 in case I forgot to mention.

<edit>Take out both API key mentions.</edit>

Edited by DaveP
Link to comment
Share on other sites

Ok, I just tested (got my own mapquest api key) and am getting the same error as you - this is something that will need to be dealt with before PW tries to sanitize the file - there is no file extension, so it doesn't know what it is. Either way, the error is coming before CUN gets a look in, so it won't help in this case.

You could use wireUpload I guess, or maybe hook in earlier - perhaps Pagefile::filename - I'll keep digging.

  • Like 1
Link to comment
Share on other sites

@Adrian Hmm, looks like a similar problem to sevarf2's. Late in the day here, so I'll revisit this over the weekend.

@kongondo Would I be right in thinking that that method would entail an intermediate step, much like the one in the thread Adrian links to above? 

@justb3a Tried a few variations with your suggestion, but no joy.

If the worst comes to the worst, I could just call a new image every page load. I was trying to do some local caching and play nice with MapQuest's API.  :P

<edit> Couldn't resist a bit more googling and http://stackoverflow.com/questions/10233577/create-image-from-url-any-file-type looks promising. No time to test now but I will report back at some point.</edit>

Edited by DaveP
  • Like 1
Link to comment
Share on other sites

  • 3 weeks later...
  • 1 month later...
  • 1 year later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...