Welcome Guest, Not a member yet? Register   Sign In
Storing image bytea type in postgre db
#1

Hi Masta

i have try insert to postgre db with bytea type, but when storing that image still fail below my code

this code when inserting into table
Code:
public function viewPhotograph($id)
    {

        if($this->request->getMethod() == 'post')
        {
            $file = $this->request->getFile('emp_poto');
            $name = $file->getName();
            $tempfile = $file->getTempName();
            $type = $file->getClientMimeType();
            $ext = $file->getExtension();
            $image_size    = $file->getSize('kb');
            $size = getimagesize($tempfile);
            list($width, $height) = $size;

            $epic_model = new \App\Models\Employee\EpicModel();

            $allowed_type = array('image/png', 'image/jpg' , 'image/gif' , 'image/jpeg');
            $allowed_size = 1000000; // in Byte

            if(!in_array($type, $allowed_type)){
                $this->resp_json['status'] = 'error';
                $this->resp_json['msg'] = "Image type does not allowed ".$type;
                return $this->response->setJSON( $this->resp_json);
            }

            $real_integer = filter_var($image_size, FILTER_SANITIZE_NUMBER_INT);

            if(!is_numeric($real_integer) || $real_integer > $allowed_size){
                $this->resp_json['status'] = 'error';
                $this->resp_json['msg'] = "Image size Maximum ".$allowed_size." Kb current size ".$real_integer;
                return $this->response->setJSON( $this->resp_json);
            }

            $image_size .= ' Kb';
            $img = pg_escape_bytea(file_get_contents($tempfile));
            $data = [
                'emp_number'            => $id,
                'epic_filename'          => $name,
                'epic_type'          => $type,
                'epic_picture'  => $img,
                'epic_file_size' => $image_size,
                'epic_file_width'=> $width,
                'epic_file_height'            => $height
            ];
           
           
            if ($epic_model->insert($data, false) === false)
            {
                $this->resp_json['status'] = 'error';
                $this->resp_json['msg'] = array($epic_model->errors());
                return $this->response->setJSON( $this->resp_json);
            }
           
            $this->resp_json['status'] = 'success';
            $this->resp_json['msg'] = "success";
            return $this->response->setJSON($this->resp_json);

        }
       
    }

when storing i create custome function like below

Code:
if(!function_exists('img_from_bytea')){
    function img_from_bytea($raw, $type) {
        
        header('Content-type: '.$type);
        return pg_unescape_bytea($raw);
    }
}


and then call that function

Code:
echo img_from_bytea($epic_detail[0]['epic_picture'], $epic_detail[0]['epic_type']);

//and try


<img src="<?php echo img_from_bytea($epic_detail[0]['epic_picture'], $epic_detail[0]['epic_type'])?>" class="img-circle" width="200">

and not working

Thank you
Reply




Theme © iAndrew 2016 - Forum software by © MyBB