rosell-dk / webp-convert

Convert jpeg/png to webp with PHP (if at all possible)
MIT License
576 stars 101 forks source link

Error occured while converting png to webp #337

Closed inajaf closed 2 years ago

inajaf commented 2 years ago

When trying to convert png images to webp error occured: Warning: imagecreatefrompng(): gd-png: libpng warning: Interlace handling should be turned on when using png_read_image and for some png filles the following error occured: imagecreatefrompng(): gd-png: libpng warning: iCCP: known incorrect sRGB profile

Снимок экрана 2022-05-06 114431

Here is my class: which scans uploads folder and subfolder for image files for converting to webp:

<?php

namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

use WebPConvert\Convert\Converters\Gd;
use WebPConvert\Convert\Exceptions\ConversionFailedException;

class TestController
{

    /**
     * @throws ConversionFailedException
     */
    #[Route('/')]
    public function homepage(): Response
    {

        $source = __DIR__ . '\uploads';
        $results = $this->getDirContents($source);

        foreach ($results as $key => $value) {

            $folderPath = pathinfo($value[0], PATHINFO_DIRNAME);
            $destionation = $folderPath . '\\';
            $ext = pathinfo($value[0], PATHINFO_EXTENSION);
            $fileName = basename($value[0], '.' . $ext);

            GD::convert($value[0], $destionation . $fileName . '.webp', self::getConvertOptions(), $logger = null);

            if (is_file($value[0])) {
                unlink($value[0]);
            }
        }

        return new Response("test");
    }

    public function getDirContents($source)
    {
        $results = [];
        $files = scandir($source);

        foreach ($files as $key => $value) {
            $path = realpath($source . DIRECTORY_SEPARATOR . $value);
            if (!is_dir($path)) {
                $results[] = $path;
            } else if ($value !== "." && $value !== "..") {
                $this->getDirContents($path, $results);
                foreach (scandir($path) as $file) {
                    if ($file !== "." && $file !== ".." && !in_array(pathinfo($file, PATHINFO_EXTENSION), ['webp', 'svg', 'pdf', 'docx', 'xls'])) {

                        $results[] = [
                            $path . "/" . $file,
                        ];
                    }
                }
            }
        }

        return $results;
    }

    public static function getConvertOptions(): array
    {
        return [
            'png' => [
                'encoding' => 'auto',
                'near-lossless' => 75,
                'quality' => 80,
                'sharp-yuv' => true,
                'imageinterlace' => false,
                'converters' => ['cwebp', 'vips', 'imagick', 'gmagick', 'imagemagick', 'graphicsmagick', 'wpc', 'ewww', 'gd'],
            ],
            'jpeg' => [
                'encoding' => 'auto',
                'near-lossless' => 75,
                'quality' => 80,
                'sharp-yuv' => true,
            ],
            'jfif' => [
                'encoding' => 'auto',
                'near-lossless' => 75,
                'quality' => 80,
                'sharp-yuv' => true,
            ],
        ];
    }
}
Jarzebowsky commented 2 years ago

Have same issue while using one of module using this repository. Any idea how Warning: imagecreatefrompng(): gd-png: libpng warning: Interlace handling should be turned on when using png_read_image should be handled?

inajaf commented 2 years ago

I used Imagick instead of Gd converter. Convertation is working but very slow.

inajaf commented 2 years ago

Have same issue while using one of module using this repository. Any idea how Warning: imagecreatefrompng(): gd-png: libpng warning: Interlace handling should be turned on when using png_read_image should be handled?

this fix the issue with gd on Windows error_reporting(0);

RaFr commented 2 years ago

It seems warnings could be suppressed using @ : $image = @imagecreatefrompng($this->source);