这实际上更多的是一个答案,而不是一个问题,但是,当然,评论或其他解决方案是受欢迎的,只要它们不涉及将图像保存到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();
?>
暂无答案!
目前还没有任何答案,快来回答吧!