简体   繁体   English

从iOS上传PHP

[英]PHP Uploading from iOS

Sorry for the long dilemma. 很抱歉长期困扰您。 I write iOS projects and have just started getting into PHP. 我编写iOS项目,并且刚开始使用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. 我已经开发了一个Xcode项目,可以将照片上传到网络文件夹,该文件夹会调用来自在线php脚本的响应。 But the response returns NSLog (@"No response given"). 但是响应返回NSLog(@“未给出响应”)。 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. 我修改了php脚本,该脚本将照片从我的应用程序上传到主文件夹,创建了一个随机文件名,然后将其复制到子文件夹中。 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. 我正在尝试在Xcode项目中获得响应,以将路径返回到主图像上载位置。 Here are my codes 这是我的代码

XCODE (Uploads perfectly fine but returns "No response given" instead of "My Image Upload Path" 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 (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 CODE(上传,创建随机文件名,将照片复制到thumbs文件夹中非常好,但是在上传时照片被翻转了,我的日志中出现了一些错误,我需要在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 = '';
 }
 ?>

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 [09-Mar-2017 21:29:54 UTC] PHP警告:/home/imagine/public_html/galleries/main/galleries/test/uploading.php在第35行上的字符串偏移量'IFD0'不合法[2017年3月9日21:29:54 UTC] PHP警告:第35行/home/imagine/public_html/galleries/main/galleries/test/uploading.php中的字符串偏移量'Orientation'无效[09-Mar-2017 21:29:54 UTC ] PHP警告:file_get_contents(6dbaa3653321640d706c1c5bd281eed5.jpg):无法打开流:在第37行的/home/imagine/public_html/galleries/main/galleries/test/uploading.php中没有此类文件或目录

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. 如果使用IFD0节调用exif_read_data ,则应将$exif['IFD0']['Orientation']替换$exif['IFD0']['Orientation'] $exif['Orientation']因为返回的数组仅包含IFD0值。

Additionally, there is a typo in the code you provided: O should be 0 in IFDO here: 此外,您提供的代码中有一个错字: IFDO在此处的O应该为0

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

Eventually, check if Orientation is defined in EXIF data before using it: 最终,在使用前检查是否在EXIF数据中定义了Orientation

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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