1
0

smaller prog previews + self test

This commit is contained in:
2018-01-27 14:21:34 +01:00
parent 88e04c4a87
commit a84a475008
58 changed files with 521 additions and 54 deletions

View File

@@ -181,4 +181,111 @@ function isLoggedInByCookie()
function clearLoginCookie()
{
setcookie("mikescher_auth", "", time()+30);
}
/**
* easy image resize function
* @author http://www.nimrodstech.com/php-image-resize/
* @param $file - file name to resize
* @param $string - The image data, as a string
* @param $width - new image width
* @param $height - new image height
* @param $proportional - keep image proportional, default is no
* @param $output - name of the new file (include path if needed)
* @param $quality - enter 1-100 (100 is best quality) default is 100
* @return boolean|resource
*/
function smart_resize_image($file, $string = null, $width = 0, $height = 0, $proportional = false, $output = 'file', $quality = 100
) {
if ( $height <= 0 && $width <= 0 ) return false;
if ( $file === null && $string === null ) return false;
# Setting defaults and meta
$info = $file !== null ? getimagesize($file) : getimagesizefromstring($string);
$image = '';
$final_width = 0;
$final_height = 0;
list($width_old, $height_old) = $info;
$cropHeight = $cropWidth = 0;
# Calculating proportionality
if ($proportional) {
if ($width == 0) $factor = $height/$height_old;
elseif ($height == 0) $factor = $width/$width_old;
else $factor = min( $width / $width_old, $height / $height_old );
$final_width = round( $width_old * $factor );
$final_height = round( $height_old * $factor );
}
else {
$final_width = ( $width <= 0 ) ? $width_old : $width;
$final_height = ( $height <= 0 ) ? $height_old : $height;
$widthX = $width_old / $width;
$heightX = $height_old / $height;
$x = min($widthX, $heightX);
$cropWidth = ($width_old - $width * $x) / 2;
$cropHeight = ($height_old - $height * $x) / 2;
}
# Loading image to memory according to type
switch ( $info[2] ) {
case IMAGETYPE_JPEG: $file !== null ? $image = imagecreatefromjpeg($file) : $image = imagecreatefromstring($string); break;
case IMAGETYPE_GIF: $file !== null ? $image = imagecreatefromgif($file) : $image = imagecreatefromstring($string); break;
case IMAGETYPE_PNG: $file !== null ? $image = imagecreatefrompng($file) : $image = imagecreatefromstring($string); break;
default: return false;
}
# This is the resizing/resampling/transparency-preserving magic
$image_resized = imagecreatetruecolor( $final_width, $final_height );
if ( ($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG) ) {
$transparency = imagecolortransparent($image);
$palletsize = imagecolorstotal($image);
if ($transparency >= 0 && $transparency < $palletsize) {
$transparent_color = imagecolorsforindex($image, $transparency);
$transparency = imagecolorallocate($image_resized, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
imagefill($image_resized, 0, 0, $transparency);
imagecolortransparent($image_resized, $transparency);
}
elseif ($info[2] == IMAGETYPE_PNG) {
imagealphablending($image_resized, false);
$color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127);
imagefill($image_resized, 0, 0, $color);
imagesavealpha($image_resized, true);
}
}
imagecopyresampled($image_resized, $image, 0, 0, $cropWidth, $cropHeight, $final_width, $final_height, $width_old - 2 * $cropWidth, $height_old - 2 * $cropHeight);
# Preparing a method of providing result
switch ( strtolower($output) ) {
case 'browser':
$mime = image_type_to_mime_type($info[2]);
header("Content-type: $mime");
$output = NULL;
break;
case 'file':
$output = $file;
break;
case 'return':
return $image_resized;
break;
default:
break;
}
# Writing image according to type to the output destination and image quality
switch ( $info[2] ) {
case IMAGETYPE_GIF: imagegif($image_resized, $output); break;
case IMAGETYPE_JPEG: imagejpeg($image_resized, $output, $quality); break;
case IMAGETYPE_PNG:
$quality = 9 - (int)((0.9*$quality)/10.0);
imagepng($image_resized, $output, $quality);
break;
default: return false;
}
return true;
}

View File

@@ -18,6 +18,8 @@ class Blog
$d['canonical'] = "https://www.mikescher.com" . $d['url'];
$d['file_fragment'] = __DIR__ . '/../statics/blog/' . $d['fragment'];
return $d;
}
@@ -38,7 +40,40 @@ class Blog
public static function getPostFragment($post)
{
return file_get_contents( __DIR__ . '/../statics/blog/' . $post['fragment']);
return file_get_contents($post['file_fragment']);
}
public static function checkConsistency()
{
$keys = [];
foreach (self::listAll() as $post)
{
if (in_array($post['id'], $keys)) return ['result'=>'err', 'message' => 'Duplicate key ' . $post['id']];
$keys []= $post['id'];
if ($post['cat'] !== 'log' && $post['cat'] !== 'blog') return ['result'=>'err', 'message' => 'Unknown cat ' . $post['cat']];
if ($post['type'] === 'markdown') {
if (!file_exists($post['file_fragment'])) return ['result'=>'err', 'message' => 'Fragment not found ' . $post['fragment']];
} else if ($post['type'] === 'plain') {
if (!file_exists($post['file_fragment'])) return ['result'=>'err', 'message' => 'Fragment not found ' . $post['fragment']];
} else if ($post['type'] === 'euler') {
// aok
} else {
return ['result'=>'err', 'message' => 'Unknown type ' . $post['type']];
}
}
return ['result'=>'ok', 'message' => ''];
}
}

View File

@@ -67,5 +67,29 @@ class Euler
return 4;
}
public static function checkConsistency()
{
$warn = null;
$numbers = [];
$realname = [];
foreach (self::listAll() as $ep)
{
if (in_array($ep['number'], $numbers)) return ['result'=>'err', 'message' => 'Duplicate number ' . $ep['number']];
$numbers []= $ep['number'];
if (in_array($ep['title'], $realname)) return ['result'=>'err', 'message' => 'Duplicate title ' . $ep['title']];
$realname []= $ep['title'];
if (!file_exists($ep['file_description'])) return ['result'=>'err', 'message' => 'file_description not found ' . $ep['file_description']];
if (!file_exists($ep['file_code'])) return ['result'=>'err', 'message' => 'file_code not found ' . $ep['file_code']];
if (!file_exists($ep['file_explanation'])) return ['result'=>'err', 'message' => 'file_explanation not found ' . $ep['file_explanation']];
}
if ($warn != null) return $warn;
return ['result'=>'ok', 'message' => ''];
}
}

View File

@@ -26,4 +26,26 @@ class MikescherGitGraph
return $v;
}
public static function getPathRenderedData()
{
return __DIR__ . '/../dynamic/egh.html';
}
public static function includeRender()
{
if (file_exists(__DIR__ . '/../dynamic/egh.html'))
include __DIR__ . '/../dynamic/egh.html';
}
public static function checkConsistency()
{
$p = self::getPathRenderedData();
if (!file($p)) return ['result'=>'err', 'message' => 'Rendered data not found'];
if (filemtime($p) < time()-(3*7*24*60*60)) return ['result'=>'warn', 'message' => 'Rendered data is older than 3 weeks'];
return ['result'=>'ok', 'message' => ''];
}
}

View File

@@ -1,7 +1,11 @@
<?php if(count(get_included_files()) ==1) exit("Direct access not permitted.");
require_once __DIR__ . '/base.php';
class Programs
{
const PROG_LANGS = [ 'Java', 'C#', 'Delphi', 'PHP', 'C++' ];
const URL_ORDER =
[
'github',
@@ -29,8 +33,14 @@ class Programs
public static function readSingle($a)
{
$a['thumbnail_url'] = '/data/images/program_thumbnails/' . $a['internal_name'] . '.png';
$a['mainimage_url'] = '/data/images/program_img/' . $a['internal_name'] . '.png';
$a['mainimage_path'] = __DIR__ . '/../data/images/program_img/' . $a['internal_name'] . '.png';
$a['preview_url'] = '/data/dynamic/progprev_' . $a['internal_name'] . '.png';
$a['preview_path'] = __DIR__ . '/../data/dynamic/progprev_' . $a['internal_name'] . '.png';
$a['file_longdescription'] = (__DIR__ . '/../statics/programs/' . $a['internal_name'] . '_description.md');
$a['url'] = '/programs/view/' . $a['internal_name'];
return $a;
@@ -73,6 +83,12 @@ class Programs
public static function urlComparator($a, $b)
{
$na = 0;
$nb = 0;
if (strpos($a, '#') !== FALSE) { $na = intval(explode('#', $a)[1]); $a=explode('#', $a)[0]; }
if (strpos($b, '#') !== FALSE) { $nb = intval(explode('#', $b)[1]); $b=explode('#', $b)[0]; }
$ia = array_search($a, Programs::URL_ORDER);
$ib = array_search($b, Programs::URL_ORDER);
@@ -80,18 +96,153 @@ class Programs
if ($ia === false && $ib !== false) return +1; // sort [IA | IB]
if ($ia !== false && $ib === false) return -1; // sort [IB | IA]
if ($ia === $ib) return ($na < $nb) ? -1 : +1;
return ($ia < $ib) ? -1 : +1;
}
public static function sortUrls($urls)
public static function getURLs($prog)
{
$urls = $prog['urls'];
uksort($urls, 'self::urlComparator');
return $urls;
$result = [];
foreach ($urls as $fulltype => $urldata)
{
$type = $fulltype;
if (strpos($fulltype, '#') !== FALSE) $type=explode('#', $fulltype)[0];
$caption = '?';
$css = '?';
$svg = '?';
$direct = false;
if ($type === 'download') { $caption = 'Download'; $css = 'prgv_dl_download'; $svg = 'download'; }
if ($type === 'github') { $caption = 'Github'; $css = 'prgv_dl_github'; $svg = 'github'; }
if ($type === 'homepage') { $caption = 'Homepage'; $css = 'prgv_dl_homepage'; $svg = 'home'; }
if ($type === 'wiki') { $caption = 'Wiki'; $css = 'prgv_dl_wiki'; $svg = 'wiki'; }
if ($type === 'playstore') { $caption = 'Google Playstore'; $css = 'prgv_dl_playstore'; $svg = 'playstore'; }
if ($type === 'amazonappstore') { $caption = 'Amazon Appstore'; $css = 'prgv_dl_amznstore'; $svg = 'amazon'; }
if ($type === 'windowsstore') { $caption = 'Microsoft Store'; $css = 'prgv_dl_winstore'; $svg = 'windows'; }
if ($type === 'itunesstore') { $caption = 'App Store'; $css = 'prgv_dl_appstore'; $svg = 'apple'; }
if ($type === 'sourceforge') { $caption = 'Sourceforge'; $css = 'prgv_dl_sourceforge'; $svg = 'sourceforge'; }
if ($type === 'alternativeto') { $caption = 'AlternativeTo'; $css = 'prgv_dl_alternativeto'; $svg = 'alternativeto'; }
if (is_array($urldata))
{
$url = $urldata['url'];
if (isset($urldata['caption'])) $caption = $urldata['caption'];
if (isset($urldata['css'])) $css = $urldata['css'];
if (isset($urldata['svg'])) $svg = $urldata['svg'];
}
else
{
$url = $urldata;
}
if ($url === 'direct')
{
$direct = true;
$url = Programs::getDirectDownloadURL($prog);
}
$result []=
[
'type' => $type,
'caption' => $caption,
'svg' => $svg,
'href' => $url,
'css' => $css,
'isdirect' => $direct,
];
}
return $result;
}
public static function getLicenseUrl($license)
{
return self::LICENSES[$license];
}
public static function getDirectDownloadURL($prog)
{
return '/data/binaries/'.$prog['internal_name'].'.zip';
}
public static function getDirectDownloadPath($prog)
{
return (__DIR__ . '/../data/binaries/'.$prog['internal_name'].'.zip');
}
public static function checkConsistency()
{
$warn = null;
$intname = [];
$realname = [];
foreach (self::listAll() as $prog)
{
if (in_array($prog['internal_name'], $intname)) return ['result'=>'err', 'message' => 'Duplicate internal_name ' . $prog['name']];
$intname []= $prog['internal_name'];
if ($prog['internal_name_alt'] !== null)
{
if (in_array($prog['internal_name_alt'], $intname)) return ['result'=>'err', 'message' => 'Duplicate internal_name ' . $prog['name']];
$intname []= $prog['internal_name_alt'];
}
if (in_array($prog['name'], $realname)) return ['result'=>'err', 'message' => 'Duplicate name ' . $prog['name']];
$realname []= $prog['name'];
if (strpos($prog['internal_name'], ' ') !== FALSE) return ['result'=>'err', 'message' => 'Internal name contains spaces ' . $prog['name']];
foreach (explode('|', $prog['ui_language']) as $lang) if (convertLanguageToFlag($lang) === null) return ['result'=>'err', 'message' => 'Unknown ui-lang ' . $prog['name']];;
if (!in_array($prog['prog_language'], self::PROG_LANGS)) return ['result'=>'err', 'message' => 'Unknown prog-lang ' . $prog['name']];
if (strlen($prog['short_description'])> 128) $warn = ['result'=>'warn', 'message' => 'short_description too long ' . $prog['name']];
if ($prog['license'] !== null && !array_key_exists($prog['license'], self::LICENSES)) return ['result'=>'err', 'message' => 'Unknown license ' . $prog['name']];
$isdl = false;
foreach (self::getURLs($prog) as $xurl)
{
if (!in_array($xurl['type'], self::URL_ORDER)) return ['result'=>'err', 'message' => 'Unknown url ' . $xurl['type']];
if ($xurl['type']==='download' && $xurl['isdirect'] && !file_exists(self::getDirectDownloadPath($prog))) return ['result'=>'err', 'message' => 'Direct download not found ' . $prog['name']];
if ($xurl['type']==='download' || $xurl['type']==='playstore' || $xurl['type']==='itunesstore') $isdl = true;
}
if (!$isdl) return ['result'=>'err', 'message' => 'No download link ' . $prog['name']];
if (!file_exists($prog['mainimage_path'])) return ['result'=>'err', 'message' => 'Image not found ' . $prog['name']];
if (!file_exists($prog['file_longdescription'])) return ['result'=>'err', 'message' => 'Description not found ' . $prog['name']];
}
if ($warn != null) return $warn;
return ['result'=>'ok', 'message' => ''];
}
public static function checkThumbnails()
{
foreach (self::listAll() as $prog)
{
if (!file_exists($prog['preview_path'])) return ['result'=>'err', 'message' => 'Preview not found ' . $prog['name']];
}
return ['result'=>'ok', 'message' => ''];
}
public static function createPreview($prog)
{
$src = $prog['mainimage_path'];
$dst = $prog['preview_path'];
smart_resize_image($src , null, 250, 0, true, $dst, 100);
}
}