<?php
// include composer autoload
require getcwd() . '/vendor/autoload.php';
// import the Intervention Image Manager Class
use Intervention\Image\ImageManager;

require_once(getcwd() . '/_connect.2018.php');

define('PUBLIC_DIR', '/home/webjeban/jeban-2017/public/');
define('PUBLIC_URI', 'https://www.jeban.com/');

$topic_id = (int) $_GET['t'];

$dimension = $_GET['d'];
$dimensions = explode('x', $dimension);

$width = (int) $dimensions[0];
$height = (int) $dimensions[1];

if (($width == 0) || ($height == 0) || ($topic_id == 0)) {
	die('Invalid dimension.');
}

$imagefolder = PUBLIC_DIR . 'userfiles/thumbs/topics';
if (file_exists($imagefolder) == false) {
    mkdir($imagefolder);
    chmod($imagefolder, 0755);
}
$imagefolder .= '/' . $width . 'x' . $height;
if (file_exists($imagefolder) == false) {
    mkdir($imagefolder);
    chmod($imagefolder, 0755);
}

$original_img = '';
$thumb_img = $imagefolder . "/$topic_id.jpg";
$thumb_url = str_replace(PUBLIC_DIR, PUBLIC_URI, $thumb_img);
@unlink($thumb_img);
if (file_exists($thumb_img)) {
    header("Location: $thumb_url");
    die(0);
}

$sql = "SELECT cover_url FROM topics WHERE id = :topicId LIMIT 0, 1";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':topicId', $topic_id);
$stmt->execute();

$row = $stmt->fetch();
        
$cover_url = $row['cover_url'];
if (strrpos($cover_url, ".jeban.com/") === false) {
	die('not in jeban');
} else {
	if (!file_exists(str_replace(PUBLIC_URI, PUBLIC_DIR, $cover_url))) {
		$cover_url = '';
	}
}
if (!$cover_url) {
	$sql = "SELECT 
				i.image_version,
				CASE i.image_version
					WHEN 0 THEN image_name
					ELSE CONCAT('/', image_year, '/', LPAD(image_month, 2, '0'), '/', image_name)
				END AS image_name
			FROM 
				blocks b, block_image bi, images i 
			WHERE 
				b.id = bi.block_id
				AND bi.image_id = i.id
				AND content_type = 'TOPIC' 
				AND content_id = :topicId 
				AND block_type = 'IMAGE'
				AND image_name NOT LIKE '%/emo/%'
			ORDER BY
				b.block_order
			LIMIT
				0, 1";
	$stmt = $pdo->prepare($sql);
	$stmt->bindParam(':topicId', $topic_id);
	$stmt->execute();
	
	$row = $stmt->fetch();

	if ($row['image_version'] == 0) {
		$cover_url = $row['image_name'];
	} else {
		$cover_url = PUBLIC_DIR . $row['image_name'];
	}
}
$original_img = $cover_url;

$default_covers = array(
	'https://www.jeban.com/images/jeban-default-cover-01.png',
	'https://www.jeban.com/images/jeban-default-cover-02.png',
	'https://www.jeban.com/images/jeban-default-cover-03.png'
);
shuffle($default_covers);
$substitute_image = $default_covers[0];

if ($original_img) {
	try {
		// create an image manager instance with favored driver
		$manager = new ImageManager();
		
		// to finally create image instances
		$image = $manager->make($original_img)->encode('jpg', 90);
		$image->fit($width, $height, function ($constraint) { $constraint->aspectRatio(); },  'center');
		
		try {
			if (file_exists($thumb_img)) { @unlink($thumb_img); }
			$image->save($thumb_img);

			$imagine = new Imagine\Gd\Imagine();			
			$image = $imagine->open($thumb_img)->show('jpg');
			
			exit;
		} catch (Exception $e) {
			//echo $e->getMessage(), '<br>';
		} finally {
			header("Location: $thumb_url");
		}
	} catch (Exception $e) {
		header("Location: https://www.jeban.com/images/jeban-default-cover-03.png");
	}
} else {
	header("Location: https://www.jeban.com/images/jeban-default-cover-03.png");
}
?>