Skip to content

Commit

Permalink
added support for dynamic content controllers
Browse files Browse the repository at this point in the history
Also implemented a placeholder generator to showcase it
  • Loading branch information
geek-at committed Aug 26, 2023
1 parent c90a716 commit 19d7cb0
Show file tree
Hide file tree
Showing 10 changed files with 346 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Then open http://localhost:8080 in your browser

## New Features in v2

- Generate placeholder images by specifying the size in the URL. [example](https://pictshare.net/placeholder/555x250/color-white-blue)
- Added support for external storage
- [Encryption of files in external storage](/rtfm/ENCRYPTION.md)
- Added text hosting (like pastebin)
Expand Down Expand Up @@ -105,13 +106,13 @@ Read [here](/rtfm/CONFIG.md) what those options do
- [x] MASTER_DELETE_CODE
- [x] MASTER_DELETE_IP
- [x] UPLOAD_FORM_LOCATION
- [x] S3 Backend
- [ ] UPLOAD_QUOTA
- [ ] UPLOAD_CODE
- [ ] LOW_PROFILE
- [ ] IMAGE_CHANGE_CODE
- [ ] MAX_RESIZED_IMAGES
- [ ] ALLOW_BLOATING
- [ ] BACKBLAZE

### Image hosting
- [x] Resizing
Expand Down
1 change: 1 addition & 0 deletions content-controllers/image/image.controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

class ImageController implements ContentController
{
public const ctype = 'static';
//returns all extensions registered by this type of content
public function getRegisteredExtensions(){return array('png','bmp','gif','jpg','jpeg','x-png','webp');}

Expand Down
Binary file not shown.
52 changes: 52 additions & 0 deletions content-controllers/placeholder/placeholder.controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

class PlaceholderController implements ContentController
{
public const ctype = 'dynamic';

//returns all extensions registered by this type of content
public function getRegisteredExtensions(){return array('placeholder');}

public function handleHash($hash,$url)
{
$path = ROOT.DS.'data'.DS.$hash.DS.$hash;

include_once(dirname(__FILE__).DS.'placeholdergenerator.php');
$pg = new PlaceholderGenerator();

foreach($url as $u)
{
if(isSize($u))
$modifiers['size'] = $u;
if(startsWith($u,'color-'))
{
$u = substr($u,6);
$colors = explode('-',$u);
foreach($colors as $c)
if(isColor($c))
$modifiers['colors'][] = (ctype_xdigit($c)?$c:color_name_to_hex($c));

if(count($modifiers['colors'])>4)
$modifiers['colors'] = array_slice($modifiers['colors'],0,4);
}
}

$img = $pg->generateImage($modifiers);

$img = $pg->gradient($img, $modifiers['colors']);
$img = $pg->addSizeText($img,$modifiers);

header ("Content-type: image/jpeg");
header ("ETag: $hash");
header('Cache-control: public, max-age=31536000');

imagejpeg($img,null,(defined('JPEG_COMPRESSION')?JPEG_COMPRESSION:90));
}

public function handleUpload($tmpfile,$hash=false)
{
return array('status'=>'err','hash'=>$hash,'reason'=>'Cannot upload to placeholder image');
}


}
88 changes: 88 additions & 0 deletions content-controllers/placeholder/placeholdergenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

class PlaceholderGenerator {

function generateImage($modifiers)
{
$size = ($modifiers['size']?:'800x600');

$sd = sizeStringToWidthHeight($size);
$width = $sd['width'];
$height = $sd['height'];

$im = imagecreatetruecolor($width, $height);

return $im;
}

function addSizeText($im,$modifiers)
{
$size = imagesx($im).'x'.imagesy($im);
$text = $size;
//add the size as text in the center of the image
$textcolor = imagecolorallocate($im, 0, 0, 0);
$font = dirname(__FILE__).DS.'fonts/RonysiswadiArchitect5-1GErv.ttf';
//calculate the size of the text to make sure it will alway be visible
$fontsize = 20;
$textsize = imagettfbbox($fontsize, 0, $font, $text);
$scaleX = imagesx($im) / ($textsize[2] - $textsize[0] + 25);
$scaleY = imagesy($im) / ($textsize[1] - $textsize[7] + 25);

$scale = min($scaleX,$scaleY);

$fontsize = 20 * $scale;
$textsize = imagettfbbox($fontsize, 0, $font, $text);
$textwidth = $textsize[2] - $textsize[0];
$textheight = $textsize[1] - $textsize[7];

if($textwidth > imagesx($im) || $textheight > imagesy($im))
return $im;

$x = (imagesx($im) - $textwidth) / 2;
$y = (imagesy($im) - $textheight) / 2 + $textheight;
imagettftext($im, $fontsize, 0, $x, $y, $textcolor, $font, $text);

return $im;
}

function gradient($im, $c) {

$w = imagesx($im);
$h = imagesy($im);

if(!$c[0]) $c = ['ffffff','ffffff','ffffff','ffffff'];
else if(!$c[1]) $c = [$c[0],$c[0],$c[0],$c[0]];
else if(!$c[2]) $c = [$c[0],$c[0],$c[1],$c[1]];
else if(!$c[3]) $c = [$c[0],$c[1],$c[2],$c[0]];

for($i=0;$i<=3;$i++) {
$c[$i]=$this->hex2rgb($c[$i]);
}

$rgb=$c[0]; // start with top left color
for($x=0;$x<=$w;$x++) { // loop columns
for($y=0;$y<=$h;$y++) { // loop rows
// set pixel color
$col=imagecolorallocate($im,$rgb[0],$rgb[1],$rgb[2]);
imagesetpixel($im,$x-1,$y-1,$col);
// calculate new color
for($i=0;$i<=2;$i++) {
$rgb[$i]=
$c[0][$i]*(($w-$x)*($h-$y)/($w*$h)) +
$c[1][$i]*($x *($h-$y)/($w*$h)) +
$c[2][$i]*(($w-$x)*$y /($w*$h)) +
$c[3][$i]*($x *$y /($w*$h));
}
}
}
return $im;
}

function hex2rgb($hex)
{
$rgb[0]=hexdec(substr($hex,0,2));
$rgb[1]=hexdec(substr($hex,2,2));
$rgb[2]=hexdec(substr($hex,4,2));
return($rgb);
}
}
2 changes: 2 additions & 0 deletions content-controllers/text/text.controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class TextController implements ContentController
{
public const ctype = 'static';

//returns all extensions registered by this type of content
public function getRegisteredExtensions(){return array('txt','text','csv');}

Expand Down
2 changes: 2 additions & 0 deletions content-controllers/url/url.controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class UrlController implements ContentController
{
public const ctype = 'static';

//returns all extensions registered by this type of content
public function getRegisteredExtensions(){return array('url');}
public function handleHash($hash,$url){}
Expand Down
2 changes: 2 additions & 0 deletions content-controllers/video/video.controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class VideoController implements ContentController
{
public const ctype = 'static';

//returns all extensions registered by this type of content
public function getRegisteredExtensions(){return array('mp4');}

Expand Down
Loading

0 comments on commit 19d7cb0

Please sign in to comment.