PHP code example of mkcg / qoi-php

1. Go to this page and download the library: Download mkcg/qoi-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mkcg / qoi-php example snippets


use MKCG\Image\QOI\Codec;
use MKCG\Image\QOI\Driver\Dynamic;
use MKCG\Image\QOI\Writer\StreamWriter;

$inputFilepath  = "/foobar.png";
$outputFilepath = "/foobar.qoi";

$context = Dynamic::loadFromFile($inputFilepath);

if ($context) {
    $outputFile = fopen($outputFilepath, 'w');

    if ($outputFile) {
        $writer = new StreamWriter($outputFile);
        Codec::encode($context->iterator, $context->descriptor, $writer);
        fclose($outputFile);
    }
}

use MKCG\Image\QOI\Codec;
use MKCG\Image\QOI\Driver\Dynamic;
use MKCG\Image\QOI\Writer\InMemoryWriterFactory;

$inputFilepath  = "/foobar.png";
$outputFilepath = "/foobar.qoi";

$context = Dynamic::loadFromFile($inputFilepath);

if ($context) {
    $outputFile = fopen($outputFilepath, 'w');

    if ($outputFile) {
        $writer = (new Writer\InMemoryWriterFactory)->createWriter($context->descriptor);
        Codec::encode($context->iterator, $context->descriptor, $writer);
        fwrite($outputFile, (string) $writer, $writer->countWritten());
        fclose($outputFile);
    }
}

use MKCG\Image\QOI\Codec;
use MKCG\Image\QOI\Colorspace;
use MKCG\Image\QOI\Context;
use MKCG\Image\QOI\ImageDescriptor;
use MKCG\Image\QOI\Writer\StreamWriter;

$outputFilepath = "/white.qoi";

$width    = 800;
$height   = 600;
$channels = 3;

$context = new Context(
    new ImageDescriptor($width, $height, $channels, Colorspace::SRGB),
    (function() use ($width, $height, $channels) {
        $size = $width * $height * $channels;
        for ($i = 0; $i < $size; $i++) {
            yield [ 255, 255, 255, 255 ];
        }
    })(),
);

$outputFile = fopen($outputFilepath, 'w');

if ($outputFile) {
    $writer = new StreamWriter($outputFile);
    Codec::encode($context->iterator, $context->descriptor, $writer);
    fclose($outputFile);
}


use MKCG\Image\QOI\Codec;

function createFileIterator($filepath): \Generator
{
    $bytes = file_get_contents($filepath);

    for ($i = 0; $i < strlen($bytes); $i++) {
        yield $bytes[$i];
    }
};

$filepath = "/input.qoi";

$reader = Codec::decode(createFileIterator($filepath));

$width    = $reader->descriptor->width;
$height   = $reader->descriptor->height;
$channels = $reader->descriptor->channels;

$pixels = iterator_to_array($reader->iterator);

use MKCG\Image\QOI\Codec;
use MKCG\Image\QOI\Format;
use MKCG\Image\QOI\Driver\Dynamic;

function createFileIterator($filepath): \Generator
{
    $handler = fopen($filepath, 'r');

    while (($bytes = fread($handler, 8192)) !== false) {
        for ($i = 0; $i < strlen($bytes); $i++) {
            yield $bytes[$i];
        }
    }

    fclose($handler);
};

$filepath = "/input.qoi";

$reader = Codec::decode(createFileIterator($filepath));
Dynamic::convertInto($reader, "/output.png", Format::PNG);

// Important: you need to create another reader
$reader = Codec::decode(createFileIterator($filepath));
Dynamic::convertInto($reader, "/output.jpg", Format::JPG);