【问题标题】:Google App Engine: How to serve uploaded images in a PHP loop from google cloud storageGoogle App Engine:如何在 PHP 循环中从谷歌云存储中提供上传的图像
【发布时间】:2015-09-10 05:01:02
【问题描述】:

我一直在使用托管在 Google App Engine 平台上的 PHP Codeigniter 框架。

据我们所知,出于安全原因,Google 不允许像一般平台一样上传图片。所以我使用 Google Cloud Storage 上传图片,并通过在 Codeigniter 中创建一个独立的库成功实现了它。

我遇到的问题是如何在 foreach 或 for 循环中为它们提供服务。如果我调用图像一次而不是显示在网页上,但是如果我尝试在循环中实现该代码,它会在循环计数器进入秒计数时停止。下面是我创建并循环调用的库和 Helper 函数。

图书馆:

    <?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>

如果有人遇到同样的问题,请留下您的答案。

【问题讨论】:

  • 任何错误或有任何输出??
  • @Abdulla 是的,我在控制台中收到错误,但在页面上没有。我收到“NetworkError:500 Internal Server Error - http://*******.appspot.com/venue/venueDetailes/elements-mall-jaipur”当文件不存在但我交叉检查文件时出现当我打印 $images 数组的 secong 键元素时也会显示。
  • 问题在于您的 URL 或您传递的数据
  • @Abdulla 我认为这不是因为我个人执行了为 $image 数组中初始化的图像生成的每个 url。 $images = explode(",", $dbdata['beach_photo']);在这个变量中,我得到 0 到 2 的数组索引值,并且我对所有三个元素都执行了。如果我执行单个调用,它不会给出任何错误,但是每当我进行多个调用时,它会在控制台中给出错误,并且 PHP 脚本会停止并且不执行剩余的脚本。希望这是有道理的。

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


【解决方案1】:

由于我没有从任何人那里得到任何解决方案,所以我一步一步地挖掘我的代码并找到了解决方案。 由于在循环中调用基本辅助函数时路径生成不正确,我得到了错误。所以这里是可以使用的更新版本。将我的答案放在这里,因为我没有找到 OOP 概念或库形式的 Google Cloud 示例。

库:(Display.php)

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
use google\appengine\api\cloud_storage\CloudStorageTools;

class Display
{

    public $config = Array();
    public $image_url = '';
    public $folder = '';
    public $image = '';
    public $size = 400;
    public $path = '';
    public $crop = true;
    public $bucket = '';

    /**
     * Constructor
     *
     * @access    public
     */
    public function __construct($props = array())
    {
        $this->bucket = CloudStorageTools::getDefaultGoogleStorageBucketName();
        if (count($props) > 0) {
            $this->initialize($props);
        }

        log_message('debug', "Upload Class Initialized");
    }

    // --------------------------------------------------------------------

    /**
     * Initialize preferences
     *
     * @param    array
     * @return    void
     */
    public function initialize($config = array())
    {
        $defaults = array(
            'folder' => 0,
            'image' => 0,
            'size' => 600,
            'crop' => false
        );
        foreach ($defaults as $key => $val) {
            if (isset($config[$key])) {
                $method = 'set_' . $key;
                if (method_exists($this, $method)) {
                    $this->$method($config[$key]);
                } else {
                    $this->$key = $config[$key];
                }
            } else {
                $this->$key = $val;
            }
        }

        $this->image_url = 'gs://' . $this->bucket . '/uploads/';
    }

    public function set_image($image)
    {
        $this->image = $image;
    }

    public function set_folder($folder)
    {
        $this->folder = $folder;
    }

    public function set_crop($n)
    {
        $this->crop = $n;
    }

    public function set_size($n)
    {
        $this->size = (int) $n;
    }

    public function image_url()
    {
        $this->path = $this->image_url . $this->folder . '/';
        /*printArray($this->size.' AND '.$this->crop,1);*/
        $this->image_url = (!empty($this->image) && is_file($this->path . $this->image)) ? $this->path . $this->image : $this->path . 'noimage_thb.png';
        $image_url = @CloudStorageTools::getImageServingUrl($this->image_url, ['size' => (int) $this->size, 'crop' => $this->crop]);
        if ($image_url == '') {
            $image_url = '';
        }
        return $image_url;
    }

    public function delete_image()
    {
        $this->path = $this->image_url . $this->folder . '/';
        /*printArray($this->size.' AND '.$this->crop,1);*/
        $this->image_url = (!empty($this->image) && is_file($this->path . $this->image)) ? $this->path . $this->image : $this->path . 'noimage_thb.png';
        $image_url = @CloudStorageTools::deleteImageServingUrl($this->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');
        $config['folder'] = $folderName;
        $config['size'] = $size;
        $config['crop'] = $crop;
        $config['image'] = $image;
        $CI->display->initialize($config);
        $url = $CI->display->image_url($image);
        return $url;
    }
}

查看:(如何使用)

<?php

$images = explode(",", $dbdata['beach_photo']);

for ($img_no = 0; $img_no < count($images); $img_no++) {

$imageUrl = image_url('venue', $images[$img_no], '1200', true);

?>

<img src="<?php echo $imageUrl; ?>" class="img-responsive"/>

<?php

}

?>

希望这篇文章对 Google 开发者有所帮助。更多关于我的信息,你可以在这里找到www.iamabhishek.com

【讨论】:

    【解决方案2】:

    我发现了问题,问题就在我的库中:

        $path = $this->image_url.$this->folder.'/';
         $this->image_url  = 
    ( !empty($this->image) &&  is_file($path .$this->image)) ?$path.$this->image:'noimage_thb.png' ;
    

    每次循环执行并创建图像路径时,第一次创建的图像路径是正确的,但在每次生成路径时第一次索引后,它并没有替换先前生成的路径,而是附加旧的路径变量。

    我会尽快发布适当的解决方案。使用新版本的库。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-20
      • 1970-01-01
      • 1970-01-01
      • 2017-05-01
      • 1970-01-01
      • 2016-12-30
      • 2017-05-04
      相关资源
      最近更新 更多