Php example pngquant not working

I am trying to use pngquant compression algorithm to compress PNG images on the fly using WAMP. They provide an example of PHP , which (I think) should use the binary command line for Windows , which I put in the system32 , and I can access from anywhere on the command line.

I gave their example and traced the problem to the line $compressed_png_content = shell_exec("pngquant --quality=$min_quality-$max_quality - < ".escapeshellarg( $path_to_png_file)); . I simplified it to var_dump(shell_exec('pngquant - < test.png')); but it only prints the first 5 characters, even if passthru('pngquant - < test.png'); seems to send the correct output to the user as a string. exec('pngquant - < test.png',$output); var_dump($output); also seems to capture the correct output, but in the form of an array, which I really don't know how to convert back to an image file. I want to capture the output in a variable, so I can use additional compression and manipulation algorithms and send it to the user as a downloadable file.

I read the differences between system () vs exec () vs shell_exec () vs passthru () vs proc_open () vs popen () . Shell_exec () seems like the right choice, however, php.net says shell_exec () prints a string. Maybe this is a problem? How to correctly write the output of pngquant - < test.png to a variable?

+5
source share
2 answers

Use the PHP shell ( php-pngquant ) instead of PNGQuant, I ran into the same problem, the official wrapper finally saved me.

 function compress_image($source_path, $destination_path, $quality){ $instance = new PNGQuant(); // Change the path to the binary of pngquant, for example in windows would be (with an example path): $instance->setBinaryPath("E:\\wamp64\\www\\testing\\pngquant\\pngquant.exe") // Other options of PNGQuant here ->execute(); // Set the path to the image to compress $result = $instance->setImage($source_path) // Overwrite output file if exists, otherwise pngquant will generate output ... ->overwriteExistingFile() // As the quality in pngquant isn't fixed (it uses a range) // set the minimum quality to 60 ->setQuality(60, $quality) // Retrieve RAW data from pngquant ->getRawOutput(); $exit_code = $result["statusCode"]; // if exit code is equal to 0 then everything went right ! if($exit_code == 0){ $rawImage = imagecreatefromstring($result["imageData"]); // Example Save the PNG Image from the raw data into a file or do whatever you want. imagepng($rawImage , $destination_path); echo "Image succesfully compressed, do something with the raw Data"; }else{ echo "Something went wrong (status code $exit_code) with description: ". $instance->getErrorTable()[(string) $exit_code]; } } 
+2
source

right text: "pngquant --quality = $ min_quality- $ max_quality" .escapeshellarg ($ path_to_png_file)

0
source

Source: https://habr.com/ru/post/1246968/


All Articles