Page 1 of 1

Image Upload Help

Posted: Tue Jan 14, 2014 11:44 am
by smashapps
Hey guys,

I could upload an image to a database or to a website, but I need to upload an image to an image sharing website like tinypic, imageshack and what not. I have the path of an image and I need to be able to upload it and then return the path of the uploaded image.

There are APIs that can help but I haven't been successful with this yet. Anyone here able to help me?

Thanks.

Re: Image Upload Help

Posted: Tue Jan 14, 2014 12:33 pm
by visualtech
Depending on the language, this problem can be taken down, differently.

If you are using PHP, you're in luck, PHP has a native function to help upload the file. Here: you can either: (i) Upload to a Web Folder or (ii) Insert as a BLOB in MySQL DB.

If you are using VB.NET, then things might get a little complicated. You can either: (i) Use PHP as a code-behind or (ii) Use MySQL Connector for .NET to insert into a MySQL DB or (iii) FTP [Easy]

=========================================================

As far as your question is concerned, I have used an API to upload an Image to Flikr. For TinyPic, you have to use PHP to utilize their RESTful API and then send data to the PHP as multipart/form-data for a significantly sized payload, otherwise, use application/x-www-form-urlencoded.

Re: Image Upload Help

Posted: Tue Jan 14, 2014 3:42 pm
by anthonygz
You can try this bit of code written by Falord (Not the Falord you know)

Public Class tinypic
Public Function upload(ByVal image As String) As String
Dim wc As New System.Net.WebClient
Dim tpicHTML As String = wc.DownloadString("http://tinypic.com")
Dim contentID As String = Regex.Match(tpicHTML, "value=""([0-9]*_[0-9]*)""").Groups.Item(1).Value.ToString
Dim upk As String = Regex.Match(tpicHTML, "name=""upk"" value=""([0-9a-f]*)""").Groups.Item(1).Value.ToString



Dim s As Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP)
Dim filename As String = image
Dim boundry As String = "----------" + DateTime.Now.Ticks.ToString("x")
Dim packet As StringBuilder = New StringBuilder
Dim pd As MemoryStream = New MemoryStream
pd.Write(Encoding.ASCII.GetBytes("--" & boundry & vbCrLf), 0, ("--" & boundry & vbCrLf).Length)
pd.Write(Encoding.ASCII.GetBytes("Content-Disposition: form-data; name=""UPLOAD_IDENTIFIER""" & vbCrLf & vbCrLf), 0, ("Content-Disposition: form-data; name=""UPLOAD_IDENTIFIER""" & vbCrLf & vbCrLf).Length)
pd.Write(Encoding.ASCII.GetBytes(contentId & vbCrLf), 0, (contentId & vbCrLf).Length)
pd.Write(Encoding.ASCII.GetBytes("--" & boundry & vbCrLf), 0, ("--" & boundry & vbCrLf).Length)
pd.Write(Encoding.ASCII.GetBytes("Content-Disposition: form-data; name=""upk""" & vbCrLf & vbCrLf), 0, ("Content-Disposition: form-data; name=""upk""" & vbCrLf & vbCrLf).Length)
pd.Write(Encoding.ASCII.GetBytes(upk & vbCrLf), 0, (upk & vbCrLf).Length)
pd.Write(Encoding.ASCII.GetBytes("--" & boundry & vbCrLf), 0, ("--" & boundry & vbCrLf).Length)
pd.Write(Encoding.ASCII.GetBytes("Content-Disposition: form-data; name=""domain_lang""" & vbCrLf & vbCrLf), 0, ("Content-Disposition: form-data; name=""domain_lang""" & vbCrLf & vbCrLf).Length)
pd.Write(Encoding.ASCII.GetBytes("en" & vbCrLf), 0, ("en" & vbCrLf).Length)
pd.Write(Encoding.ASCII.GetBytes("--" & boundry & vbCrLf), 0, ("--" & boundry & vbCrLf).Length)
pd.Write(Encoding.ASCII.GetBytes("Content-Disposition: form-data; name=""file_type""" & vbCrLf & vbCrLf), 0, ("Content-Disposition: form-data; name=""file_type""" & vbCrLf & vbCrLf).Length)
pd.Write(Encoding.ASCII.GetBytes("image" & vbCrLf), 0, ("image" & vbCrLf).Length)
pd.Write(Encoding.ASCII.GetBytes("--" & boundry & vbCrLf), 0, ("--" & boundry & vbCrLf).Length)
pd.Write(Encoding.ASCII.GetBytes("Content-Disposition: form-data; name=""dimension""" & vbCrLf & vbCrLf), 0, ("Content-Disposition: form-data; name=""dimension""" & vbCrLf & vbCrLf).Length)
pd.Write(Encoding.ASCII.GetBytes("1600" & vbCrLf), 0, ("1600" & vbCrLf).Length)
pd.Write(Encoding.ASCII.GetBytes("--" & boundry & vbCrLf), 0, ("--" & boundry & vbCrLf).Length)
pd.Write(Encoding.ASCII.GetBytes("Content-Disposition: form-data; name=""video-settings""" & vbCrLf & vbCrLf), 0, ("Content-Disposition: form-data; name=""video-settings""" & vbCrLf & vbCrLf).Length)
pd.Write(Encoding.ASCII.GetBytes("" & vbCrLf), 0, ("" & vbCrLf).Length)
pd.Write(Encoding.ASCII.GetBytes("--" & boundry & vbCrLf), 0, ("--" & boundry & vbCrLf).Length)
pd.Write(Encoding.ASCII.GetBytes("Content-Disposition: form-data; name=""addresses""" & vbCrLf & vbCrLf), 0, ("Content-Disposition: form-data; name=""addresses""" & vbCrLf & vbCrLf).Length)
pd.Write(Encoding.ASCII.GetBytes("" & vbCrLf), 0, ("" & vbCrLf).Length)
pd.Write(Encoding.ASCII.GetBytes("--" & boundry & vbCrLf), 0, ("--" & boundry & vbCrLf).Length)
pd.Write(Encoding.ASCII.GetBytes("Content-Disposition: form-data; name=""description""" & vbCrLf & vbCrLf), 0, ("Content-Disposition: form-data; name=""description""" & vbCrLf & vbCrLf).Length)
pd.Write(Encoding.ASCII.GetBytes("" & vbCrLf), 0, ("" & vbCrLf).Length)
pd.Write(Encoding.ASCII.GetBytes("--" & boundry & vbCrLf), 0, ("--" & boundry & vbCrLf).Length)
pd.Write(Encoding.ASCII.GetBytes("Content-Disposition: form-data; name=""action""" & vbCrLf & vbCrLf), 0, ("Content-Disposition: form-data; name=""action""" & vbCrLf & vbCrLf).Length)
pd.Write(Encoding.ASCII.GetBytes("upload" & vbCrLf), 0, ("upload" & vbCrLf).Length)
pd.Write(Encoding.ASCII.GetBytes("--" & boundry & vbCrLf), 0, ("--" & boundry & vbCrLf).Length)
pd.Write(Encoding.ASCII.GetBytes("Content-Disposition: form-data; name=""MAX_FILE_SIZE""" & vbCrLf & vbCrLf), 0, ("Content-Disposition: form-data; name=""MAX_FILE_SIZE""" & vbCrLf & vbCrLf).Length)
pd.Write(Encoding.ASCII.GetBytes("500000000" & vbCrLf), 0, ("500000000" & vbCrLf).Length)
pd.Write(Encoding.ASCII.GetBytes("--" & boundry & vbCrLf), 0, ("--" & boundry & vbCrLf).Length)
pd.Write(Encoding.ASCII.GetBytes("Content-Disposition: form-data; name=""shareopt""" & vbCrLf & vbCrLf), 0, ("Content-Disposition: form-data; name=""shareopt""" & vbCrLf & vbCrLf).Length)
pd.Write(Encoding.ASCII.GetBytes("true" & vbCrLf), 0, ("true" & vbCrLf).Length)
pd.Write(Encoding.ASCII.GetBytes("--" & boundry & vbCrLf), 0, ("--" & boundry & vbCrLf).Length)
pd.Write(Encoding.ASCII.GetBytes("Content-Disposition: form-data; name=""the_file""; filename=""" & filename.Split("\")(1) & """" & vbCrLf), 0, ("Content-Disposition: form-data; name=""the_file""; filename=""" & filename.Split("\")(1) & """" & vbCrLf).Length)
pd.Write(Encoding.ASCII.GetBytes("Content-Type: image/pjpeg" & vbCrLf & vbCrLf), 0, ("Content-Type: image/pjpeg" & vbCrLf & vbCrLf).Length)
pd.Write(File.ReadAllBytes(filename), 0, New FileInfo(filename).Length)
pd.Write(Encoding.ASCII.GetBytes("" & vbCrLf), 0, Encoding.ASCII.GetByteCount("" & vbCrLf))
pd.Write(Encoding.ASCII.GetBytes("--" & boundry & "--"), 0, ("--" & boundry & "--").Length)
packet.AppendLine("POST /upload.php HTTP/1.1")
packet.AppendLine("Accept: Accept: image/gif, image/jpeg, image/pjpeg, application/x-ms-application, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-ms-xbap, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, application/vnd.ms-powerpoint, */*")
packet.AppendLine("Accept-Language: en-us")
packet.AppendLine("Referer: http://www.tinypic.com/")
packet.AppendLine("User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.21022; OfficeLiveConnector.1.3; .NET CLR 3.5.30729; .NET CLR 3.0.30618; OfficeLivePatch.1.3)")
packet.AppendLine("Content-Type: multipart/form-data; boundary=" & boundry)
packet.AppendLine("Accept-Encoding: text")
packet.AppendLine("Host: s5.tinypic.com")
packet.AppendLine("Content-Length: " & pd.Length)
packet.AppendLine("Connection: Keep-Alive")
packet.AppendLine("Cache-Control: no-cache")
packet.AppendLine("Cookie: language=a%3A1%3A%7Bs%3A8%3A%22language%22%3Bs%3A2%3A%22en%22%3B%7D;")
packet.AppendLine("")
Dim postdatabyte As Byte() = pd.ToArray
s.Connect("s5.tinypic.com", 80)
s.Send(Encoding.ASCII.GetBytes(packet.ToString), Encoding.ASCII.GetByteCount(packet.ToString), 0)
s.Send(postdatabyte)
Dim b As Int32 = 0
Dim buffer(256) As Byte
Dim html As StringBuilder = New StringBuilder
Do
b = s.Receive(buffer, 0, 256, 0)
html.Append(Encoding.ASCII.GetString(buffer, 0, 256))
Loop While b > 0
Try
s.Shutdown(SocketShutdown.Both)
s.Close()
Catch ex As Exception
End Try
Dim imgSrc As String = ""
'http://i42.tinypic.com/r9q549.jpg
If Regex.IsMatch(html.ToString, "name=""pic"" value=""(.*)""") Then
Dim picName As String = Regex.Match(html.ToString, "name=""pic"" value=""(.*)""").Groups.Item(1).Value
Dim server As String = Regex.Match(html.ToString, "name=""ival"" value=""(.*)""").Groups.Item(1).Value
Return "http://i" & server & ".tinypic.com/" & picName & ".jpg"
Else
Return imgSrc
End If
End Function
End Class

tinypic.upload("Location of Image")

If it doesn't work, then tinypic must've changed their parameters or how their website is layed out to where you can't use this sort of "API" like approach to the website uploading wise.

Re: Image Upload Help

Posted: Tue Jan 14, 2014 5:21 pm
by Filip
Hello #anthonygz

We really appreciate your code examples, but please try putting your code in code tags

[*code]Put code here[*/code] without asterisks

to get this:
Code: Select all
Put code here
It helps keep the code organized and thread look nicer

Thank you!
-Filip

Re: Image Upload Help

Posted: Tue Jan 14, 2014 10:18 pm
by smashapps
I've tried that code #anthonygz when the image uploads the app freezes for 5 minutes+ and the picture doesn't upload.

#visualtech could I get an vb.net example using the API?

Thanks everyone

Re: Image Upload Help

Posted: Wed Jan 15, 2014 3:16 am
by Shim
Something I found that can help you.

Flickr API(Library) :

VB.NET
Code: Select all
' http://www.flickr.com/services/api/misc.api_keys.html
Dim flickrApiKey As String = "<api key>"
Dim flickrApiSharedSecret As String = "<shared secret>"
Dim flickrAuthenticationToken As String = "<authentication token>"

Dim flickr As New Flickr(flickrApiKey, flickrApiSharedSecret)

flickr.AuthToken = flickrAuthenticationToken

For Each image As FileInfo In New FileInfo() {New FileInfo("C:\image1.jpg"), New FileInfo("C:\image2.jpg")}
	Dim photoId As String = flickr.UploadPicture(image.FullName, image.Name, image.Name, "tag1, tag2")
Next
C#
Code: Select all
// http://www.flickr.com/services/api/misc.api_keys.html
string flickrApiKey = "<api key>";
string flickrApiSharedSecret = "<shared secret>";
string flickrAuthenticationToken = "<authentication token>";

Flickr flickr = new Flickr( flickrApiKey, flickrApiSharedSecret );

flickr.AuthToken = flickrAuthenticationToken;    

foreach ( FileInfo image in new FileInfo[] { 
    new FileInfo( @"C:\image1.jpg" ), 
    new FileInfo( @"C:\image2.jpg" ) } )
{
    string photoId = flickr.UploadPicture(
        image.FullName, image.Name, image.Name, "tag1, tag2" );
}
Tinypic : Tinypic doesn't have an API but there was a library/plugin Cropper SendToTinyPic they had a feature called SendToTinyPic unfortunately that feature has been removed now.

Imgur : Imgur is similar to Tinypic. I found a very well working Imgur Uploading project. Download it from here(It's not my built so VirusTotal Report)

Thank you :)

Re: Image Upload Help

Posted: Wed Jan 15, 2014 12:14 pm
by visualtech
Ofcourse, #smashapps, I'll write and send the code (via PM)