Welcome Guest, Not a member yet? Register   Sign In
uploading PNG turns transparent background black (not transparent)
#1

(This post was last modified: 05-01-2022, 08:41 PM by spreaderman.)

I have been struggling to find a solution for this.  For some unknown reason, when I upload a png with a transparent background via my script, the background turns black. If I manually upload the same png with a transparent background, it works perfectly. Any pointers appreciated.


Code:
public function update_site_logo_app_setting(){
    $appSettingModel = model('\App\Models\AppSettingModel');
    if ($this->request->getMethod() == "post") {
        $validation =  \Config\Services::validation();   
        $rules = [
            "dh_site_logo" => [
                "label" => "Site Logo",
                "rules" =>
                'uploaded[dh_site_logo]'
                . '|is_image[dh_site_logo]'
                . '|mime_in[dh_site_logo,image/jpg,image/jpeg,image/gif,image/png,image/webp]'
                . '|max_size[dh_site_logo,200000]'
                . '|max_dims[dh_site_logo,500,150]',
            ],
        ];
        if ( $this->validate($rules) ){
            $x_file = $this->request->getFile('dh_site_logo');
            $x_file_name = $x_file->getName();
            $image = \Config\Services::image()
                    ->withFile($x_file)
                    ->save(FCPATH . $x_file_name);
            $data[] = array(
                'app_setting_key'  => 'dh_site_logo',
                'app_setting_value' => $x_file_name,
            );
            $count = $appSettingModel->update_general_app_setting_batch( $data, 'app_setting_key');
            if ($count > 0) {
                $this->cache_delete();
                return redirect()->to('/admin/settings')
                    ->with('info', 'Success - settings updated!');               
            } else {
                return redirect()->back()
                ->with('warning', 'Nothing to Update')
                ->withInput();
            }
        } else {
            // return the validation errors.
            return redirect()->back()
                ->with('errors', $validation->getErrors())
                ->with('warning', 'Invalid Data')
                ->withInput();
        }
    }
}
Reply
#2

Why not just save the file in a directory?
PHP Code:
$x_file $this->request->getFile('dh_site_logo');
$x_file->move(FCPATH); 
Reply
#3

(05-01-2022, 09:43 PM)iRedds Wrote: Why not just save the file in a directory?
PHP Code:
$x_file $this->request->getFile('dh_site_logo');
$x_file->move(FCPATH); 

Wow, over thought. Thank you very much iReddis. Worked.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB