谷歌应用程序引擎:如何在PHP循环中提供从谷歌云存储上传的图像



我一直在使用托管在谷歌应用引擎平台上的PHP Codeigner框架。

正如我们所知,由于安全原因,谷歌不允许像普通平台一样上传图像。因此,我使用谷歌云存储上传图像,并通过在Codeigniter中创建一个sepate库成功实现了这一点。

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

库:

    <?php
defined('BASEPATH') OR exit('No direct script access allowed');
use googleappengineapicloud_storageCloudStorageTools;
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>

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

由于我没有从任何人那里得到任何解决方案,所以我一步一步地研究我的代码并找到了解决方案由于在循环中调用基本助手函数时路径生成不正确,导致出现错误。所以这里是可以使用的更新版本。把我的答案放在这里,因为我没有在OOP概念或库形式中找到谷歌云的例子

库:(Display.php)

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
use googleappengineapicloud_storageCloudStorageTools;
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
}
?>

希望这篇文章能对谷歌开发者有所帮助。更多关于我的信息,请访问www.iamabhishek.com

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

    $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生成时,它都不会替换以前生成的路径,而是附加旧的路径变量。

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

最新更新