<commit_before><?php

/*
Title:      Thumb.php
URL:        http://github.com/jamiebicknell/Thumb.php
Author:     <NAME>
Twitter:    @jamiebicknell
*/

define('THUMB_CACHE',           './cache/');    // Path to cache directory (must be writeable)
define('THUMB_CACHE_AGE',       86400);         // Duration of cached files in seconds
define('THUMB_BROWSER_CACHE',   true);          // Browser cache true or false
define('SHARPEN_MIN',           12);            // Minimum sharpen value
define('SHARPEN_MAX',           28);            // Maximum sharpen value
define('ADJUST_ORIENTATION',    true);          // Auto adjust orientation for JPEG true or false

$src = isset($_GET['src']) ? $_GET['src'] : false;
$size = isset($_GET['size']) ? str_replace(array('<','x'),'',$_GET['size'])!='' ? $_GET['size'] : 100 : 100;
$crop = isset($_GET['crop']) ? max(0,min(1,$_GET['crop'])) : 1;
$trim = isset($_GET['trim']) ? max(0,min(1,$_GET['trim'])) : 0;
$zoom = isset($_GET['zoom']) ? max(0,min(1,$_GET['zoom'])) : 0;
$align = isset($_GET['align']) ? $_GET['align'] : false;
$sharpen = isset($_GET['sharpen']) ? max(0,min(100,$_GET['sharpen'])) : 0;
$path = parse_url($src);

if(isset($path['scheme'])) {
    $base = parse_url($_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
    if(preg_replace('/^www\./i','',$base['host'])==preg_replace('/^www\./i','',$path['host'])) {
        $base = explode('/',preg_replace('/\/+/','/',$base['path']));
        $path = explode('/',preg_replace('/\/+/','/',$path['path']));
        $temp = $path;
        $part = count($base);
        foreach($base as $k => $v) {
            if($v==$path[$k]) {
                array_shift($temp);
            }
            else {
                if($part-$k>1) {
                    $temp = array_pad($temp,0-(count($temp)+($part-$k)-1),'..');
                    break;
                }
                else {
                    $temp[0] = './' . $temp[0];
                }
            }
        }
        $src = implode('/',$temp);
    }
}

if(!extension_loaded('gd')) {
    die('GD extension is not installed');
}
if(!is_writable(THUMB_CACHE)) {
    die('Cache not writable');
}
if(isset($path['scheme'])||!file_exists($src)) {
    die('File cannot be found');
}
if(!in_array(strtolower(substr(strrchr($src,'.'),1)),array('gif','jpg','jpeg','png'))) {
    die('File is not an image');
}

$file_salt = 'v1.0';
$file_size = filesize($src);
$file_time = filemtime($src);
$file_date = gmdate('D, d M Y H:i:s T',$file_time);
$file_type = strtolower(substr(strrchr($src,'.'),1));
$file_hash = md5($file_salt . ($src.$size.$crop.$trim.$zoom.$align.$sharpen) . $file_time);
$file_name = THUMB_CACHE . $file_hash . '.img.txt';

if(!file_exists(THUMB_CACHE . 'index.html')) {
    touch(THUMB_CACHE . 'index.html');
}
if(time()-THUMB_CACHE_AGE>filemtime(THUMB_CACHE . 'index.html')) {
    $files = glob(THUMB_CACHE . '*.img.txt');
    if(is_array($files)&&count($files)>0) {
        foreach($files as $file) {
            if(time()-THUMB_CACHE_AGE>filemtime($file)) {
                unlink($file);
            }
        }
    }
    touch(THUMB_CACHE . 'index.html');
}

if(THUMB_BROWSER_CACHE&&(isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])||isset($_SERVER['HTTP_IF_NONE_MATCH']))) {
    if($_SERVER['HTTP_IF_MODIFIED_SINCE']==$file_date&&$_SERVER['HTTP_IF_NONE_MATCH']==$file_hash) {
        header($_SERVER['SERVER_PROTOCOL'] . ' 304 Not Modified');
        die();
    }
}

if(!file_exists($file_name)) {
    list($w0,$h0,$type) = getimagesize($src);
    switch($type) {
        case 1:
            $oi = imagecreatefromgif($src);
            break;
        case 2:
            $oi = imagecreatefromjpeg($src);
            break;
        case 3:
            $oi = imagecreatefrompng($src);
            break;
    }
    if(ADJUST_ORIENTATION&&$type==2) {
        // I know supressing errors is bad, but calling exif_read_data on invalid
        // or corrupted data returns a fatal error and there's no way to validate
        // the EXIF data before calling the function.
        $exif = @exif_read_data($src,EXIF);
        if(isset($exif['Orientation'])) {
            $degree = 0;
            $mirror = false;
            switch($exif['Orientation']) {<commit_msg>Fixed race condition<commit_after><?php

/*
Title:      Thumb.php
URL:        http://github.com/jamiebicknell/Thumb.php
Author:     <NAME>
Twitter:    @jamiebicknell
*/

define('THUMB_CACHE',           './cache/');    // Path to cache directory (must be writeable)
define('THUMB_CACHE_AGE',       86400);         // Duration of cached files in seconds
define('THUMB_BROWSER_CACHE',   true);          // Browser cache true or false
define('SHARPEN_MIN',           12);            // Minimum sharpen value
define('SHARPEN_MAX',           28);            // Maximum sharpen value
define('ADJUST_ORIENTATION',    true);          // Auto adjust orientation for JPEG true or false

$src = isset($_GET['src']) ? $_GET['src'] : false;
$size = isset($_GET['size']) ? str_replace(array('<','x'),'',$_GET['size'])!='' ? $_GET['size'] : 100 : 100;
$crop = isset($_GET['crop']) ? max(0,min(1,$_GET['crop'])) : 1;
$trim = isset($_GET['trim']) ? max(0,min(1,$_GET['trim'])) : 0;
$zoom = isset($_GET['zoom']) ? max(0,min(1,$_GET['zoom'])) : 0;
$align = isset($_GET['align']) ? $_GET['align'] : false;
$sharpen = isset($_GET['sharpen']) ? max(0,min(100,$_GET['sharpen'])) : 0;
$path = parse_url($src);

if(isset($path['scheme'])) {
    $base = parse_url($_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
    if(preg_replace('/^www\./i','',$base['host'])==preg_replace('/^www\./i','',$path['host'])) {
        $base = explode('/',preg_replace('/\/+/','/',$base['path']));
        $path = explode('/',preg_replace('/\/+/','/',$path['path']));
        $temp = $path;
        $part = count($base);
        foreach($base as $k => $v) {
            if($v==$path[$k]) {
                array_shift($temp);
            }
            else {
                if($part-$k>1) {
                    $temp = array_pad($temp,0-(count($temp)+($part-$k)-1),'..');
                    break;
                }
                else {
                    $temp[0] = './' . $temp[0];
                }
            }
        }
        $src = implode('/',$temp);
    }
}

if(!extension_loaded('gd')) {
    die('GD extension is not installed');
}
if(!is_writable(THUMB_CACHE)) {
    die('Cache not writable');
}
if(isset($path['scheme'])||!file_exists($src)) {
    die('File cannot be found');
}
if(!in_array(strtolower(substr(strrchr($src,'.'),1)),array('gif','jpg','jpeg','png'))) {
    die('File is not an image');
}

$file_salt = 'v1.0';
$file_size = filesize($src);
$file_time = filemtime($src);
$file_date = gmdate('D, d M Y H:i:s T',$file_time);
$file_type = strtolower(substr(strrchr($src,'.'),1));
$file_hash = md5($file_salt . ($src.$size.$crop.$trim.$zoom.$align.$sharpen) . $file_time);
$file_name = THUMB_CACHE . $file_hash . '.img.txt';

if(!file_exists(THUMB_CACHE . 'index.html')) {
    touch(THUMB_CACHE . 'index.html');
}
$fp = fopen(THUMB_CACHE . 'index.html','r');
if(flock($fp,LOCK_EX)) {
    if(time()-THUMB_CACHE_AGE>filemtime(THUMB_CACHE . 'index.html')) {
        $files = glob(THUMB_CACHE . '*.img.txt');
        if(is_array($files)&&count($files)>0) {
            foreach($files as $file) {
                if(time()-THUMB_CACHE_AGE>filemtime($file)) {
                    unlink($file);
                }
            }
        }
        touch(THUMB_CACHE . 'index.html');
    }
    flock($fp,LOCK_UN);
}
fclose($fp);

if(THUMB_BROWSER_CACHE&&(isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])||isset($_SERVER['HTTP_IF_NONE_MATCH']))) {
    if($_SERVER['HTTP_IF_MODIFIED_SINCE']==$file_date&&$_SERVER['HTTP_IF_NONE_MATCH']==$file_hash) {
        header($_SERVER['SERVER_PROTOCOL'] . ' 304 Not Modified');
        die();
    }
}

if(!file_exists($file_name)) {
    list($w0,$h0,$type) = getimagesize($src);
    switch($type) {
        case 1:
            $oi = imagecreatefromgif($src);
            break;
        case 2:
            $oi = imagecreatefromjpeg($src);
            break;
        case 3:
            $oi = imagecreatefrompng($src);
            break;
    }
    if(ADJUST_ORIENTATION&&$type==2) {
        // I know supressing errors is bad, but calling exif_read_data on invalid
        // or corrupted data returns a fatal error and there's no way to validate
        // the EXIF data before calling the function.
        $exif = @exif_read_data($src,EXIF);
        if(isset($exif['Orientation'])) {
            $degree = 0;
            $mirror = false;
            switch($exif['Orientation']) {
{'fraction_non_alphanumeric': 0.20759758364312267, 'fraction_numerical': 0.011617100371747211, 'mean_word_length': 2.725227174383384, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '439272', 'n_tokens_mistral': 3077, 'n_tokens_neox': 2976, 'n_words': 490}