简体   繁体   English

如何从Google Cloud Storage提供SVG图像?

[英]How can I serve an SVG image from Google Cloud Storage?

Right now I'm working on allowing user image uploads to my site using the Google Cloud Storage. 目前,我正在努力允许使用Google Cloud Storage将用户图像上传到我的网站。 Uploading regular image files such as jpg, png, gif, and webp works fine. 上载常规图像文件(例如jpg,png,gif和webp)效果很好。 However, SVG images do not work. 但是,SVG图像不起作用。 They get uploaded ok but when I have the PHP code echo the URL as an image source, all browsers just display the missing image icon. 他们可以正常上传,但是当我让PHP代码将URL作为图像源回显时,所有浏览器仅显示缺少的图像图标。 However, it does appear as if the image is downloading in the network tab of the code inspector. 但是,它确实好像在映像检查器的“网络”选项卡中正在下载映像。 Not only that, pasting the link into it's own tab causes the file to download. 不仅如此,将链接粘贴到其自己的选项卡中还会导致文件下载。 This makes me think that the server is telling the browser to download the file rather than serve it as an image. 这使我认为服务器正在告诉浏览器下载文件而不是将其用作映像。 Here is the code that I am using: 这是我正在使用的代码:

include 'GDS/GDS.php';
//create datastore
$obj_store = new GDS\Store('HomeImages');
$bucket = CloudStorageTools::getDefaultGoogleStorageBucketName();
$root_path = 'gs://' . $bucket . '/' . $_SERVER["REQUEST_ID_HASH"] . '/';

$public_urls = [];
//loop through all files that are images
foreach($_FILES['images']['name'] as $idx => $name) {
    if ($_FILES['images']['type'][$idx] === 'image/jpeg' || $_FILES['images']['type'][$idx] === 'image/png' || $_FILES['images']['type'][$idx] === 'image/gif' || $_FILES['images']['type'][$idx] === 'image/webp' || $_FILES['images']['type'][$idx] === 'image/svg+xml') {
        //path where the file should be moved to
        $original = $root_path . 'original/' . $name;
        //move the file
        move_uploaded_file($_FILES['images']['tmp_name'][$idx], $original);

        //don't use the getImageServingUrl function on SVG files because they aren't really images
        if($_FILES['images']['type'][$idx] === 'image/svg+xml')
            $public_urls[] = [
                'name' => $name,
                'original' => CloudStorageTools::getPublicUrl($original, true),
                'thumb' => CloudStorageTools::getPublicUrl($original, true),
                'location' => $original
            ];
        else
            $public_urls[] = [
                'name' => $name,
                'original' => CloudStorageTools::getImageServingUrl($original, ['size' => 1263, 'secure_url' => true]),
                'thumb' => CloudStorageTools::getImageServingUrl($original, ['size' => 150, 'secure_url' => true]),
                'location' => $original
            ];
      }
}
//store image location and name in the datastore
foreach($public_urls as $urls){
    $image = new GDS\Entity();
    $image->URL = $urls['original'];
    $image->thumbURL = $urls['thumb'];
    $image->name = $urls['name'];
    $image->location = $urls['location'];
    $obj_store->upsert($image);
}
//redirect back to the admin page
header('Location: /admin/homeimages');

Having run into this issue just now, I found a solution. 刚刚遇到此问题,我找到了解决方案。 It turns out that every file in a bucket has metadata attached and stored as key-value pairs. 事实证明,存储桶中的每个文件都有附加的元数据并存储为键值对。 The key we're after is 'Content-Type', and the value isn't always correct for SVG. 我们需要的键是“ Content-Type”,并且该值对于SVG并不总是正确的。 the value needs to be 'image/svg+xml'. 该值必须为“ image / svg + xml”。 I don't know how to set that programmatically, but if you only have a few objects, then it's easy to do in the file's ellipses menu in the online interface for the bucket. 我不知道如何以编程方式进行设置,但是如果您只有几个对象,则可以在存储桶在线界面的文件椭圆菜单中轻松进行设置。

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

相关问题 Google App Engine:如何在 PHP 循环中从 Google 云存储提供上传的图像 - Google App Engine: How to serve uploaded images in a PHP loop from google cloud storage 如何使用PHP从Google云端存储中删除对象? - How can I delete an object from the Google Cloud Storage using PHP? 谷歌云存储:我如何避免边缘缓存 - Google cloud storage : How can I Avoid edge cache 如何使用PHP备份服务器文件将文件从服务器上传到Google云存储到Google服务器? - How can I upload files to google cloud storage from my server to google's server using PHP to backup my server files? GAE PHP提供云存储中的图像 - Gae php serve images from cloud storage 我可以从不是Google App Engine的网站上编写/读取Google Cloud Storage吗? - Can I write/read Google Cloud Storage from my website which is not a Google App Engine? 通过 Google Cloud Storage 中的 PHP 服务器提供文件的最有效方法? - Most efficient way to serve files through a PHP server from Google Cloud Storage? PHP Google App Engine从云端存储中永久删除图像 - PHP Google App Engine permanently delete Image from Cloud Storage 使用PHP从Google Cloud Storage上传和检索图像 - Upload and Retrieve Image from Google Cloud Storage in PHP 如何通过Google App Engine PHP脚本连接到Google Cloud Storage? - How do I connect to Google Cloud Storage from a Google App Engine PHP script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM