从iOS

时间:2017-03-09 22:01:49

标签: php ios

对于漫长的困境感到抱歉。我编写iOS项目并刚开始使用PHP。这是我的问题。我开发了一个Xcode项目,将照片上传到一个web文件夹,该文件夹调用来自在线php脚本的响应。但是响应返回NSLog(@“没有给出响应”)。我修改了将我的应用程序中的照片上传到主文件夹的php脚本,创建了一个随机文件名,然后将其复制到子文件夹中。这一切都很完美。但是有两个主要问题。一。上传的图像水平翻转(镜像)。还有两个。我试图在我的Xcode项目中获得响应,以返回主图像上传位置的路径。这是我的代码

XCODE(上传完全正常但返回“没有给出响应”而不是“我的图片上传路径”

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(上传,创建随机文件名,将照片复制到thumb文件夹中完全没问题,但上传时照片被翻转,我的日志中出现了一些错误,我需要有一个响应显示图像的路径Xcode响应。)

<?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 = '';
 }
 ?>

最后来自我的日志的错误

  

[09-Mar-2017 21:29:54 UTC] PHP警告:非法字符串偏移'IFD0'   在   /home/imagine/public_html/galleries/main/galleries/test/uploading.php   第35行[09-Mar-2017 21:29:54 UTC] PHP警告:非法字符串   偏移'方向'   /home/imagine/public_html/galleries/main/galleries/test/uploading.php   第35行[09-Mar-2017 21:29:54 UTC] PHP警告:   file_get_contents(6dbaa3653321640d706c1c5bd281eed5.jpg):未能   open stream:没有这样的文件或目录   /home/imagine/public_html/galleries/main/galleries/test/uploading.php   第37行

1 个答案:

答案 0 :(得分:0)

如果您使用exif_read_data部分致电IFD0,则应将$exif['IFD0']['Orientation']替换为$exif['Orientation'],因为返回的数组仅包含IFD0个值。

此外,您提供的代码中存在拼写错误:O 0位于此IFDO

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

最终,在使用之前检查EXIF数据中是否定义了Orientation

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