Resize image does nothing in a CodeIgniter4 app

37 Views Asked by At

I am working on a project, I try to create a feature that resize an image previously sent in a form.

The form works correctly and I can display the images on a view.

In a little gallery, there are some buttons, one is a delete button and the other one is for the resize.

In my database, I have an attribute which is isFront, a boolean.

The way I want my feature to work is if isFront = 1 I do the resize else I delete resized image but first I am working on the resize.

Here is my function.

To get more context, in my database, "medias" is a table linked with "actualites" (French word for news).

In the table "medias" I save the local path & and the web path.

To get the folder where I want to save the resized image, I want to keep the same folder than the original image with the _min at the end of the image (that way, I am not lost in the code).

So I get no errors but nothing happens.

I don't know where is my error.

GD is installed.

Also, I am working with CodeIgniter4, Wamp64, phpMyAdmin.

public function getHandleIsFront($id){

    helper('url');

    $media = $this->mediasModel->where('idMedia', $id)->first();

    if($media) {

        $pathLocal = $media['localPathMedia'];

        $pathInfo = pathinfo($pathLocal);

        $minFileName = $pathInfo['filename'] . "_min." . $pathInfo['extension'];

        $minPath = $pathInfo['dirname'] . "\\" . $minFileName;

        $idNews = $media['idNews'];

        $tabIsFront = [
            'isFront' => !$media['isFront']
        ];
    
        $this->mediasModel->where('idMedia',$id)->set($tabIsFront)->update();

        if($media['isFront'] == 1) {

            try {
                $image = \Config\Services::image()
                    ->withFile($pathLocal)
                    ->resize(350, 255.4, true, 'height')
                    ->save($minPath);
            } catch (\Exception $e) {
                
                $errorMessage = $e->getMessage();
                
                echo "Erreur lors du redimensionnement de l'image : $errorMessage";
                
            }
        }

    } else {

        return redirect()->to('/Admin/Adminhome');

    }

    return redirect()->to('/Admin/Medias/listMedias?id=' . $idNews);

}

I tried with & without the try catch, nothing changed, I tried to echo the paths, both seems fine.

0

There are 0 best solutions below