From 823f7e60d88c826389be9502a10f93dbbe5f1ba1 Mon Sep 17 00:00:00 2001 From: Chris Date: Sun, 26 Aug 2018 15:50:10 +0200 Subject: [PATCH] added gradient support. closes #55 --- README.md | 13 ++------- inc/core.php | 21 +++++++++++++- models/pictsharemodel.php | 59 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 578e97c..68e9c12 100644 --- a/README.md +++ b/README.md @@ -79,17 +79,6 @@ By using this compose file, you should know that: - (optional) You can and should put a [nginx](https://www.nginx.com/) proxy before the Apache server. That thing is just insanely fast with static content like images. - (optional) To secure your traffic I'd highly recommend getting an [SSL Cert](https://letsencrypt.org/) for your server if you don't already have one. - -UPDATES -======== -- Nov. 23: Added support for MP4 uploads and conversion from gif to MP4 -- Nov. 12: Created new git project: [Pictshare stats](https://github.com/chrisiaut/pictshare_stats) -- Nov. 07: Added 9 new (instagram-like) filters -- Nov. 06: Master delete code. One code to delete them all -- Nov. 01: [Restricted uploads and option-use](#restriction-settings) -- Oct. 30: [Rotations and filters](#smart-query-system) -- Oct. 10: [Album functionality](#smart-query-system) finally ready - ## Why would I want to host my own images? If you own a server (even a home server) you can host your own PictShare instance so you have full control over your content and can delete images hasslefree. @@ -106,10 +95,12 @@ If you're a blogger like myself, you can use it as storage for your images so th - MP4 resizing - PictShare removes all exif data so you can upload photos from your phone and all GPS tags and camera model info get wiped - Smart [resize, filter and rotation](#smart-query-system) features +- Generate gradients by specifying only a size. eg: https://pictshare.net/800x200 - Duplicates don't take up space. If the exact same images is uploaded twice, the second upload will link to the first - You can control who can upload images or use filters/resizes by defining an [upload-code](#restriction-settings) - You can set a code in your ```/inc/config.inc.php``` (MASTER_DELETE_CODE) that, if appended to any URL of an Image, will delete the image and all cached versions of it from the server - Detailed traffic and view statistics of your images via [Pictshare stats](https://github.com/chrisiaut/pictshare_stats) +- For more configuration possibilities check out the ```/inc/example.config.inc.php``` file ## Smart query system PictShare images can be changed after upload just by modifying the URL. It works like this: diff --git a/inc/core.php b/inc/core.php index 580da8b..c7675d0 100644 --- a/inc/core.php +++ b/inc/core.php @@ -84,7 +84,26 @@ function whatToDo($url) $data = $pm->urlToData($url); - if(!is_array($data) || !$data['hash']) + if(!$data['hash'] && $data['size']) //if there is only a size but no hash, generate a pattern + { + $sd = $pm->sizeStringToWidthHeight($data['size']); + $width = ($sd['width'] <= 2000)?$sd['width']:2000; + $height = ($sd['height'] <= 2000)?$sd['height']:2000; + + $cachefile = ROOT.DS.'tmp'.DS.$width .'x'.$height.'.png'; + if(!file_exists($cachefile)) + { + $image=$pm->gradient($width, $height, array('#A7FF78', '#A7FF78', '#78FFD4', '#78FFD4')); + imagepng($image,$cachefile); + } + else + $image = imagecreatefrompng($cachefile); + header('Content-type: image/png'); + imagepng($image); + imagedestroy($image); + exit(); + } + else if(!is_array($data) || !$data['hash']) { if((UPLOAD_FORM_LOCATION && $url==UPLOAD_FORM_LOCATION) || (!UPLOAD_FORM_LOCATION)) { diff --git a/models/pictsharemodel.php b/models/pictsharemodel.php index e16aba0..cc1ae61 100644 --- a/models/pictsharemodel.php +++ b/models/pictsharemodel.php @@ -1042,4 +1042,63 @@ class PictshareModel extends Model return array('width'=>$maxwidth,'height'=>$maxheight); } + + function gradient($w=100, $h=100, $c=array('#FFFFFF','#FF0000','#00FF00','#0000FF'), $hex=true) + { + /* + Generates a gradient image + + Author: Christopher Kramer + + Parameters: + w: width in px + h: height in px + c: color-array with 4 elements: + $c[0]: top left color + $c[1]: top right color + $c[2]: bottom left color + $c[3]: bottom right color + + if $hex is true (default), colors are hex-strings like '#FFFFFF' (NOT '#FFF') + if $hex is false, a color is an array of 3 elements which are the rgb-values, e.g.: + $c[0]=array(0,255,255); + + */ + + $im=imagecreatetruecolor($w,$h); + + if($hex) { // convert hex-values to rgb + 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,1,2)); + $rgb[1]=hexdec(substr($hex,3,2)); + $rgb[2]=hexdec(substr($hex,5,2)); + return($rgb); + } + } +