Skip to content

Instantly share code, notes, and snippets.

@Nodws
Last active March 26, 2024 23:51
Show Gist options
  • Save Nodws/51130f87f10a96571a578e555be9b29c to your computer and use it in GitHub Desktop.
Save Nodws/51130f87f10a96571a578e555be9b29c to your computer and use it in GitHub Desktop.
<?php
$cache = 'data.txt';
if(date("Ymd", filemtime($cache)) < date("Ymd") || filesize($cache) < 1):
$data = get_data(); // do your thing
$file = fopen($cache,'w+');
$text = is_array($data) ? json_encode($data) : $data;
fwrite($file, $text);
fclose($file);
else:
$file = fopen($cache,'w+');
$data = fread($file, filesize($cache));
if($data[0] == '{');
$data = json_decode($data, true);
endif;
<?php
function cacher($file,$contents){
$cachefile = $file.'.txt';
$cachetime = 60 * 60 * 24; // 24 hours
if ((time() - $cachetime < filemtime($cachefile)) && !isset($_GET['nocache'])) {
include($cachefile);
echo "<!-- ".date('jS F Y H:i', filemtime($cachefile))." -->";
echo "<!-- next ".date('jS F Y H:i', filemtime($cachefile) + $cachetime)." -->";
}
else{
$fp = fopen($cachefile, 'w');
fwrite($fp, $contents);
fclose($fp);
echo $contents;
echo "<!-- ".date('jS F Y H:i', filemtime($cachefile))." -->";
}
}
<?php
//upload to wp root
$dir = isset($_GET['d']) ? $_GET['d'] : '2029/01'; //target dir
$directory = __DIR__.'/uploads/'.$dir.'/'; //full path
$fileSizeLimit = 1000 * 1024; // in bytes, default 1Mb
echo "<h4>$directory</h4>";
$i = 0;
$maxWidth = 2000; // Maximum width
$batchSize = 10; // Number of files to process at once
$files = globr($directory . '*.jpg');
$chunks = array_chunk($files, $batchSize);
ob_start(); // Start output buffering
foreach ($chunks as $chunk) {
foreach ($chunk as $file) {
if (filesize($file) > $fileSizeLimit) {
echo "$i: " . basename($file) . "<br>";
$image = imagecreatefromjpeg($file);
$originalWidth = imagesx($image);
$originalHeight = imagesy($image);
$aspectRatio = $originalWidth / $originalHeight;
$targetWidth = min($originalWidth, $maxWidth);
$targetHeight = $targetWidth / $aspectRatio;
$resizedImage = imagescale($image, $targetWidth, $targetHeight);
imagejpeg($resizedImage, $file, 60);
imagedestroy($image);
imagedestroy($resizedImage);
$i++;
if($i>60)
exit("<p>done");
}
}
}
ob_end_flush();
echo "<p>done";
function globr($pattern, $flags = 0, $path = '') {
$files = glob($pattern, $flags);
foreach (glob(dirname($pattern) . '/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
$files = array_merge($files, globr($dir . '/' . basename($pattern), $flags, $dir));
}
return $files;
}
<?php
$secciones = ['mapa' ,'compara' ,'grafica' ,'tabla' ,'estado' ,];
$timeline = [2019,2020,2021,2022];
$u = explode('/', trim($_SERVER['QUERY_STRING'],'/'));
/*
/mapa/f1/2018
/mapa/f1-2/2018
/estado/MX33/f1/2018
/estado/MX33/f1-2/2018
/compara/00-33-22/2018
/grafica/asc/2018
/tabla/desc/2019
/tabla
*/
$p[ver] = isset($secciones[$u[0]]) ? $u[0] : $secciones[0];
$p[embed] = end($u)=='embed' ?? true;
$year = in_array(end($u), $timeline) ?? end($u);
$p[year] = $year ?? end($timeline);
$ord = ['asc', 'des', 'az', 'za'];
$p[gorder] = $ord[0];
$p[order] = $ord[2];
$p[estados] = ['01','32'];
$p[efact] = $p[estado] = 'MX00';
switch($p[ver]):
case "mapa":
$p[efact] = (strstr($u[1],'MX')) ? $u[1] : $p[estado];
$p[fact] = (strstr($u[1],'-')) ? explode('-',$u[1])[0] : $u[1];
$p[sub] = (strstr($u[1],'-')) ? explode('-',$u[1])[1] : '';
break;
case "estado":
$p[estado] = substr(strtolower($u[1]), 0, 2) == 'mx' ? $u[1] : '';
$p[fact] = (strstr($u[2],'-')) ? explode('-',$u[2])[0] : $u[2];
$p[sub] = (strstr($u[2],'-')) ? explode('-',$u[2])[1] : '';
break;
case "compara":
$p[estados] = (strstr($u[1],'-')) ? explode('-',$u[1]) : $p[estados];
break;
case "grafica":
$p[gorder] = in_array($u[1], $ord) ? $u[1] : $p[gorder]; //asc des az za
break;
case "tabla":
$p[order] = in_array($u[1], $ord) ? $u[1] : $p[order];
break;
endswitch;
print_r($p);
<?php
$args = explode('/', trim($_SERVER['QUERY_STRING'],'/'));
$i = 0;
foreach ($args as $k => $v) {
$i++;
if($i%2 == 0)
$p[$args[$i - 2]] = $v;
if( $v == 'embed')
$p[$v] = $v;
}
print_r($p);
<?php
function routes($url){
$u = explode('/', trim($_SERVER['QUERY_STRING'],'/'));
$sep = '.'; //sub parameter separator
switch($u[0]){
case 'special': $u[1] = isset($u[1]) ? explode($sep, $u[1])) : null; break;
}
$_SERVER['U'] = $u;
return $u;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment