<?php require_once 'library/utility.php'; require_once 'library/database.php'; global $database; $id = array_get($_GET, 'id'); $width = array_get($_GET, 'width', ''); $height = array_get($_GET, 'height', ''); $format = $width . "x" . $height; // Validate the parameters $formats = array("480x"); if (!is_scalar($id) || !is_scalar($width) || !is_scalar($height)) { die; } if (!in_array($format, $formats)) { die; } $commentFile = $database->getCommentFileByID($id); if (!$commentFile) { die; } $picture = getCommentUploadImagePath($commentFile['uploadfile']); $cached_picture = getCommentUploadImagePath($commentFile['uploadfile'], $width, $height); if (file_exists($cached_picture)) { $image = $cached_picture; } elseif (!file_exists($picture)) { $image = getDefaultImagePath(); } else { $image = getImage($picture, $cached_picture, $width, $height); } serveImage($image);
function getImage($picture, $path, $width = null, $height = null) { $image_type = exif_imagetype($picture); switch ($image_type) { case IMAGETYPE_GIF: $img = imagecreatefromgif($picture); break; case IMAGETYPE_JPEG: $img = imagecreatefromjpeg($picture); break; case IMAGETYPE_PNG: $img = imagecreatefrompng($picture); break; default: return getDefaultImagePath(); } $w_org = imagesx($img); $h_org = imagesy($img); $w = $width ?: $w_org; $h = $height ?: $h_org; if ($w_org == $w && $h_org <= $h || $h_org == $h && $w_org <= $w) { // no resampling needed $img2 = $img; $h = $h_org; $w = $w_org; } else { if ($w >= $w_org && $h >= $h_org) { // no resampling needed $img2 = $img; $h = $h_org; $w = $w_org; } else { // resampling $ratio = $w_org / $h_org; if ($w / $h > $ratio) { $w = $h * $ratio; } else { $h = $w / $ratio; } $img2 = imagecreatetruecolor($w, $h); imagecopyresampled($img2, $img, 0, 0, 0, 0, $w, $h, $w_org, $h_org); imagedestroy($img); } } if (WATERMARK_SNAPS != '') { // Watermark the picture with text $color = str_replace("#", "", TEXT_COLOR); $red = hexdec(substr($color, 0, 2)); $green = hexdec(substr($color, 2, 2)); $blue = hexdec(substr($color, 4, 2)); $text_color = imagecolorallocate($img2, $red, $green, $blue); $text_height = imagefontheight(WATERMARK_TEXT_FONT); $text_width = strlen(WATERMARK_TEXT) * imagefontwidth(WATERMARK_TEXT_FONT); $wt_y = WATERMARK_MARGIN; if (WATERMARK_ALIGN_V == 'top') { $wt_y = WATERMARK_MARGIN; } elseif (WATERMARK_ALIGN_V == 'bottom') { $wt_y = $h - $text_height - WATERMARK_MARGIN; } elseif (WATERMARK_ALIGN_V == 'center') { $wt_y = (int) ($h / 2 - $text_height / 2); } $wt_x = WATERMARK_MARGIN; if (WATERMARK_ALIGN_H == 'left') { $wt_x = WATERMARK_MARGIN; } elseif (WATERMARK_ALIGN_H == 'right') { $wt_x = $w - $text_width - WATERMARK_MARGIN; } elseif (WATERMARK_ALIGN_H == 'center') { $wt_x = (int) ($w / 2 - $text_width / 2); } if (TEXT_SHADOW == '1') { imagestring($img2, WATERMARK_TEXT_FONT, $wt_x + 1, $wt_y + 1, WATERMARK_TEXT, 0); } imagestring($img2, WATERMARK_TEXT_FONT, $wt_x, $wt_y, WATERMARK_TEXT, $text_color); } if (!is_dir(dirname($path))) { mkdir(dirname($path), 0755, true); } switch ($image_type) { case IMAGETYPE_GIF: imagegif($img2, $path); break; case IMAGETYPE_JPEG: imagejpeg($img2, $path); break; case IMAGETYPE_PNG: imagepng($img2, $path); break; } imagedestroy($img2); return $path; }