2

I am new in CI. I am trying to upload webp images using PHP CI Framework, Can anyone give me suggestion about how to upload .webp instead of jpg?

brombeer
  • 8,716
  • 5
  • 21
  • 27
Maulik Nai
  • 19
  • 2
  • 7
  • 2
    What are you having trouble with? Is there something special about uploading .webp files? – brombeer Jul 14 '21 at 11:54
  • $config['allowed_types'] = '*'; //All Files// Using this I got the result but please let me know there is another way to upload webp file type. thanks in advance. – Maulik Nai Jul 14 '21 at 12:04
  • Ok, and _what_ doesn't work? Do you get any errors? .webp files not selectable? Using what code? – brombeer Jul 14 '21 at 12:05

2 Answers2

4

.webp image uploading in CodeIgniter 3

  • Add webp MIME Type in config file: ./application/config/mimes.php. 'webp' => array('image/webp'),

  • And in your upload config "allowed_types" add "webp" : $config['allowed_types'] = 'gif|jpg|jpeg|png|webp';

  • .webp image resizing, cropping, water marking goto https://github.com/shackeelck/webp_ci_3

-1

You can use file upload function like this in CI4:

function upload() {
    helper(['form', 'url']);

    $database = \Config\Database::connect();
    $db = $database->table('TABLE NAME');

    $input = $this->validate([
        'file' => [
            'uploaded[file]',
            'mime_in[file,image/jpg,image/jpeg,image/png,image/webp]',
            'max_size[file,1024]',
        ]
    ]);

    if (!$input) {
        print_r('Choose a valid file');
    } else {
        $img = $this->request->getFile('file');
        $img->move(WRITEPATH . 'uploads');

        $data = [
           'name' =>  $img->getName(),
           'type'  => $img->getClientMimeType()
        ];

        $save = $db->insert($data);
        print_r('File has successfully uploaded');
    }
}
Mahmud Hasan Jion
  • 445
  • 1
  • 5
  • 14
  • Thanks for your code but I am using CI-3 so mime_type webp not available – Maulik Nai Jul 14 '21 at 12:09
  • I think you might want to give a look [Codeigniter 3 does not upload jpeg image](https://stackoverflow.com/a/58616391/10030431) – Mahmud Hasan Jion Jul 14 '21 at 12:16
  • @MahmudHasanJion You can't just put it there and say it's a valid image. `'mime_in[file,image/jpg,image/jpeg,image/png,image/webp]'` it doesn't work – starlings Sep 13 '22 at 20:10