Example #1
0
 /**
  * Check image size with a preset minimum
  *
  * @param string $path
  * @param int $width
  * @param int $height
  * @param int $minwidth
  * @param int $minheight
  */
 static function checkSize($path, $width, $height, $minwidth, $minheight)
 {
     if ($width < $minwidth || $height < $minheight) {
         unlink($path);
         Response::fail('The image is too small in ' . ($width < $minwidth ? $height < $minheight ? 'width and height' : 'width' : ($height < $minheight ? 'height' : '')) . ", please uploadd a bigger image.<br>The minimum size is {$minwidth}px by {$minheight}px.</p>");
     }
 }
Example #2
0
 /**
  * Sets a global cofiguration item's value
  *
  * @param string $key
  * @param mixed $value
  *
  * @return bool
  */
 static function set($key, $value)
 {
     global $Database;
     if (!isset(static::$_defaults[$key])) {
         Response::fail("Key {$key} is not allowed");
     }
     $default = static::$_defaults[$key];
     if ($Database->where('key', $key)->has(static::$_db)) {
         $Database->where('key', $key);
         if ($value == $default) {
             $Database->delete(static::$_db);
         } else {
             return $Database->update(static::$_db, array('value' => $value));
         }
     } else {
         if ($value != $default) {
             return $Database->insert(static::$_db, array('key' => $key, 'value' => $value));
         } else {
             return true;
         }
     }
 }
Example #3
0
<?php

require "../lib/sns.php";
try {
    SnS::init();
    SnS::run();
} catch (Exception $e) {
    Response::fail($e->getMessage(), $e->getHTTPCode());
}
Example #4
0
 private function _outputError($message, $errorCode = null)
 {
     $message = str_replace('@value', CoreUtils::escapeHTML($this->_value), $message);
     if ($errorCode === self::ERROR_RANGE) {
         if (isset($this->_range[0])) {
             $message = str_replace('@min', $this->_range[0], $message);
         }
         if (isset($this->_range[1])) {
             $message = str_replace('@max', $this->_range[1], $message);
         }
     }
     if ($this->_silentFail) {
         return error_log("Silenced Input validation error: {$message}\nKey: {$this->_key}\nOptions: _source={$this->_source}, _origValue={$this->_origValue}, _respond={$this->_respond}, request_uri={$_SERVER['REQUEST_URI']}");
     }
     if ($this->_respond) {
         Response::fail($message);
     }
     throw new \Exception($message);
 }
Example #5
0
 /**
  * Render appearance PNG image
  *
  * @param array $Appearance
  *
  * @throws \Exception
  */
 static function renderAppearancePNG($Appearance)
 {
     global $CGPath;
     $OutputPath = FSPATH . "cg_render/{$Appearance['id']}-palette.png";
     $FileRelPath = "{$CGPath}/v/{$Appearance['id']}.png";
     if (file_exists($OutputPath)) {
         Image::outputPNG(null, $OutputPath, $FileRelPath);
     }
     $OutWidth = 0;
     $OutHeight = 0;
     $SpriteWidth = $SpriteHeight = 0;
     $SpriteRightMargin = 10;
     $ColorCircleSize = 17;
     $ColorCircleRMargin = 5;
     $ColorNameFontSize = 12;
     $FontFile = APPATH . 'font/Celestia Medium Redux.ttf';
     //$PixelatedFontFile = APPATH.'font/Volter (Goldfish).ttf';
     $PixelatedFontFile = $FontFile;
     if (!file_exists($FontFile)) {
         throw new \Exception('Font file missing');
     }
     $Name = $Appearance['label'];
     $NameVerticalMargin = 5;
     $NameFontSize = 22;
     $TextMargin = 10;
     $ColorsOutputted = 0;
     $SplitTreshold = 12;
     $ColumnRightMargin = 20;
     // Detect if sprite exists and adjust image size & define starting positions
     $SpritePath = SPRITE_PATH . "{$Appearance['id']}.png";
     $SpriteExists = file_exists($SpritePath);
     if ($SpriteExists) {
         $SpriteSize = getimagesize($SpritePath);
         $Sprite = Image::preserveAlpha(imagecreatefrompng($SpritePath));
         $SpriteHeight = $SpriteSize[HEIGHT];
         $SpriteWidth = $SpriteSize[WIDTH];
         $SpriteRealWidth = $SpriteWidth + $SpriteRightMargin;
         $OutWidth = $SpriteRealWidth;
         $OutHeight = $SpriteHeight;
     } else {
         $SpriteRealWidth = 0;
     }
     $origin = array('x' => $SpriteExists ? $SpriteRealWidth : $TextMargin, 'y' => 0);
     // Get color groups & calculate the space they take up
     $ColorGroups = ColorGroups::get($Appearance['id']);
     $CGCount = count($ColorGroups);
     $CGFontSize = round($NameFontSize / 1.25);
     $CGVerticalMargin = $NameVerticalMargin;
     $GroupLabelBox = Image::saneGetTTFBox($CGFontSize, $FontFile, 'ABCDEFGIJKLMOPQRSTUVWQYZabcdefghijklmnopqrstuvwxyz');
     $ColorNameBox = Image::saneGetTTFBox($ColorNameFontSize, $PixelatedFontFile, 'AGIJKFagijkf');
     $CGsHeight = $CGCount * ($GroupLabelBox['height'] + $CGVerticalMargin * 2 + $ColorCircleSize);
     // Get export time & size
     $ExportTS = "Generated at: " . Time::format(time(), Time::FORMAT_FULL);
     $ExportFontSize = round($CGFontSize / 1.5);
     $ExportBox = Image::saneGetTTFBox($ExportFontSize, $FontFile, $ExportTS);
     // Check how long & tall appearance name is, and set image width
     $NameBox = Image::saneGetTTFBox($NameFontSize, $FontFile, $Name);
     $OutWidth = $origin['x'] + max($NameBox['width'], $ExportBox['width']) + $TextMargin;
     // Set image height
     $OutHeight = max($origin['y'] + ($NameVerticalMargin * 4 + $NameBox['height'] + $ExportBox['height'] + $CGsHeight), $OutHeight);
     // Create base image
     $BaseImage = Image::createTransparent($OutWidth, $OutHeight);
     $BLACK = imagecolorallocate($BaseImage, 0, 0, 0);
     // If sprite exists, output it on base image
     if ($SpriteExists) {
         Image::copyExact($BaseImage, $Sprite, 0, 0, $SpriteWidth, $SpriteHeight);
     }
     // Output appearance name
     $origin['y'] += $NameVerticalMargin * 2;
     Image::writeOn($BaseImage, $Name, $origin['x'], $NameFontSize, $BLACK, $origin, $FontFile);
     $origin['y'] += $NameVerticalMargin;
     // Output generation time
     Image::writeOn($BaseImage, $ExportTS, $origin['x'], $ExportFontSize, $BLACK, $origin, $FontFile);
     $origin['y'] += $NameVerticalMargin;
     if (!empty($ColorGroups)) {
         $LargestX = 0;
         $LargestLabel = '';
         $AllColors = ColorGroups::getColorsForEach($ColorGroups);
         foreach ($ColorGroups as $cg) {
             $CGLabelBox = Image::saneGetTTFBox($CGFontSize, $FontFile, $cg['label']);
             Image::calcRedraw($OutWidth, $OutHeight, $CGLabelBox['width'] + $TextMargin, $GroupLabelBox['height'] + $NameVerticalMargin + $CGVerticalMargin, $BaseImage, $origin);
             Image::writeOn($BaseImage, $cg['label'], $origin['x'], $CGFontSize, $BLACK, $origin, $FontFile, $GroupLabelBox);
             $origin['y'] += $GroupLabelBox['height'] + $CGVerticalMargin;
             if ($CGLabelBox['width'] > $LargestX) {
                 $LargestX = $CGLabelBox['width'];
                 $LargestLabel = $cg['label'];
             }
             if (!empty($AllColors[$cg['groupid']])) {
                 foreach ($AllColors[$cg['groupid']] as $c) {
                     $ColorNameLeftOffset = $ColorCircleSize + $ColorCircleRMargin;
                     $CNBox = Image::saneGetTTFBox($ColorNameFontSize, $PixelatedFontFile, $c['label']);
                     $WidthIncrease = $ColorNameLeftOffset + $CNBox['width'] + $TextMargin;
                     $HeightIncrease = max($ColorCircleSize, $CNBox['height']) + $CGVerticalMargin;
                     Image::calcRedraw($OutWidth, $OutHeight, $WidthIncrease, $HeightIncrease, $BaseImage, $origin);
                     Image::drawCircle($BaseImage, $origin['x'], $origin['y'], $ColorCircleSize, $c['hex'], $BLACK);
                     $yOffset = 2;
                     Image::writeOn($BaseImage, $c['label'], $origin['x'] + $ColorNameLeftOffset, $ColorNameFontSize, $BLACK, $origin, $PixelatedFontFile, $ColorNameBox, $yOffset);
                     $origin['y'] += $HeightIncrease;
                     $ColorsOutputted++;
                     $TotalWidth = $ColorNameLeftOffset + $CNBox['width'];
                     if ($TotalWidth > $LargestX) {
                         $LargestX = $TotalWidth;
                         $LargestLabel = $c['label'];
                     }
                 }
             }
             if ($ColorsOutputted > $SplitTreshold) {
                 Image::calcRedraw($OutWidth, $OutHeight, 0, $NameVerticalMargin, $BaseImage, $origin);
                 $origin['y'] = $NameVerticalMargin * 4 + Image::saneGetTTFBox($NameFontSize, $FontFile, $Name)['height'] + Image::saneGetTTFBox($ExportFontSize, $FontFile, $ExportTS)['height'];
                 $origin['x'] += $LargestX + $ColumnRightMargin;
                 $ColorsOutputted = 0;
                 $LargestX = 0;
             } else {
                 $origin['y'] += $NameVerticalMargin;
             }
         }
     }
     $FinalBase = Image::createWhiteBG($OutWidth, $OutHeight);
     Image::drawSquare($FinalBase, 0, 0, array($OutWidth, $OutHeight), null, $BLACK);
     Image::copyExact($FinalBase, $BaseImage, 0, 0, $OutWidth, $OutHeight);
     if (!CoreUtils::createUploadFolder($OutputPath)) {
         Response::fail('Failed to create render directory');
     }
     Image::outputPNG($FinalBase, $OutputPath, $FileRelPath);
 }
Example #6
0
 /**
  * Processes a preference item's new value
  *
  * @param string $key
  *
  * @return mixed
  */
 static function process($key)
 {
     $value = isset($_POST['value']) ? CoreUtils::trim($_POST['value']) : null;
     switch ($key) {
         case "cg_itemsperpage":
             $thing = 'Color Guide items per page';
             if (!is_numeric($value)) {
                 throw new \Exception("{$thing} must be a number");
             }
             $value = intval($value, 10);
             if ($value < 7 || $value > 20) {
                 throw new \Exception("{$thing} must be between 7 and 20");
             }
             break;
         case "p_vectorapp":
             if (!empty($value) && !isset(CoreUtils::$VECTOR_APPS[$value])) {
                 throw new \Exception("The specified app is invalid");
             }
             break;
         case "p_hidediscord":
         case "p_disable_ga":
         case "cg_hidesynon":
         case "cg_hideclrinfo":
             $value = $value ? 1 : 0;
             break;
         case "discord_token":
             Response::fail("You cannot change the {$key} setting");
     }
     return $value;
 }