GoogleAppEngine:如何在Google云存储的PHP循环中为上传的图像提供服务
我一直在使用谷歌应用引擎平台上托管的PHP Codeigniter框架 正如我们所知,由于安全原因,谷歌不允许像通用平台一样上传图像。因此,我使用谷歌云存储上传图像,并通过在Codeigniter中创建一个septate库成功实现了这一功能 我遇到的问题是如何在foreach或for循环中为它们提供服务器。若我调用图像一次,它显示在网页上,但若我尝试在循环中实现该代码,当循环计数器转到第二个计数时,它将停止。下面是我创建并在循环中调用的库和助手函数 库:GoogleAppEngine:如何在Google云存储的PHP循环中为上传的图像提供服务,php,image,codeigniter,google-app-engine,google-cloud-storage,Php,Image,Codeigniter,Google App Engine,Google Cloud Storage,我一直在使用谷歌应用引擎平台上托管的PHP Codeigniter框架 正如我们所知,由于安全原因,谷歌不允许像通用平台一样上传图像。因此,我使用谷歌云存储上传图像,并通过在Codeigniter中创建一个septate库成功实现了这一功能 我遇到的问题是如何在foreach或for循环中为它们提供服务器。若我调用图像一次,它显示在网页上,但若我尝试在循环中实现该代码,当循环计数器转到第二个计数时,它将停止。下面是我创建并在循环中调用的库和助手函数 库: <?php defined
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
use google\appengine\api\cloud_storage\CloudStorageTools;
class Display {
var $config = Array();
var $image_url = '';
var $folder = '';
var $image = '';
var $size = 400;
var $crop = true;
public function __construct(){
$bucket = CloudStorageTools::getDefaultGoogleStorageBucketName();
$this->image_url = 'gs://' . $bucket . '/uploads/';
}
public function image_url(){
$path = $this->image_url.$this->folder.'/';
$this->image_url = ( !empty($this->image) && is_file( $path . $this->image)) ?$path .$this->image:'noimage_thb.png' ;
$image_url = @CloudStorageTools::getImageServingUrl($this->image_url);
/*printArray($this->image_url,1);*/
if($image_url == ''){
$image_url = '';
}
return $image_url;
}
}
?>
if (!function_exists('image_url')) {
function image_url($folderName,$image, $size, $crop = false)
{
$CI =& get_instance();
// we first load the upload library
$CI->load->library('display');
$CI->display->folder = $folderName;
$CI->display->image = $image;
$CI->display->size = $size;
$CI->display->crop = $crop;
$url = $CI->display->image_url();
return $url;
}
}
<div class="fotorama" data-allowfullscreen="true" data-nav="thumbs">
<?php
$images = explode(",", $dbdata['beach_photo']);
for ($img_no = 0; $img_no < count($images); $img_no++) {
$imageUrl = image_url('venue', $images[$img_no], '400');
?>
<img src="<?php echo $imageUrl; ?>" class="img-responsive"/>
<?php } ?>
</div>
查看文件代码(循环):
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
use google\appengine\api\cloud_storage\CloudStorageTools;
class Display {
var $config = Array();
var $image_url = '';
var $folder = '';
var $image = '';
var $size = 400;
var $crop = true;
public function __construct(){
$bucket = CloudStorageTools::getDefaultGoogleStorageBucketName();
$this->image_url = 'gs://' . $bucket . '/uploads/';
}
public function image_url(){
$path = $this->image_url.$this->folder.'/';
$this->image_url = ( !empty($this->image) && is_file( $path . $this->image)) ?$path .$this->image:'noimage_thb.png' ;
$image_url = @CloudStorageTools::getImageServingUrl($this->image_url);
/*printArray($this->image_url,1);*/
if($image_url == ''){
$image_url = '';
}
return $image_url;
}
}
?>
if (!function_exists('image_url')) {
function image_url($folderName,$image, $size, $crop = false)
{
$CI =& get_instance();
// we first load the upload library
$CI->load->library('display');
$CI->display->folder = $folderName;
$CI->display->image = $image;
$CI->display->size = $size;
$CI->display->crop = $crop;
$url = $CI->display->image_url();
return $url;
}
}
<div class="fotorama" data-allowfullscreen="true" data-nav="thumbs">
<?php
$images = explode(",", $dbdata['beach_photo']);
for ($img_no = 0; $img_no < count($images); $img_no++) {
$imageUrl = image_url('venue', $images[$img_no], '400');
?>
<img src="<?php echo $imageUrl; ?>" class="img-responsive"/>
<?php } ?>
</div>
“class=”img responsive“/>
如果有人遇到同样的问题,请留下您的答案。我发现了问题,问题就在我的图书馆里:
$path = $this->image_url.$this->folder.'/';
$this->image_url =
( !empty($this->image) && is_file($path .$this->image)) ?$path.$this->image:'noimage_thb.png' ;
每次循环执行并创建映像路径时,第一次创建映像路径时,映像路径是正确的,但在第一次索引后,每当path make生成时,它并没有替换先前生成的路径,而是附加了较旧的path变量
我将很快发布正确的解决方案。使用新版本的库。因为我没有从任何人那里得到任何解决方案,所以我一步一步地深入我的代码并找到了解决方案。 由于在循环中调用basic helper函数时生成的路径不正确,所以我得到了一个错误。下面是可以使用的更新版本。将我的答案放在这里,因为我没有在OOPs概念或库形式中找到Google Cloud示例 库:(Display.php) 查看:(如何使用)
“class=”img responsive“/>
希望这篇文章能对谷歌开发者有所帮助。关于我的更多信息,您可以在这里找到任何错误或有任何输出???@Abdulla是的,我在控制台中收到错误,但不在页面上。我收到“NetworkError:500内部服务器错误-http://*********.appspot.com/venue/venueDetailes/elements mall斋浦尔”当文件不存在,但我交叉检查了文件是否存在,并且在打印$images数组的secong键元素时也会显示该文件。问题在于您的URL或您所在的数据passing@Abdulla我想这并不是因为我执行了为$image数组中初始化的图像生成的每个url$图像=爆炸(“,”,$dbdata['beach_photo']);在这个变量中,我得到0到2的数组索引值,并对所有三个元素执行。若我执行单个调用,它不会给出任何错误,但每当我进行多个调用时,它会在控制台和PHP脚本中给出错误get halt,并且不会执行剩余的脚本。希望这有意义。