Google App Engine:如何在谷歌云存储的PHP循环中提供上传的图像

时间:2015-06-24 06:17:39

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

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

据我们所知,由于安全原因,Google不允许像普通平台一样上传图片。所以我使用谷歌云存储来上传图像,我已经通过在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>

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

2 个答案:

答案 0 :(得分:2)

由于我没有从任何人那里得到任何解决方案,所以我逐步挖掘了我的代码并找到了解决方案。 由于在循环中调用基本辅助函数时生成的路径不正确而导致错误。所以这里是可以使用的更新版本。把我的答案放在这里,因为我没有在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 Developers。有关我的更多信息,请访问www.iamabhishek.com

答案 1 :(得分:0)

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

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

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

我会很快发布适当的解决方案。随着新版本的库。