这实际上是一个答案,而不是一个问题,但是,当然,欢迎评论或其他解决方案,只要它们不涉及将映像保存到MySQL的对与错。可以说这是二十年前创建该网站的原因!
重建了我的一个Web开发系统之后,该脚本在整个站点之一中都用于从MySQL获取图像,并在需要时添加文本或图像水印,然后将其显示在屏幕上,该脚本不起作用。该脚本已经使用了15年以上,但是无法在我的本地副本上使用。在花了近一个星期的时间后,我突然意识到这是因为脚本文件本身编码不正确!将其编码为UTF-8即可解决问题。
发生的事情是,正确启用了标题后,会出现一个错误,即“由于包含错误,无法显示图像”,并且标出了标题,它将仅显示blob内容。该文件已正确编码,可以正常工作!
这是我的脚本,以防它对其他人有帮助,但是请注意,它确实使用了一些自定义数据库连接功能,因此需要重写数据库调用才能在现实世界中使用。
<?php
// Create variables from parameters
$ImageID = (isset($_GET['ID']) && is_numeric($_GET['ID'])) ? $_GET['ID'] : "";
$ImageType = (isset($_GET['Type']) && is_numeric($_GET['Type'])) ? $_GET['Type'] : "";
$Rand = (isset($_GET['Rand']) && is_numeric($_GET['Rand'])) ? $_GET['Rand'] : "";
// Be sure suffcient information was recieved to try to find image
if (!$ImageID && !$Rand == 1) die ("No image");
// Build dynamic path to common and secure functions
$dirSeparator = DIRECTORY_SEPARATOR;
$serverRoot = str_replace("/",$dirSeparator,$_SERVER['DOCUMENT_ROOT']);
$SiteFolder = @end(explode($dirSeparator,$serverRoot));
$CommonPath = str_replace($SiteFolder,"",$serverRoot) . "common" . $dirSeparator; // With symbolic link
$SecurePath = str_replace($SiteFolder,"",$serverRoot) . "secure" . $dirSeparator; // Without symbolic link
// Get MySQL connection informaton and database functons
require("{$SecurePath}config.php");
require("{$CommonPath}db_mysql.php");
// Determine which table and image type to open, then create variables for the result
switch ($ImageType) :
case 1:
$sqlSelect = "SELECT Image, ImageFormat, Description, WatermarkLevel FROM table1 WHERE ID=$ImageID";
$row = DBConnect($sqlSelect,"Select","db_name");
if (is_array($row)) :
$im = $row['Image'];
$mime_type = $row['ImageFormat'];
$ImageName = $row['Description'];
$WatermarkLevel = $row['WatermarkLevel'];
$EnableText = 1;
endif;
break;
case 2:
$sqlSelect = "SELECT Thumbnail, ImageFormat, Description FROM table1 WHERE ID=$ImageID";
$row = DBConnect($sqlSelect, "Select", "db_name");
if (is_array($row)) :
$im = $row['Thumbnail'];
$mime_type = $row['ImageFormat'];
$ImageName = $row['Description'] . "_Thumbnail";
$WatermarkLevel = 0;
$EnableText = 0;
endif;
break;
case 3:
$sqlSelect = "SELECT Image, ImageFormat, Name, WatermarkLevel FROM table2 WHERE ID=$ImageID";
$row = DBConnect($sqlSelect, "Select", "db_name");
if (is_array($row)) :
$im = $row['Image'];
$mime_type = $row['ImageFormat'];
$NameParts = explode(".", $row['Name']);
$ImageName = $NameParts[0];
$WatermarkLevel = $row['WatermarkLevel'];
$EnableText = 0;
endif;
break;
case 4:
$sqlSelect = "SELECT Image, ImageFormat, Description, WatermarkLevel FROM table3 WHERE ID=$ImageID";
$row = DBConnect($sqlSelect, "Select", "db_name");
if (is_array($row)) :
$im = $row['Image'];
$mime_type = $row['ImageFormat'];
$NameParts = explode(".", $row['Description']);
$ImageName = $NameParts[0];
$WatermarkLevel = $row['WatermarkLevel'];
$EnableText = 0;
endif;
break;
case 5:
$sqlSelect = "SELECT Thumbnail, ImageFormat, Description FROM table3 WHERE ID=$ImageID";
$row = DBConnect($sqlSelect, "Select", "db_name");
if (is_array($row)) :
$im = $row['Thumbnail'];
$mime_type = $row['ImageFormat'];
$NameParts = explode(".", $row['Description']);
$ImageName = $NameParts[0] . "_Thumbnail";
$WatermarkLevel = 0;
$EnableText = 0;
endif;
break;
case 6:
$sqlSelect = "SELECT Image, ImageFormat, Description, WatermarkLevel FROM table4 WHERE ID=$ImageID";
$row = DBConnect($sqlSelect, "Select", "db_name");
if (is_array($row)) :
$im = $row['Image'];
$mime_type = $row['ImageFormat'];
$ImageName = str_replace("<br>", " - ", $row['Description']);
$ImageName = strip_tags($ImageName);
$WatermarkLevel = $row['WatermarkLevel'];
$EnableText = 0;
endif;
break;
case 7:
$sqlSelect = "SELECT Thumbnail, ImageFormat, SortDescription FROM table4 WHERE ID=$ImageID";
$row = DBConnect($sqlSelect, "Select", "db_name");
if (is_array($row)) :
$im = $row['Thumbnail'];
$mime_type = $row['ImageFormat'];
$ImageName = str_replace("<br>", " - ", $row['Description']);
$ImageName = strip_tags($ImageName) . "_Thumbnail";
$WatermarkLevel = 0;
$EnableText = 0;
endif;
break;
case 8:
$sqlSelect = "SELECT Image, ImageFormat, ItemName, WatermarkLevel FROM table5 WHERE ID=$ImageID";
$row = DBConnect($sqlSelect, "Select", "db_name");
if (is_array($row)) :
$im = $row['Image'];
$mime_type = $row['ImageFormat'];
$NameParts = explode(".", $row['ItemName']);
$ImageName = $NameParts[0];
$WatermarkLevel = $row['WatermarkLevel'];
$EnableText = 0;
endif;
break;
case 9:
$sqlSelect = "SELECT Thumbnail, ImageFormat, ItemName FROM table5 WHERE ID=$ImageID";
$row = DBConnect($sqlSelect, "Select", "db_name");
if (is_array($row)) :
$im = $row['Thumbnail'];
$mime_type = $row['ImageFormat'];
$NameParts = explode(".", $row['ItemName']);
$ImageName = $NameParts[0] . "_Thumbnail";
$WatermarkLevel = 0;
$EnableText = 0;
endif;
break;
case 10:
$sqlSelect = "SELECT Image, ImageFormat, Description FROM table6 WHERE Image IS NOT NULL AND RandomType = 1 ORDER BY RAND() LIMIT 1";
$row = DBConnect($sqlSelect, "Select", "db_name");
if (is_array($row)) :
$im = $row['Image'];
$mime_type = $row['ImageFormat'];
$NameParts = explode(".", $row['Description']);
$ImageName = $NameParts[0];
$WatermarkLevel = 30;
$EnableText = 0;
endif;
break;
case 11:
$sqlSelect = "SELECT Image, ImageFormat, Description FROM table6 WHERE Image IS NOT NULL AND RandomType = 2 ORDER BY RAND() LIMIT 1";
$row = DBConnect($sqlSelect, "Select", "db_name");
if (is_array($row)) :
$im = $row['Image'];
$mime_type = $row['ImageFormat'];
$NameParts = explode(".", $row['Description']);
$ImageName = $NameParts[0];
$WatermarkLevel = 0;
$EnableText = 0;
endif;
break;
case 12:
$sqlSelect = "SELECT Image, ImageFormat, Description FROM table6 WHERE Image IS NOT NULL AND RandomType = 3 ORDER BY RAND() LIMIT 1";
$row = DBConnect($sqlSelect, "Select", "db_name");
if (is_array($row)) :
$im = $row['Image'];
$mime_type = $row['ImageFormat'];
$NameParts = explode(".", $row['Description']);
$ImageName = $NameParts[0];
$WatermarkLevel = 80;
$EnableText = 1;
endif;
break;
case 13:
$sqlSelect = "SELECT OriginalDoc, AdminName, MimeType, WatermarkLevel FROM table7 WHERE ID=$ImageID";
$row = DBConnect($sqlSelect, "Select", "db_name");
if (is_array($row)) :
$im = $row['OriginalDoc'];
$mime_type = $row['MimeType'];
$ImageName = str_replace(" ", "_",$row['AdminName']);
$WatermarkLevel = $row['WatermarkLevel'];
$EnableText = 1;
endif;
break;
default:
$sqlSelect = "SELECT Image, ImageFormat, Description, WatermarkLevel FROM table2 WHERE ID=$ImageID";
$row = DBConnect($sqlSelect, "Select", "db_name");
if (is_array($row)) :
$im = $row['Image'];
$mime_type = $row['ImageFormat'];
$ImageName = $row['Description'];
$WatermarkLevel = $row['WatermarkLevel'];
$EnableText = 1;
endif;
endswitch;
if (isset($im) && isset($mime_type) && $mime_type !== "pdf" && $ImageType != 13) :
$im = ImageCreateFromString($im);
if (!isset($_SESSION['HideWatermark'])) :
$ServerPath = getenv("DOCUMENT_ROOT");
if ($WatermarkLevel && $WatermarkLevel != "0") :
// For image-based watermark, lower right
$WaterMark = ImageCreateFromString(DBLookup("SELECT Image FROM table3 WHERE ID=1", "db_name"));
$wmX = ImageSx($im) -35;
$wmY = ImageSy($im) -50;
ImageCopyMerge($im, $WaterMark, $wmX, $wmY, 0, 0, imageSX($WaterMark), imageSY($WaterMark), $WatermarkLevel);
endif;
if ($EnableText && $ImageType != 12) :
// For single-line text-type watermark lower center
$string = "SiteName.com";
$font = $ServerPath . "/internals/truetype/font1.ttf";
$fontsize = 8;
$FontColor = ImageColorAllocate($im, 200, 31, 31);
$tsize = ImageTTFBBox($fontsize,0,$font,$string);
$dx = abs($tsize[2]-$tsize[0]); // subtract lower right x from lower left x
$x = ( ImageSx($im) - $dx );
$y = ImageSy($im) - 5;
ImageTTFText($im, $fontsize, 0, $x, $y, $FontColor, $font, $string);
elseif ($EnableText && $ImageType == 12) :
// For two-line text-type watermark, lower center
$size = 250;
$width = imagesx($im);
$height = imagesy($im);
$aspect_ratio = $height/$width;
$new_w = $size;
$new_h = abs($new_w * $aspect_ratio);
$string1 = "presented by";
$font1 = $ServerPath ."/internals/truetype/font2.ttf";
$fontsize1 = 15;
$FontColor = ImageColorAllocate($im, 200, 31, 31);
$tsize1 = ImageTTFBBox($fontsize1,0,$font1,$string1);
$dx1 = abs($tsize1[2]-$tsize1[0]);
$x1 = ( ImageSx($im) - $dx1) / 2;
$y1 = $new_h - 50;
ImageTTFText($im, $fontsize1, 0, $x1, $y1, $FontColor, $font1, $string1);
$string2 = "SiteName.com";
$font2 = $ServerPath . "/internals/truetype/font1.ttf";
$fontsize2 = 30;
$FontColor = ImageColorAllocate($im, 200, 31, 31);
$tsize2 = ImageTTFBBox($fontsize2,0,$font2,$string2);
$dx2 = abs($tsize2[2]-$tsize2[0]);
$x2 = ( ImageSx($im) - $dx2) / 2;
$y2 = $new_h - 10;
ImageTTFText($im, $fontsize2, 0, $x2, $y2, $FontColor, $font2, $string2);
endif;
endif;
$picBG = "255, 255, 255"; // RGB-value for background
$picFG = "104, 104, 104"; // RGB-value for color levels
$colsBG = explode(",", $picBG);
$colsFG = explode(",", $picFG);
$imagebgcolor = ImageColorAllocate($im, trim($colsBG[0]), trim($colsBG[1]), trim($colsBG[2]));
$jpegQuality = 72;
// Start headers
Header ("Content-type: image/" . $mime_type);
if (isset($_GET['Download'])) :
$Replace = array("e","\"","/","(",")","@",":");
$ImageType = str_replace($Replace ,"", $mime_type);
Header("Content-Disposition: attachment; filename=" . $ImageName . "." . $ImageType);
endif;
// Add no-cache to headers
Header("Pragma: no-cache");
switch ($mime_type) :
case "gif":
ImageColorTransparent($im, $imagebgcolor);
ImageGIF($im);
break;
case "jpeg":
ImageJPEG($im, NULL, $jpegQuality);
break;
case "png":
ImageColorTransparent($im, $imagebgcolor);
ImagePNG($im);
break;
default:
ImageJPEG($im, NULL, $jpegQuality);
endswitch;
ImageDestroy($im);
echo $im;
elseif (isset($mime_type) && $mime_type !== "pdf" && $ImageType == 13) :
Header("Pragma: no-cache");
Header("Content-type: image/".$mime_type."\"");
Header("Content-Disposition: attachment; filename=$ImageName");
echo $im;
elseif (isset($mime_type) && $mime_type === "pdf") :
Header("Pragma: no-cache");
Header("Content-type: application/pdf");
Header("Content-Disposition: attachment; filename=$ImageName");
echo $im;
endif;
exit();
?>