簡體   English   中英

Google App Engine:如何在 PHP 循環中從 Google 雲存儲提供上傳的圖像

[英]Google App Engine: How to serve uploaded images in a PHP loop from google cloud storage

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

正如我們所知,出於安全原因,谷歌不允許像一般平台一樣上傳圖片。 因此,我使用 Google Cloud Storage 上傳圖片,並通過在 Codeigniter 中創建一個 septate 庫成功實現了它。

我遇到的問題是如何在 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>

如果有人遇到同樣的問題,請留下您的答案。

由於我沒有從任何人那里得到任何解決方案,所以我一步一步地研究了我的代碼並找到了解決方案。 由於在循環中調用基本輔助函數時生成的路徑不正確,我得到了錯誤。 所以這里是可以使用的更新版本。 將我的答案放在這里是因為我沒有在 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

}

?>

希望這篇文章可以對谷歌開發者有所幫助。 更多關於我的信息,你可以在這里找到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' ;

每次當循環執行並第一次創建圖像路徑時,創建的圖像路徑是正確的,但是在第一次索引后,無論何時路徑 make 生成它都不會替換以前生成的路徑,而不是附加舊的路徑變量。

我會盡快發布適當的解決方案。 隨着新版本的庫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM