gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 21:03:43 25 4
gpt4 key购买 nike

现在,我正在努力允许用户使用 Google 云存储将图像上传到我的网站。上传常规图像文件(例如 jpg、png、gif 和 webp)效果很好。但是,SVG 图像不起作用。它们上传正常,但是当我让 PHP 代码将 URL 作为图像源回显时,所有浏览器都只显示丢失的图像图标。但是,看起来好像图像正在代码检查器的网络选项卡中下载。不仅如此,将链接粘贴到其自己的选项卡中会导致文件下载。这让我认为服务器告诉浏览器下载文件而不是将其作为图像提供。这是我正在使用的代码:

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');

最佳答案

刚刚遇到这个问题,我找到了解决方案。事实证明,存储桶中的每个文件都附加了元数据并存储为键值对。我们要查找的键是“Content-Type”,该值对于 SVG 并不总是正确的。该值必须是“image/svg+xml”。我不知道如何以编程方式进行设置,但如果您只有几个对象,那么在存储桶在线界面的文件省略号菜单中很容易完成。

关于php - 如何从 Google Cloud Storage 提供 SVG 图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37050352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com