partstock/part_picture.php

97 lines
3 KiB
PHP
Raw Normal View History

<?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, false);
imagesavealpha($source_image, true);
$output_image = imagecreatetruecolor($output_width, $output_height);
imagealphablending($output_image, false);
imagesavealpha($output_image, true);
} 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);
?>