95 lines
2.9 KiB
PHP
95 lines
2.9 KiB
PHP
<?php
|
|
// Script that generates image data for a requested part in fbsql_database
|
|
//
|
|
// possible GET valiables:
|
|
// PartId (required) - Id of part in databse
|
|
// MaxWidth (optional) - When given, image is scaled down to that maximum width
|
|
// MaxHeight (optional) - When given, image is scaled down to that maximum height
|
|
|
|
|
|
//main include code
|
|
include ("./includes/globals.php");
|
|
include ("./includes/load_config.php");
|
|
include ("./includes/log.php");
|
|
include ("./includes/language.php");
|
|
include ("./includes/mysql.php");
|
|
include ("./includes/other_functions.php");
|
|
|
|
|
|
// get part Id
|
|
if (!isset($_GET['PartId'])) {
|
|
exit("ERROR: PartId no given!");
|
|
}
|
|
$part_id = intval($_GET['PartId']);
|
|
|
|
|
|
// get picture path
|
|
$db_query = "SELECT PicturePath FROM `Parts` WHERE `Id` = $part_id;";
|
|
$db_result = mysqli_query($GlobalMysqlHandler, $db_query);
|
|
if ($db_result->num_rows == 0) {
|
|
exit("ERROR: PartId not found!");
|
|
}
|
|
$source_path = $GlobalPictureDir . "/" . $db_result->fetch_array()['PicturePath'];
|
|
|
|
|
|
// get image info
|
|
list($source_width, $source_height) = getimagesize($source_path);
|
|
$mime_type = mime_content_type($source_path);
|
|
|
|
|
|
// calculate new With and Height
|
|
$output_width = $source_width;
|
|
$output_height = $source_height;
|
|
$max_width = 0;
|
|
if (isset($_GET['MaxWidth'])) {
|
|
$max_width = intval($_GET['MaxWidth']);
|
|
if ($output_width > $max_width) {
|
|
$scale_factor = $max_width / $output_width;
|
|
$output_width *= $scale_factor;
|
|
$output_height *= $scale_factor;
|
|
}
|
|
}
|
|
$max_height = 0;
|
|
if (isset($_GET['MaxHeight'])) {
|
|
$max_height = intval($_GET['MaxHeight']);
|
|
if ($output_height > $max_height) {
|
|
$scale_factor = $max_height / $output_height;
|
|
$output_width *= $scale_factor;
|
|
$output_height *= $scale_factor;
|
|
}
|
|
}
|
|
|
|
|
|
// load image
|
|
if ($mime_type == 'image/jpeg') {
|
|
$source_image = imagecreatefromjpeg($source_path);
|
|
$output_image = imagecreatetruecolor($output_width, $output_height);
|
|
} else if ($mime_type == 'image/png') {
|
|
$source_image = imagecreatefrompng($source_path);
|
|
imagealphablending($source_image, true);
|
|
imagesavealpha($source_image, true);
|
|
$output_image = imagecreate($output_width, $output_height);
|
|
} else {
|
|
ErrorLog("Unkown mime type: " . $mime_type . "!");
|
|
exit("Can't handle requested mime-type.");
|
|
}
|
|
|
|
|
|
// resize
|
|
$res = imagecopyresized($output_image, $source_image, 0, 0, 0, 0, $output_width, $output_height, $source_width, $source_height);
|
|
if ($res !== True) {
|
|
$msg = "Cannot resize image: " . $source_path . "\n";
|
|
$msg .= "source: " . $source_width . "x" . $source_height;
|
|
$msg .= ", max: " . $max_width . "x" . $max_height;
|
|
$msg .= ", output: " . $output_width . "x" . $output_height;
|
|
ErrorLog($msg);
|
|
exit("Cannot resize image!");
|
|
}
|
|
|
|
|
|
// output as png
|
|
header('Content-Type: image/png');
|
|
imagepng($output_image);
|
|
imagedestroy($output_image);
|
|
imagedestroy($source_width);
|
|
?>
|