• Terbaru

    Code Igniter :: Upload File Base64 jadi image ke Google Cloud Storage

    Kondisi :: google storage sudah siap .

    download file json google dan taruh di bagian Application/third_party/.


    Environtment ::

    1. install komposer

    2. buka project di vscode dan ketik composer require google/cloud-storage ,

    3. Application/config/config.php  :: dibagian paling bawah file config.php tambahkan kode :

    $config['project_id'] = 'NAMAPROJECT';
    $config['bucket_name'] = $bucketname;
    $config['key_file_path'] = 'application/third_party/JSONFILE.json';
    $config['google_api_key'] = 'GOOGLE API KEY';
    .
    4. buat file di dalam folder application/libraries dengan nama file GoogleCloudStorage.php ,

    5.isi file GoogleCloudStorage.php,
    <?php
    defined('BASEPATH') or exit('No direct script access allowed');

    use Google\Cloud\Storage\StorageClient;


    class GoogleCloudStorage
    {

        protected $ci;

        public function __construct()
        {
            $this->ci = &get_instance();
        }

        public function uploadBase64Image($base64ImageData, $originalObjectName, $newObjectName)
        {
            $base64ImageData = str_replace('[removed]', '', $base64ImageData);
            $base64ImageData = str_replace('data:image/png;base64,', '', $base64ImageData);
            $base64ImageData = str_replace('data:image/jpeg;base64,', '', $base64ImageData);
            $imageData = base64_decode($base64ImageData);
            try {
                $storage = new StorageClient([
                    'projectId' => $this->ci->config->item('project_id'),
                    'keyFilePath' => $this->ci->config->item('key_file_path'),
                ]);
                $bucket = $storage->bucket($this->ci->config->item('bucket_name'));
                $bucket->upload(
                    $imageData,
                    [
                        'name' => $originalObjectName,
                    ]
                );
                $bucket->object($originalObjectName)->rename($newObjectName);
                return 'Image uploaded and renamed successfully to Google Cloud Storage.';
            } catch (Exception $e) {
                return 'Error: ' . $e->getMessage();
            }
        }
    }

    .
    note ::
    file base64 yang di upload tidak boleh ada encoding dari image nya,

    untuk mengeksekusi upload ketik :: 
    $filename = 'NAMAFOLDER/NAMAFILE. '.jpg';
    $this->googlecloudstorage->uploadBase64Image('BASE64 FILE', 'oldname.jpg', $filename);
    .

    function untuk melihat :: di dalam model (terserah model yang mana) ,
    misal di sini saya membuat model dengan nama m_function,
    di bagian paling atas file m_function tambahkan text: 
    use Google\Cloud\Storage\StorageClient;
    ,
    kemudian di dalam class M_function  tambahkan :
     public function __construct()
        {
            parent::__construct();
            $this->load->library('GoogleCloudStorage');
        }

    setelah itu buat function ::
    public function getImageUrl($objectName)
        {
            $bucketName = $this->bucketname();
            $storage = new StorageClient([
                'projectId' => $this->config->item('project_id'),
                'keyFilePath' => $this->config->item('key_file_path'),
            ]);
            $bucket = $storage->bucket($bucketName);
            $object = $bucket->object($objectName);
            $expires = new DateTime();
            $expires->modify('+1 hour');
            $signedUrl = $object->signedUrl($expires);
            return $signedUrl;
        }

    untuk mengeksekusi/membaca foto yang sudah terupload :
    $objectName = 'NAMA FOLDER/NAMA FILE. '.jpg';
    $img = $this->m_function->getImageUrl($objectName);
    .

    No comments

    Saya sangat berterimakasih apabila sahabat sekalian bersedia untuk tidak meninggalkan spam dan meninggalkan komentar yang berhubungan dengan artikel di atas.