<?php

namespace App\Utils;

use Imagick;

class ConvertIco {

    static function convert($demande, $size) {

        rename($demande->requestImg, $demande->requestImg . '.ico');
        $icoFile = $demande->requestImg . '.ico';

        if (empty(Imagick::queryFormats("ICO"))) {
            throw new \Exception('Unsupported format');
        }

        $im = new Imagick($icoFile);
        $targetWidth = $targetHeight = $size;

        if ($im->getImageWidth() <> $targetWidth || $im->getImageHeight() <> $targetHeight) {
            $im->cropThumbnailImage($targetWidth, $targetHeight);
        }

        $im->writeImage($demande->requestImg);
        unlink($icoFile);
    }
}
