save image from url

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller

Post Reply
cusingerBUSCw5N
Posts: 339
Joined: Wed Jul 11, 2012 9:24 pm

save image from url

Post by cusingerBUSCw5N » Thu Nov 29, 2018 4:15 am

Hi. I am trying to save a url image to a mobile device.

I have tried SAVING IT:

useline5 is the filename "https://www.yoururlhere.com/image.png" (edited by Heather as the original example url was a porn site :))

Code: Select all

put useline5 into gheaderimage
	  load url gheaderimage as timage
	 export timage to file specialFolderPath("documents") & "/image_header.png" as png
I have tried ACCESSING it:

Code: Select all

 put url("binfile:" &specialFolderPath("documents") & "/image_header.png" ) into gheaderimage
ACCESSING it definitely doesn't work. I'm not sure whether it is the code that is the problem, or whether the code for saving is wrong.
---------
I had thought that

Code: Select all

load url gheaderimage with message showHeader

was working. But I now realize that it only works because it had been cached. When I try to change gheaderimage, it doesn't change. So I need to know how to display an image accessed with a url. All my headers are now blank, because I got rid of the caching code. :cry:
-------
Any help would be appreciated.
Thanks in advance

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3373
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: save image from url

Post by bn » Thu Nov 29, 2018 5:06 pm

I tried this on a Mac/desktop in LC 9.0.2 RC1 and it worked.

You have to adapt the code to your code. (e.g. I download to desktop, I use a script local etc.)

All code is in the button. I unload before load.

Please see also the User Guide for load. (around p 446/447 for the LC 9.0.2 User Guide)
The dictionary entry for load is not very helpful.

Maybe this works also on mobile.

Kind regards
Bernd

PS please don't post anything that Heather has to edit :)


loadImage.livecode.zip
(4.71 KiB) Downloaded 43 times

cusingerBUSCw5N
Posts: 339
Joined: Wed Jul 11, 2012 9:24 pm

Re: save image from url

Post by cusingerBUSCw5N » Thu Nov 29, 2018 5:58 pm

oohhh...I'm sorry ...I had no idea about the site!

And the solution works. Thanks!

cusingerBUSCw5N
Posts: 339
Joined: Wed Jul 11, 2012 9:24 pm

Re: save image from url

Post by cusingerBUSCw5N » Fri Nov 30, 2018 2:44 am

Unfortunately....more complications. The solution worked as long as the image was directly declared in

Code: Select all

local gheaderimage = "https://livecode.com/wp-content/uploads/2018/04/multiplatform-2-1018x1024.jpg"
when the image comes in through the results of a data request and is put in a variable (tuse) and then put into global gheaderimage:

Code: Select all

 ( put "https://livecode.com/wp-content/uploads/2018/04/multiplatform-2-1018x1024.jpg" into tuse
   global gheaderimage
   put tuse into gheaderimage


it returns with a tstatus of "contacted":

Code: Select all

put the URLStatus of gheaderimage into tStatus
... not cached. When I changed the code to allow a tstatus to be cached or contacted, the image does not appear.

Just guessing...but does "contacted" mean that Livecode didn't have time to grab the whole image? Regardless... I have no idea how to fix it. I have attached the stack (without bad urls)
Attachments
loadImage.zip
(1003 Bytes) Downloaded 38 times

cusingerBUSCw5N
Posts: 339
Joined: Wed Jul 11, 2012 9:24 pm

Re: save image from url

Post by cusingerBUSCw5N » Fri Nov 30, 2018 5:20 am

I have tried another example, and when I bring in a different image, it gives me "error" - instead of connected. When I typed in the address, it gives me empty as a status.

This is the code that gives me empty.

Code: Select all

   put "https://www.mywebsite/images/paloalto.png" into gheaderimage
   put the URLStatus of gheaderimage into tStatus
If I bring in the same information, but put it in a variable, it returns an error:

Code: Select all

put "https://www.mywebsite/images/paloalto.png" into useline5
	put useline5 into gheaderimage
      put the URLStatus of gheaderimage into tStatus
The app depends on dynamic loading of these images, so I have to use script to load them. But this method obviously isn't working. :(

AndyP
Posts: 481
Joined: Wed Aug 27, 2008 12:57 pm
Location: Seeheim, Germany (ex UK)
Contact:

Re: save image from url

Post by AndyP » Fri Nov 30, 2018 5:33 pm

Try using libUrlDownloadToFile instead

Code: Select all

on mouseUp
   
   set the itemdel to slash

   put "https://fileinfo.com/img/icons/files/128/livecode-9094.png" into tUrl
   
   --must have a filename to save too!
   put specialFolderPath("desktop") & slash & item -1 of tUrl into tDestPath 
   
   libUrlDownloadToFile tUrl, tDestPath, "DownloadComplete"
   
end mouseUp

on DownloadComplete pUrl, pStatus
   If pStatus = "Downloaded" then
      answer  information "Picture saved!"
   else
      answer warning "Picture failed to download."
   end if
   unload url pUrl
end DownloadComplete
Andy Piddock

http://2108.co.uk/tinyide/ - Mini LC IDE alternative

http://2108.co.uk/script-editor-themer/ - Script Editor colour theming

http://livecodeshare.runrev.com/stack/897/LiveCode-Multi-Search - Search across 8 LC resources

cusingerBUSCw5N
Posts: 339
Joined: Wed Jul 11, 2012 9:24 pm

Re: save image from url

Post by cusingerBUSCw5N » Fri Nov 30, 2018 8:27 pm

So it finally works.

I found that the link I was bringing in has a hidden character.

So this works: :D

Code: Select all

put useline5 into gheaderimage
 set the itemdel to slash
 put useline5 into turl0   //begins fixing the hidden character
 find "png" in turl0
put offset("png",turl0) + 2 into twhat
put char 1 to twhat of turl0 into turl   //hidden character is removed
   put specialFolderPath("documents") & slash & "image_header.png" into tDestPath 
   libUrlDownloadToFile tUrl, tDestPath, "DownloadComplete"
   put url("binfile:" &specialFolderPath("documents") & "/image_header.png" ) into gheaderimage
    put gheaderimage into image "header" of card "homepage"
end if
Thanks for your help!

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”