繁体   English   中英

从iOS上传PHP

[英]PHP Uploading from iOS

很抱歉长期困扰您。 我编写iOS项目,并且刚开始使用PHP。 这是我的问题。 我已经开发了一个Xcode项目,可以将照片上传到网络文件夹,该文件夹会调用来自在线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(上传,创建随机文件名,将照片复制到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 = '';
 }
 ?>

并最终从我的日志中发现错误

[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中没有此类文件或目录

如果使用IFD0节调用exif_read_data ,则应将$exif['IFD0']['Orientation']替换$exif['IFD0']['Orientation'] $exif['Orientation']因为返回的数组仅包含IFD0值。

此外,您提供的代码中有一个错字: IFDO在此处的O应该为0

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

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

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

暂无
暂无

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

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