GD调整大小PHP后的S3图像上传

时间:2014-11-28 01:56:15

标签: php amazon-s3 gd

我有将代码上传到Amazon S3的工作代码

工作代码......

    $filename = $s."/avatars/".$user_id."-".$file['name'];        
    $path_parts = pathinfo($file['name']);
    $ext = $path_parts['extension'];

    $input_file = S3::inputFile($file['tmp_name'], false);

    if(S3::putObject($input_file, 'mybucket', $filename, S3::ACL_PUBLIC_READ)){
      //Success handling
    } else {
      // Error handling
    }

但是,当我使用GD调整大小并尝试上传时,它将无法再工作。

取消工作代码......

    $size = getimagesize($file['tmp_name']);
    $x = (int) $input['x'];
    $y = (int) $input['y'];
    $w = (int) $input['w'] ? $input['w'] : $size[0];
    $h = (int) $input['h'] ? $input['h'] : $size[1];

    $data = file_get_contents($file['tmp_name']);
    $vImg = imagecreatefromstring($data);
    $dstImg = imagecreatetruecolor($nw, $nh);
    imagecopyresampled($dstImg, $vImg, 0, 0, $x, $y, $nw, $nh, $w, $h);

      ob_start();
      imagejpeg($dstImg, null, 100);
      $jpeg = ob_get_contents();
      ob_end_clean();

      //Send to S3
      $input = S3::inputFile($jpeg, false);

      if(S3::putObject($input, 'mybucket', $filename, S3::ACL_PUBLIC_READ)){
          // Success handling
      } else {
         // Error handling
      }

我收到的错误讯息是......

S3::inputFile(): Unable to open input file: ÿØÿà

任何想法?我错误地得到了流吗?调整大小后我应该做些不同的事吗?我甚至要阅读流吗?或者我可以发送$ dstImg资源吗?

非常感谢任何帮助

1 个答案:

答案 0 :(得分:2)

对于其他有这个问题的人。 我解决了这个问题

改变了这个......

  ob_start();
  imagejpeg($dstImg, null, 100);
  $jpeg_to_upload = ob_get_contents();
  ob_end_clean();

对此...

$jpeg_to_upload = tempnam(sys_get_temp_dir(), "tempfilename");
imagejpeg($dstImg, $jpeg_to_upload);

然后发送了它......

$input = S3::inputFile($jpeg_to_upload, false);
if(S3::putObject($input, 'mybucket', $filename, S3::ACL_PUBLIC_READ)){
相关问题