thumbnails, page generation

This commit is contained in:
mappu 2013-09-21 13:52:12 +12:00
parent 19a5dbb07c
commit 8852b2ae8b
3 changed files with 100 additions and 29 deletions

View File

View File

@ -1,20 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h2>##PROJECTNAME##</h2>
<p>##DESCRIPTION##</p>
##IMAGESECTION##
<a href="##IMAGE##"><img src="##THUMBNAIL##"></a>
##ENDIMAGESECTION##
##DOWNLOADSECTION##
<a href="##DOWNLOAD_URL##">##FILENAME##</a>
##ENDDOWNLOADSECTION##
</body>
</html>

View File

@ -4,13 +4,18 @@
// ````````````````````
define('BASEDIR', __DIR__.'\\');
define('SITE_TITLE', 'code.ivysaur.me');
define('PAGE_THUMB_W', 60);
define('PAGE_THUMB_H', 60);
// clean up wwwroot
if (file_exists(BASEDIR.'wwwroot')) {
rename(BASEDIR.'wwwroot', BASEDIR.'wwwroot.old.'.uniqid());
}
mkdir(BASEDIR.'wwwroot');
mkdir(BASEDIR.'wwwroot/srv');
// List of projects
@ -26,11 +31,46 @@ foreach($ls as $dirname) {
}
}
/**
* Create a thumbnail of an image. It overscales and crops to fit the target
* box.
*
* @param string $src_file
* @param string $dest_file
* @param int $width
* @param int $height
* @return boolean
*/
function mkthumbnail($src_file, $dest_file, $width, $height) {
list($src_width, $src_height) = getimagesize($src_file);
$im = imagecreatefromstring(file_get_contents($src_file));
$dest = imagecreatetruecolor($width, $height);
// TODO
$scale = max( $width/$src_width, $height/$src_height ); // overscale + crop
print_r($scale);
$box_w = $width/$scale;
$box_h = $height/$scale;
$box_xoff = floor(($src_width - $box_w)/2);
$box_yoff = floor(($src_height - $box_h)/2);
print_r(array(
$dest, $im,
0, 0,
$box_xoff, $box_yoff,
$width, $height, $box_w, $box_h
));
imagecopyresampled(
$dest, $im,
0, 0,
$box_xoff, $box_yoff,
$width, $height, $box_w, $box_h
);
return imagejpeg($dest, $dest_file);
}
@ -44,6 +84,10 @@ function is_image($sz) {
return in_array(strtolower(str_ext($sz)), ['jpg', 'png', 'jpeg']);
}
function hesc($sz) {
return @htmlentities($sz, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
}
class CProject {
private $dir;
@ -79,27 +123,74 @@ class CProject {
public function write() {
// Build file mapping
// Generate image thumbnails
// Copy data files to wwwroot
foreach($this->images as $idx => $image) {
$outfile = BASEDIR.'wwwroot/srv/'.$this->projname.'_'.$idx;
copy($this->dir.$image, $outfile.'.'.str_ext($image));
mkthumbnail($outfile.'.'.str_ext($image), $outfile.'_thumb.jpg', PAGE_THUMB_W, PAGE_THUMB_H);
}
// Copy downloads to wwwroot
foreach($this->downloads as $idx => $filename) {
copy($this->dir.$filename, BASEDIR.'wwwroot/srv/'.$filename);
}
// Generate index page
ob_start();
$this->index();
$idxfile = ob_get_clean();
file_put_contents(BASEDIR.'wwwroot/'.$this->projname.'.html', $idxfile);
}
public function index() {
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><?=hesc($this->projname)?> | <?=hesc(SITE_TITLE)?></title>
</head>
<body>
<h2><?=hesc($this->projname)?></h2>
<p><?=nl2br(hesc($this->longdesc))?></p>
<?php if (count($this->images)) { ?>
<h3>Images</h3>
<?php foreach($this->images as $idx => $origname) { ?>
<a href="srv/<?=hesc(urlencode($this->projname))?>_<?=$idx?>.<?=str_ext($origname)?>"><img src="srv/<?=hesc(urlencode($this->projname))?>_<?=$idx?>_thumb.jpg"></a>
<?php } ?>
<?php } ?>
<?php if (count($this->downloads)) { ?>
<h3>Files</h3>
<ul>
<?php foreach($this->downloads as $filename) { ?>
<li><a href="srv/<?=hesc(urlencode($filename))?>"><?=hesc($filename)?></a></li>
<?php } ?>
</ul>
<?php } ?>
</body>
</html>
<?php
}
}
// Format project
$pagetpl = @file_get_contents(BASEDIR.'page_template.htm');
if ($pagetpl === false) die('Missing page_template.htm');
// Build all projects
foreach($projects as $dirname => $projectname) {
$pr = new CProject($dirname, $projectname);
print_r($pr);
$pr->write();
}
// Index page
// Build index page