简体   繁体   中英

PHP Uploading from iOS

Sorry for the long dilemma. I write iOS projects and have just started getting into PHP. Here's my problem. I have developed an Xcode project to upload a photo to a web folder which calls a response from an online php script. But the response returns NSLog (@"No response given"). I modified the php script that uploads the photo from my app to a main folder, creates a random filename, and then copies it into a subfolder. That all works perfect. But there are two main issues. One. The image uploaded is flipped horizontally (mirrored). And two. I am trying to get the response in my Xcode project to return the path to the main image upload location. Here are my codes

XCODE (Uploads perfectly fine but returns "No response given" instead of "My Image Upload Path"

AFHTTPRequestOperationManager *manager [AFHTTPRequestOperationManager manager]; 
AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer]; 
NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:
@"POST" URLString:UPLOAD_URLs parameters:nil    
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

[formData appendPartWithFileData:UIImageJPEGRepresentation(capturedImage, QUALITY_PHOTO_FOR_UPLOAD)
 name:@"photo"
 fileName:@"test"
 mimeType:@"image/jpeg"];
         }];

AFHTTPRequestOperation *operation =
[manager HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"My Image Upload Path: %@", responseObject);

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Report" 
message:@"Your file is uploaded." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
 [alertView show];
 failure:^(AFHTTPRequestOperation *operation, NSError *error) {
           NSLog(@"No response given");
                 }];
[operation setUploadProgressBlock:^(NSUInteger __unused bytesWritten, 
long long totalBytesWritten, long long totalBytesExpectedToWrite) {

                                }];

                                [operation start];

PHP CODE (Uploads, creates random file name, copies photo into thumbs folder perfectly fine but photos are flipped when uploaded and I get a few errors in my log and I need to have a response that displays the path to the image in the Xcode response).

<?php

header('Content-Type: application/json');


function cwUpload($field_name = 'photo', $target_folder = '', $file_name = '', $thumb = TRUE, $thumb_folder = 'thumbs/', $thumb_width = '352', $thumb_height = '264'){
//folder path setup
$target_path = $target_folder;
$thumb_path = $thumb_folder;

//file name setup
$newFileName = md5(rand().time().basename($_FILES['photo']['name'])).'.jpeg';

//upload image path
$upload_image = $target_path.$newFileName;





$exif = exif_read_data($upload_image, 'IFDO', true);
$orientation = $exif['IFD0']['Orientation'];;
if($orientation != 0) {
  $image = imagecreatefromstring(file_get_contents($upload_image));
  switch($orientation) {
       case 8:
          $image = imagerotate($image,90,0);
          break;
      case 3:
         $image = imagerotate($image,180,0);
         break;
      case 6:
         $image = imagerotate($image,-90,0);
         break;
   }
   imagejpeg($image, $upload_image);
}


//upload image
if(move_uploaded_file($_FILES[$field_name]['tmp_name'],$upload_image))
{
    //thumbnail creation
    if($thumb == TRUE)
    {


        $thumbnail = $thumb_path.$newFileName;
        list($width,$height) = getimagesize($upload_image);
        $thumb_create = imagecreatetruecolor($thumb_width,$thumb_height);
        switch($file_ext){
            case 'jpg':
                $source = imagecreatefromjpeg($upload_image);
                break;
            case 'jpeg':
                $source = imagecreatefromjpeg($upload_image);
                break;
            case 'png':
                $source = imagecreatefrompng($upload_image);
                break;
            case 'gif':
                $source = imagecreatefromgif($upload_image);
                break;
            default:
                $source = imagecreatefromjpeg($upload_image);
        }
            imagecopyresized($thumb_create,$source,0,0,0,0,$thumb_width,$thumb_height,$width,$height);
        switch($file_ext){
            case 'jpg' || 'jpeg':
                imagejpeg($thumb_create,$thumbnail,100);
                break;
            case 'png':
                imagepng($thumb_create,$thumbnail,100);
                break;
            case 'gif':
                imagegif($thumb_create,$thumbnail,100);
                break;
            default:
                imagejpeg($thumb_create,$thumbnail,100);
        }


    }

    return $fileName;
}
else
{
    return false;
}
}


if(!empty($_FILES['photo']['name'])){

//call thumbnail creation function and store thumbnail name
$upload_img = cwUpload('photo','','',TRUE,'thumbs/','352','264');

//full path of the thumbnail image
$thumb_src = 'thumbs/'.$upload_img;

  echo json_encode($message);

 }else{

//if form is not submitted, below variable should be blank
$thumb_src = '';
$message = '';
 }
 ?>

AND FINALLY THE ERROR FROM MY LOG

[09-Mar-2017 21:29:54 UTC] PHP Warning: Illegal string offset 'IFD0' in /home/imagine/public_html/galleries/main/galleries/test/uploading.php on line 35 [09-Mar-2017 21:29:54 UTC] PHP Warning: Illegal string offset 'Orientation' in /home/imagine/public_html/galleries/main/galleries/test/uploading.php on line 35 [09-Mar-2017 21:29:54 UTC] PHP Warning: file_get_contents(6dbaa3653321640d706c1c5bd281eed5.jpg): failed to open stream: No such file or directory in /home/imagine/public_html/galleries/main/galleries/test/uploading.php on line 37

If you call exif_read_data with section IFD0 , you should replace $exif['IFD0']['Orientation'] by $exif['Orientation'] since the array returned contains only IFD0 values.

Additionally, there is a typo in the code you provided: O should be 0 in IFDO here:

$exif = exif_read_data($upload_image, 'IFDO', true);

Eventually, check if Orientation is defined in EXIF data before using it:

if (isset($exif['Orientation']))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM