Cum fac upload la o imagine pe un server php?
Trimis: 11 Ianuarie 2012 10:53 AM
[ Ignoră ]
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
Trimis: 11 Ianuarie 2012 02:38 PM
[ Ignoră ]
[ # 1 ]
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ă
Generally a smart-ass!
Trimis: 11 Ianuarie 2012 02:45 PM
[ Ignoră ]
[ # 2 ]
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
Acum testez json-ul care din partea mea e ok implementarea:
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL : url] ; 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 setObject : username forKey : @ "username" ] ; [dict setObject : password forKey : @ "password" ] ; [dict setObject : userId forKey : @ "id" ] ; [dict setObject : imageString forKey : @ "image" ] ; NSError * jsonError = nil ; NSData * jsonData = [NSJSONSerialization dataWithJSONObject : dict options : 0 error : & jsonError ; ] ; [request setValue :@ "application/json-rpc" forHTTPHeaderField :@ "Content-Type" ] ; [request setHTTPMethod : @ "POST" ] ; [request setHTTPBody : jsonData] ; [UIApplication sharedApplication] . networkActivityIndicatorVisible = YES ; [NSURLConnection sendAsynchronousRequest : request queue : self . myQueue completionHandler : ^( NSURLResponse * response , NSData * data , NSError * error )
Semnătură
A man should look for what is, and not for what he thinks should be.—Albert Einstein
Trimis: 11 Ianuarie 2012 02:50 PM
[ Ignoră ]
[ # 3 ]
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ă
Generally a smart-ass!
Trimis: 11 Ianuarie 2012 02:51 PM
[ Ignoră ]
[ # 4 ]
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
Trimis: 11 Ianuarie 2012 02:54 PM
[ Ignoră ]
[ # 5 ]
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
Trimis: 11 Ianuarie 2012 02:56 PM
[ Ignoră ]
[ # 6 ]
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
Semnătură
A man should look for what is, and not for what he thinks should be.—Albert Einstein
Trimis: 11 Ianuarie 2012 02:56 PM
[ Ignoră ]
[ # 7 ]
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ă
Generally a smart-ass!
Trimis: 11 Ianuarie 2012 03:03 PM
[ Ignoră ]
[ # 8 ]
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ă
Generally a smart-ass!
Trimis: 11 Ianuarie 2012 03:06 PM
[ Ignoră ]
[ # 9 ]
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
Semnătură
A man should look for what is, and not for what he thinks should be.—Albert Einstein
Trimis: 11 Ianuarie 2012 03:29 PM
[ Ignoră ]
[ # 10 ]
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ă
Generally a smart-ass!
Trimis: 11 Ianuarie 2012 04:14 PM
[ Ignoră ]
[ # 11 ]
Sr. Member
Din: Bucuresti
Macuser din: 22.09.06
@equinoxe multumesc mult,s-a rezolvat pana la urma
Semnătură
A man should look for what is, and not for what he thinks should be.—Albert Einstein
Trimis: 11 Ianuarie 2012 04:27 PM
[ Ignoră ]
[ # 12 ]
Member
Din: Bucuresti - Baba Novac
Macuser din: 10.05.06
Cu placere! Glad to be of help! Care era beleaua?
Semnătură
Generally a smart-ass!
Trimis: 11 Ianuarie 2012 04:28 PM
[ Ignoră ]
[ # 13 ]
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
Semnătură
A man should look for what is, and not for what he thinks should be.—Albert Einstein
Trimis: 11 Ianuarie 2012 04:56 PM
[ Ignoră ]
[ # 14 ]
Jr. Member
Din: Bucuresti
Macuser din: 26.03.10
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