【问题标题】:imagejpeg causing file type 'binary/octet-stream' in GCSimagejpeg 导致 GCS 中的文件类型为“二进制/八位字节流”
【发布时间】:2014-12-26 19:44:11
【问题描述】:

我有一个 GAE php 脚本,它旋转给定的图像(存储在 GCS 中),然后将其写入它源自的同一个 GCS 存储桶(都在同一个 Cloud Platform 项目中)。

一切正常 - 图像已正确加载、旋转并保存。问题是,当图像被保存时,GCS 将它的类型识别为binary/octet-stream 而不是image/jpeg,这会导致任何公共链接自动下载图像文件,而不是直接在浏览器中显示。

代码:

    $bucket = 'gs://my-bucket-here/';
    $imageRes = imagecreatefromjpeg($bucket . $picName);

    if ($imageRes) {
        $rotate = imagerotate($imageRes, 90, 0);
        imagedestroy($imageRes);
        imagejpeg($rotate, $bucket . "test.jpg");
        imagedestroy($rotate);
    }

有什么提示是什么原因造成的吗?我考虑过使用 Imagick,但它在 GAE 中不可用...

【问题讨论】:

    标签: php google-app-engine google-cloud-storage


    【解决方案1】:

    您可以在上下文选项中指定内容类型,在这种情况下,您需要在默认选项中设置它,因为无法将上下文传递给imagejpeg() 方法。

    $options = [
      'gs' => [
          'Content-Type' => 'image/jpeg',
      ],
    ];
    
    stream_context_set_default($options);
    
    $bucket = 'gs://my-bucket-here/';
    $imageRes = imagecreatefromjpeg($bucket . $picName);
    
    if ($imageRes) {
      $rotate = imagerotate($imageRes, 90, 0);
      imagedestroy($imageRes);
      imagejpeg($rotate, $bucket . "test.jpg");
      imagedestroy($rotate);
    }
    

    【讨论】:

      猜你喜欢
      • 2020-07-09
      • 2016-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-05
      • 2021-11-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多