<?php
include_once '../sys/inc/start.php';
include_once '../sys/inc/compress.php';
include_once '../sys/inc/sess.php';
include_once '../sys/inc/home.php';
include_once '../sys/inc/settings.php';
include_once '../sys/inc/db_connect.php';
include_once '../sys/inc/ipua.php';
include_once '../sys/inc/fnc.php';
include_once '../sys/inc/user.php';
if (isset($_GET['name'])) {$name = $_GET['name'];}else{$name = "";} 
if (isset($_GET['prev'])) {$prev = $_GET['prev'];}else{$prev = 60;} 
$dir = 'blogs/pic/';
if (preg_match('|^[a-z0-9_\-/]+$|i', $dir) && preg_match('|^[a-z0-9_\.\-]+$|i', $name)) {
if (file_exists(H . $dir . '/' . $name)) {
$getim = getimagesize(H . $dir . '/' . $name);
if ($getim[2] == 1 || $getim[2] == 2 || $getim[2] == 3) {
            $width = $getim[0];
            $height = $getim[1];

            if ($width > $prev || $height > $prev) {
                $x_ratio = $prev / $width;
                $y_ratio = $prev / $height;

                if (($x_ratio * $height) < $prev) {
                    $tn_height = ceil($x_ratio * $height);
                    $tn_width = $prev;
                } else {
                    $tn_width = ceil($y_ratio * $width);
                    $tn_height = $prev;
                } 
                // -------------------------------//
                if ($getim[2] == 2) {
                    $img = imagecreatefromjpeg(H . $dir . '/' . $name);
                    $dst = imagecreatetruecolor($tn_width, $tn_height);
                    imagecopyresampled($dst, $img, 0, 0, 0, 0, $tn_width, $tn_height, $width, $height);

                    header('content-type: image/jpeg');
                    header('Content-Disposition: filename="' . $name . '"');
                    imagejpeg ($dst, null, 75);
                    imagedestroy($img);
                    imagedestroy($dst);
                } 
                // -------------------------------//
                if ($getim[2] == 1) {
                    $img = imagecreatefromgif(H . $dir . '/' . $name);
                    $dst = imagecreatetruecolor($tn_width, $tn_height);

                    $colorTransparent = imagecolortransparent($img);
                    imagepalettecopy($img, $dst);
                    imagefill($dst, 0, 0, $colorTransparent);
                    imagecolortransparent($dst, $colorTransparent);
                    imagetruecolortopalette($dst, true, 256);

                    imagecopyresampled($dst, $img, 0, 0, 0, 0, $tn_width, $tn_height, $width, $height);

                    header('content-type: image/gif');
                    header('Content-Disposition: filename="' . $name . '"');
                    imagegif ($dst);
                    imagedestroy($img);
                    imagedestroy($dst);
                } 
                // -------------------------------//
                if ($getim[2] == 3) {
                    $img = imagecreatefrompng(H . $dir . '/' . $name);
                    $dst = imagecreatetruecolor($tn_width, $tn_height);

                    $colorTransparent = imagecolortransparent($img);
                    imagepalettecopy($img, $dst);
                    imagefill($dst, 0, 0, $colorTransparent);
                    imagecolortransparent($dst, $colorTransparent);
                    imagetruecolortopalette($dst, true, 256);

                    imagecopyresampled($dst, $img, 0, 0, 0, 0, $tn_width, $tn_height, $width, $height);

                    header('content-type: image/png');
                    header('Content-Disposition: filename="' . $name . '"');
                    imagepng ($dst);
                    imagedestroy($img);
                    imagedestroy($dst);
                } 
            } else {
                $filename = file_get_contents(H . $dir . '/' . $name);
                header('Content-type: ' . $getim['mime']);
                header('Content-Disposition: filename="' . $name . '"');
                header('Content-Length: ' . strlen($filename));
                echo $filename;
            } 
        } 
    } 
} 

exit;

?>