<commit_before><?php
/**
 * @author <NAME> <<EMAIL>>
 */

class Spritzer_Sprite implements Spritzer_Sprite_Interface
{
    private $_imageResource = null;
    
    protected $tileWidth = null;
    protected $tileHeight = null;
    protected $tableWidth = null;
    protected $tableHeight = null;


    function __construct()
    {
    }


    function setTileWidth($w)
    {
        $this->tileWidth = $w;
    }


    function setTileHeight($h)
    {
        $this->tileHeight = $h;
    }


    function setTableWidth($w)
    {
        $this->tableWidth = $w;
    }


    function setTableHeight($h)
    {
        $this->tableHeight = $h;
    }


    function setTile($x, $y, $value)
    {
        if (!$this->_imageResource) {
            $this->_createImg();

        }
        $this->pasteAt($this->tileWidth * $x, $this->tileHeight * $y, $value);
    }


    private function _createImg()
    {
        $this->_imageResource = imagecreatetruecolor($this->tableWidth * $this->tileWidth, $this->tableHeight * $this->tileHeight);
        imagealphablending($this->_imageResource, false);
        imagefilledrectangle($this->_imageResource, 0, 0, imagesx($this->_imageResource), imagesy($this->_imageResource), imagecolorallocatealpha($this->_imageResource, 255, 255, 255, 127));
        imagealphablending($this->_imageResource, true);
        imagesavealpha($this->_imageResource, true);
    }


    function pasteAt($x, $y, $fileName)
    {
        $info = @getImageSize($fileName);
        if (!$info) {
            throw new InvalidArgumentException("Can not read file $fileName");
        }
        switch ($info[2]) {
            case IMAGETYPE_JPEG:
                $callback = 'imagecreatefromjpeg';
                break;
            case IMAGETYPE_PNG:
                $callback = 'imagecreatefrompng';
                break;
            case IMAGETYPE_GIF:
                $callback = 'imagecreatefromgif';
                break;
            default:
                throw new InvalidArgumentException("Imagetype of $fileName not supported");
        }

        $im = $callback($fileName);
        imagecopy($this->_imageResource, $im, $x, $y, 0, 0, imagesx($im), imagesy($im));
        imagedestroy($im);
    }

<commit_msg>adaptive resize if source image is larger than available space<commit_after><?php
/**
 * @author <NAME> <<EMAIL>>
 */

class Spritzer_Sprite implements Spritzer_Sprite_Interface
{
    private $_imageResource = null;
    
    protected $tileWidth = null;
    protected $tileHeight = null;
    protected $tableWidth = null;
    protected $tableHeight = null;


    function __construct()
    {
    }


    function setTileWidth($w)
    {
        $this->tileWidth = $w;
    }


    function setTileHeight($h)
    {
        $this->tileHeight = $h;
    }


    function setTableWidth($w)
    {
        $this->tableWidth = $w;
    }


    function setTableHeight($h)
    {
        $this->tableHeight = $h;
    }


    function setTile($x, $y, $value)
    {
        if (!$this->_imageResource) {
            $this->_createImage($this->tableWidth * $this->tileWidth, $this->tableHeight * $this->tileHeight);
        }
        $this->pasteAt($this->tileWidth * $x, $this->tileHeight * $y, $value);
    }


    private function _createImage($width, $height)
    {
        $this->_imageResource = imagecreatetruecolor($width, $height);

        imagealphablending($this->_imageResource, false);
        imagefilledrectangle($this->_imageResource, 0, 0, imagesx($this->_imageResource), imagesy($this->_imageResource), imagecolorallocatealpha($this->_imageResource, 255, 255, 255, 127));
        imagealphablending($this->_imageResource, true);
        imagesavealpha($this->_imageResource, true);
    }


    private function _expand($newWidth, $newHeight) {
        $newWidth = max(imagesx($this->_imageResource), $newWidth);
        $newHeight = max(imagesy($this->_imageResource), $newHeight);

        $oldImg = $this->_imageResource;
        $this->_createImage($newWidth, $newHeight);
        imagecopy($this->_imageResource, $oldImg, 0, 0, 0, 0, imagesx($oldImg), imagesy($oldImg));
        imagedestroy($oldImg);
    }


    function pasteAt($x, $y, $fileName)
    {
        $info = @getImageSize($fileName);
        if (!$info) {
            throw new InvalidArgumentException("Can not read file $fileName");
        }
        switch ($info[2]) {
            case IMAGETYPE_JPEG:
                $callback = 'imagecreatefromjpeg';
                break;
            case IMAGETYPE_PNG:
                $callback = 'imagecreatefrompng';
                break;
            case IMAGETYPE_GIF:
                $callback = 'imagecreatefromgif';
                break;
            default:
                throw new InvalidArgumentException("Imagetype of $fileName not supported");
        }

        $im = $callback($fileName);
        
        if(
            $x + imagesx($im) > imagesx($this->_imageResource)
        ||  $y + imagesy($im) > imagesy($this->_imageResource)
        ) {
            $this->_expand($x + imagesx($im), $y + imagesy($im));
        }
        imagecopy($this->_imageResource, $im, $x, $y, 0, 0, imagesx($im), imagesy($im));
        imagedestroy($im);
    }


{'fraction_non_alphanumeric': 0.1370782208588957, 'fraction_numerical': 0.0072852760736196315, 'mean_word_length': 2.3399487836107555, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '735727', 'n_tokens_mistral': 1596, 'n_tokens_neox': 1515, 'n_words': 301}