Cum fac upload la o imagine pe un server php?
  [ Ignoră ]
Avatar
RankRankRankRank
Sr. Member
Din: Bucuresti
Macuser din: 22.09.06

Am inteles ca o varianta ar fi prin json ca string encoded base 64, am creat json-ul,l-am atribuit proprietatii HTTPBody a NSMutableURLRequest dar pe server nu a reusit sa salveze imaginea.
Exista alta optiune pentru a trimite imaginea?

 Semnătură 

A man should look for what is, and not for what he thinks should be.—Albert Einstein

Profil
 
  [ Ignoră ]   [ # 1 ]
Avatar
RankRankRank
Member
Din: Bucuresti - Baba Novac
Macuser din: 10.05.06

Poti detalia te rog? Ce vrei anume sa faci… si eventual cum te-ai gandit in momentul de fata.

 Semnătură 

cool smirk  Generally a smart-ass!

Profil
 
  [ Ignoră ]   [ # 2 ]
Avatar
RankRankRankRank
Sr. Member
Din: Bucuresti
Macuser din: 22.09.06

Trebuie sa fac upload la o imagine dar impreuna cu ea si un user id pentru actualizarea unui user account pe server.
As avea mai multe variante:

1. json cu imaginea encode base 64 + user id.
2. id-ul in http header si imaginea in httpbody request.
3. multipart body, care as evita-o smile

Acum testez json-ul care din partea mea e ok implementarea:

NSMutableURLRequest *request [NSMutableURLRequest requestWithURLurl];
    
    
NSString *username [NSString stringWithFormat: @"usr_mail@gmail.com"];
    
NSString *password [NSString stringWithFormat: @"pass123"];
    
NSString *userId [NSString stringWithFormat: @"%@"[self.userProfileDictionary objectForKey: @"id"]];

    
NSData *imageData UIImagePNGRepresentation(selectedImage);
    
NSString *imageString [imageData base64EncodedString];
    
    
NSMutableDictionary *dict [[NSMutableDictionary alloc] init];
    
[dict setObjectusername forKey: @"username"];
    
[dict setObjectpassword forKey: @"password"];
    
[dict setObjectuserId forKey: @"id"];
    
[dict setObjectimageString forKey: @"image"];
    
    
NSError *jsonError nil;
    
NSData *jsonData [NSJSONSerialization dataWithJSONObjectdict options0 error: &jsonError;];
    
    
[request setValue:@"application/json-rpc" forHTTPHeaderField:@"Content-Type"];
    
[request setHTTPMethod: @"POST"];
    
[request setHTTPBodyjsonData];
    
    
[UIApplication sharedApplication].networkActivityIndicatorVisible YES;
    
[NSURLConnection sendAsynchronousRequestrequest queueself.myQueue completionHandler: ^(NSURLResponse *responseNSData *dataNSError *error
 Semnătură 

A man should look for what is, and not for what he thinks should be.—Albert Einstein

Profil
 
  [ Ignoră ]   [ # 3 ]
Avatar
RankRankRank
Member
Din: Bucuresti - Baba Novac
Macuser din: 10.05.06

ok, seems ok. De retinut ca prin API-ul iOS imaginea este trimisa ca PNG.
PHP-ul care primeste datele cum arata? cum arata?

 Semnătură 

cool smirk  Generally a smart-ass!

Profil
 
  [ Ignoră ]   [ # 4 ]
Avatar
RankRankRankRank
Sr. Member
Din: Bucuresti
Macuser din: 22.09.06

Asta nu stiu,nu eu lucrez la partea de web. A primit json-ul mi-a arata copy paste, string-ul arata exact ca al meu.
Problema este sigur pe server dar nu stiu cu ce sa il ajut, nu stiu cum trebuie in php sa salveze imaginea si sa decodeze cu base 64

 Semnătură 

A man should look for what is, and not for what he thinks should be.—Albert Einstein

Profil
 
  [ Ignoră ]   [ # 5 ]
RankRank
Jr. Member
Din: Bucuresti
Macuser din: 26.03.10

ai grija cand comunici cu serverul sa folosesti escape-uri!

 Semnătură 

 iPhone 5 16GB black, iOS 7 beta 5
 MacBook Pro 15” Unibody, Late 2011 2,4 Ghz Intel Core i7, 8GB RAM, 128GB SSD, Radeon 6770 1GB, HiRes Display, OS X 10.8.3
 Thunderbolt Display

Profil
 
  [ Ignoră ]   [ # 6 ]
Avatar
RankRankRankRank
Sr. Member
Din: Bucuresti
Macuser din: 22.09.06
VladN - 11 Ianuarie 2012 02:54 PM

ai grija cand comunici cu serverul sa folosesti escape-uri!


Nu stiu la ce te referi smile

 Semnătură 

A man should look for what is, and not for what he thinks should be.—Albert Einstein

Profil
 
  [ Ignoră ]   [ # 7 ]
Avatar
RankRankRank
Member
Din: Bucuresti - Baba Novac
Macuser din: 10.05.06

aha.

$imgcode = chunk_split($encimg,20,’‘.’‘);

function display_img($imgcode,$type)
    {
      header(‘Content-type: image/’.$type);
      header(‘Content-length: ‘.size($imgcode));
      echo base64_decode($imgcode);
    }

si folosesti display_img(“string_base64”, “png”);

de salvat, apoi e simplu

 Semnătură 

cool smirk  Generally a smart-ass!

Profil
 
  [ Ignoră ]   [ # 8 ]
Avatar
RankRankRank
Member
Din: Bucuresti - Baba Novac
Macuser din: 10.05.06
$imgcode chunk_split($encimg,20,".");

function 
save_image($imgpath,$imgcode,$type)
    
{
      header
('Content-type: image/'.$type);
      
header(‘Content-length.size($imgcode));
      
//echo base64_decode($imgcode); -> pentru afisare
       
$im imagecreatefrompng($imgpath."/".base64decode($imgcode));
      
imagepng($im);
      
imagedestroy($im);
    

si folosesti

save_image("path_salvare","string_base64_venit_din_json""png"); 
 Semnătură 

cool smirk  Generally a smart-ass!

Profil
 
  [ Ignoră ]   [ # 9 ]
Avatar
RankRankRankRank
Sr. Member
Din: Bucuresti
Macuser din: 22.09.06

@equinoxe Merci, am inteles ca a facut si asa si nu a mers.
Imaginea am vazut ca a primit-o, mi-a dat link in browser o vede dar nu o poate salva.
A zis sa intreb daca sti o metoda sa atribuie la $_FILES o imagine, gen la cazul de mai sus $_FILES[‘avatar’] = $im.

Nu stiu ce inseamna asta smile

 Semnătură 

A man should look for what is, and not for what he thinks should be.—Albert Einstein

Profil
 
  [ Ignoră ]   [ # 10 ]
Avatar
RankRankRank
Member
Din: Bucuresti - Baba Novac
Macuser din: 10.05.06

E aiurea sa conversam asa. Il poti intreba ce erori ii da cand incearca s-o creeze asa?

 Semnătură 

cool smirk  Generally a smart-ass!

Profil
 
  [ Ignoră ]   [ # 11 ]
Avatar
RankRankRankRank
Sr. Member
Din: Bucuresti
Macuser din: 22.09.06

@equinoxe multumesc mult,s-a rezolvat pana la urma smile

 Semnătură 

A man should look for what is, and not for what he thinks should be.—Albert Einstein

Profil
 
  [ Ignoră ]   [ # 12 ]
Avatar
RankRankRank
Member
Din: Bucuresti - Baba Novac
Macuser din: 10.05.06

Cu placere! Glad to be of help! Care era beleaua?

 Semnătură 

cool smirk  Generally a smart-ass!

Profil
 
  [ Ignoră ]   [ # 13 ]
Avatar
RankRankRankRank
Sr. Member
Din: Bucuresti
Macuser din: 22.09.06

Nu stiu exact,avea el pe server o problema cu salvarea imaginii,la mine era ok smile

 Semnătură 

A man should look for what is, and not for what he thinks should be.—Albert Einstein

Profil
 
  [ Ignoră ]   [ # 14 ]
RankRank
Jr. Member
Din: Bucuresti
Macuser din: 26.03.10

Escape Character

 Semnătură 

 iPhone 5 16GB black, iOS 7 beta 5
 MacBook Pro 15” Unibody, Late 2011 2,4 Ghz Intel Core i7, 8GB RAM, 128GB SSD, Radeon 6770 1GB, HiRes Display, OS X 10.8.3
 Thunderbolt Display

Profil
 
   
 
 
‹‹ Parser XML      Popover frame color ››