测试工程费用
@坏人,如果你需要在处理JPEG和PNG图像时使用不同的处理方式,你可以通过检查文件类型(MIME 类型)来确定它们,并相应地进行处理。下面是修改后的代码,用于处理JPEG和PNG图像:
<?php
include("config_1.php");
if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($zhanghao)) {
$liebiao = 'shuiyinxiangji'; // 水印相机
// 处理上传的文件
// 处理其他表单字段
// ...
$fileToUpload = $_FILES['fileToUpload']['tmp_name'];
// 获取文件的 MIME 类型
$fileMimeType = mime_content_type($fileToUpload);
if ($fileMimeType === 'image/jpeg') {
// JPEG 图像处理
$image = imagecreatefromjpeg($fileToUpload);
// 获取图片的旋转角度信息,如果有EXIF信息
$exif = @exif_read_data($fileToUpload);
$orientation = isset($exif['Orientation']) ? $exif['Orientation'] : 1;
if ($orientation === 6) {
$image = imagerotate($image, -90, 0);
} elseif ($orientation === 8) {
$image = imagerotate($image, 90, 0);
} elseif ($orientation === 3) {
$image = imagerotate($image, 180, 0);
}
// 生成唯一的文件名,以避免重复文件名覆盖问题
$groundImage = $uploadDir . uniqid() . '.jpg';
// 保存旋转后的 JPEG 图像
imagejpeg($image, $groundImage);
// 释放内存
if (isset($image)) {
imagedestroy($image);
}
// 继续处理其他表单字段和添加水印
// ...
// 文件上传和水印添加成功
echo '' . $groundImage . '?' . uniqid() . '=' . $zhanghao . '';
} elseif ($fileMimeType === 'image/png') {
// PNG 图像处理
// 在这里添加处理PNG图像的代码
// 例如:imagecreatefrompng 和 imagepng
// 继续处理其他表单字段和添加水印
// ...
// 文件上传和水印添加成功
// 输出PNG图像的路径
echo 'Path_to_png_image'; // 请替换为实际的PNG图像路径
} else {
// 处理其他图像类型或不支持的类型
echo "不支持的图像类型。";
}
}
?>
这段代码首先检查文件的 MIME 类型,如果是JPEG,则按照之前的方式处理。如果是PNG,则可以在 elseif
部分添加处理PNG图像的代码,包括使用 imagecreatefrompng
创建图像和使用 imagepng
保存图像。
请确保根据你的需求进一步完善PNG图像的处理部分,包括添加水印和其他所需的操作。这个代码片段提供了处理不同图像类型的框架,你可以根据具体要求进行定制。
@坏人,下面是整体代码,包括处理JPEG和PNG图像的部分:
<?php
include("config_1.php");
if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($zhanghao)) {
$liebiao = 'shuiyinxiangji'; // 水印相机
// 处理上传的文件
// 处理其他表单字段
// ...
$fileToUpload = $_FILES['fileToUpload']['tmp_name'];
// 获取文件的 MIME 类型
$fileMimeType = mime_content_type($fileToUpload);
if ($fileMimeType === 'image/jpeg') {
// JPEG 图像处理
$image = imagecreatefromjpeg($fileToUpload);
// 获取图片的旋转角度信息,如果有EXIF信息
$exif = @exif_read_data($fileToUpload);
$orientation = isset($exif['Orientation']) ? $exif['Orientation'] : 1;
if ($orientation === 6) {
$image = imagerotate($image, -90, 0);
} elseif ($orientation === 8) {
$image = imagerotate($image, 90, 0);
} elseif ($orientation === 3) {
$image = imagerotate($image, 180, 0);
}
// 生成唯一的文件名,以避免重复文件名覆盖问题
$groundImage = $uploadDir . uniqid() . '.jpg';
// 保存旋转后的 JPEG 图像
imagejpeg($image, $groundImage);
// 释放内存
if (isset($image)) {
imagedestroy($image);
}
// 继续处理其他表单字段和添加水印
// ...
// 文件上传和水印添加成功
echo '' . $groundImage . '?' . uniqid() . '=' . $zhanghao . '';
} elseif ($fileMimeType === 'image/png') {
// PNG 图像处理
$image = imagecreatefrompng($fileToUpload);
// 生成唯一的文件名,以避免重复文件名覆盖问题
$groundImage = $uploadDir . uniqid() . '.png';
// 保存 PNG 图像
imagepng($image, $groundImage);
// 释放内存
if (isset($image)) {
imagedestroy($image);
}
// 继续处理其他表单字段和添加水印
// ...
// 文件上传和水印添加成功
echo '' . $groundImage . '?' . uniqid() . '=' . $zhanghao . '';
} else {
// 处理其他图像类型或不支持的类型
echo "不支持的图像类型。";
}
}
?>
这是完整代码,包括处理JPEG和PNG图像的部分。根据文件的 MIME 类型,它会分别处理JPEG和PNG图像。你可以根据需要进一步完善不同图像类型的处理逻辑和添加水印等操作。
@坏人,以下是修改后的完整代码,包括根据图像类型添加水印的处理:
<?php
include("config_1.php");
if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($zhanghao)) {
$liebiao = 'shuiyinxiangji'; // 水印相机
// 处理上传的文件
// 处理其他表单字段
// ...
$fileToUpload = $_FILES['fileToUpload']['tmp_name'];
// 获取文件的 MIME 类型
$fileMimeType = mime_content_type($fileToUpload);
if ($fileMimeType === 'image/jpeg') {
// JPEG 图像处理
$image = imagecreatefromjpeg($fileToUpload);
// 获取图片的旋转角度信息,如果有EXIF信息
$exif = @exif_read_data($fileToUpload);
$orientation = isset($exif['Orientation']) ? $exif['Orientation'] : 1;
if ($orientation === 6) {
$image = imagerotate($image, -90, 0);
} elseif ($orientation === 8) {
$image = imagerotate($image, 90, 0);
} elseif ($orientation === 3) {
$image = imagerotate($image, 180, 0);
}
// 生成唯一的文件名,以避免重复文件名覆盖问题
$groundImage = $uploadDir . uniqid() . '.jpg';
// 保存旋转后的 JPEG 图像
imagejpeg($image, $groundImage);
// 释放内存
if (isset($image)) {
imagedestroy($image);
}
} elseif ($fileMimeType === 'image/png') {
// PNG 图像处理
$image = imagecreatefrompng($fileToUpload);
// 生成唯一的文件名,以避免重复文件名覆盖问题
$groundImage = $uploadDir . uniqid() . '.png';
// 保存 PNG 图像
imagepng($image, $groundImage);
// 释放内存
if (isset($image)) {
imagedestroy($image);
}
} else {
// 处理其他图像类型或不支持的类型
echo "不支持的图像类型。";
exit;
}
// 继续处理其他表单字段和添加水印
// ...
$textFont = "10"; //文字大小
$textFont2 = "6"; //文字大小
// 准备要插入的数据
// 插入数据的SQL语句
$sql = "INSERT INTO $liebiao (daodi_xiangmuid, project, comments, latitude, longitude, locationDescription, groundImage, zhanghao, zhengchangzhuangtai) VALUES ('$daodi_xiangmuid', '$project', '$comments', $latitude, $longitude, '$locationDescription', '$groundImage', '$zhanghao', $zhengchangzhuangtai)";
// 执行插入操作
if (mysqli_query($con, $sql)) {
//echo "数据插入成功";
} else {
echo "数据插入失败: " . mysqli_error($con);
}
// 关闭数据库连接
mysqli_close($con);
if (!file_exists($uploadDir)) { //用工程id创造工程相机图片
mkdir($uploadDir, 0777);
}
// 打开原图像
if (!empty($groundImage) && file_exists($groundImage)) {
$ground_info = getimagesize($groundImage);
$ground_w = $ground_info[0]; // 取得背景图片的宽
$ground_h = $ground_info[1]; // 取得背景图片的高
switch ($ground_info[2]) // 取得背景图片的格式
{
case 1:
$image = imagecreatefromgif($groundImage);
break;
case 2:
$image = imagecreatefromjpeg($groundImage);
break;
case 3:
$image = imagecreatefrompng($groundImage);
break;
default:
die($formatMsg);
}
} else {
die("需要加水印的图片不存在!.".$groundImage."");
}
// 定义水印文字颜色和字体
$textColor = imagecolorallocate($image, 255, 255, 255);
$font = 'ttff.ttf'; // 使用自己的字体文件路径
// 计算文字大小
$textFontSize = min($ground_w, $ground_h) / 30; // 可根据需要调整比例
// 计算文字大小
$textFontSize2 = min($ground_w, $ground_h) / 45; // 可根据需要调整比例
// 第一行文字
$line1Text = '' . $project;
$line1BackgroundColor = imagecolorallocatealpha($image, 0, 153, 255, 90); // 天空蓝色
$line1FontSize = $textFontSize + 10; // 可根据需要调整字体大小
// 第二行文字(系统时间)
$line2Text = '时间:' . date('Y-m-d H:i:s'); // 默认系统时间
$line2ShadowColor = imagecolorallocate($image, 0, 0, 0); // 阴影颜色
$line2FontSize = $textFontSize2 + 6; // 可根据需要调整字体大小
// 第三行文字(经纬度)
$line3Text = '经纬度: ' . $longitude . ', ' . $latitude . '';
$line3ShadowColor = imagecolorallocate($image, 0, 0, 0); // 阴影颜色
$line3FontSize = $textFontSize2 + 6; // 可根据需要调整字体大小
// 第四行文字(位置名称)
$line4Text = '位置:' . $locationDescription;
$line4ShadowColor = imagecolorallocate($image, 0, 0, 0); // 阴影颜色
$line4FontSize = $textFontSize2 + 5; // 可根据需要调整字体大小
// 第五行文字(备注)
$line5Text = '备注:' . $comments;
$line5ShadowColor = imagecolorallocate($image, 0, 0, 0
// 阴影颜色
$line5FontSize = $textFontSize2 + 6; // 可根据需要调整字体大小
// 计算每一行文字的垂直位置
$lineHeight = $line1FontSize + 5; // 设置行高,可根据需要调整
$line1Y = $ground_h - ($lineHeight * 5); // 第一行文字的Y坐标
$line2Y = $line1Y + $lineHeight; // 第二行文字的Y坐标
$line3Y = $line2Y + $lineHeight; // 第三行文字的Y坐标
$line4Y = $line3Y + $lineHeight; // 第四行文字的Y坐标
$line5Y = $line4Y + $lineHeight; // 第五行文字的Y坐标
// 计算X坐标
$line1TextWidth = imagettfbbox($line1FontSize, 0, $font, $line1Text);
$line1TextWidth = $line1TextWidth[2] - $line1TextWidth[0];
// 如果行文字不够十个字符,使用十个字符的长度
if (strlen($line1Text) < 16) {
$line1TextWidth = imagettfbbox($line1FontSize, 0, $font, str_repeat('X', 17));
$line1TextWidth = $line1TextWidth[2] - $line1TextWidth[0];
}
$line1X = $ground_w - $line1TextWidth - 20 - ($textFontSize * 3); // 第一行文字的X坐标
// 启用混合模式,实现透明效果
imagealphablending($image, true);
imagefilledrectangle($image, $line1X - 5, $line1Y - $line1FontSize - 5, $ground_w + 5, $line1Y + 5, $line1BackgroundColor);
// 添加第一行文字水印
imagettftext($image, $line1FontSize, 0, $line1X, $line1Y, $textColor, $font, $line1Text);
// 添加第二行文字水印
imagettftext($image, $line2FontSize, 0, $line1X, $line2Y, $line2ShadowColor, $font, $line2Text);
imagettftext($image, $line2FontSize, 0, $line1X + 2, $line2Y + 2, $textColor, $font, $line2Text);
// 添加其他行文字水印,依次类推
imagettftext($image, $line3FontSize, 0, $line1X, $line3Y, $line3ShadowColor, $font, $line3Text);
imagettftext($image, $line3FontSize, 0, $line1X + 2, $line3Y + 2, $textColor, $font, $line3Text);
imagettftext($image, $line4FontSize, 0, $line1X, $line4Y, $line4ShadowColor, $font, $line4Text);
imagettftext($image, $line4FontSize, 0, $line1X + 2, $line4Y + 2, $textColor, $font, $line4Text);
imagettftext($image, $line5FontSize, 0, $line1X, $line5Y, $line5ShadowColor, $font, $line5Text);
imagettftext($image, $line5FontSize, 0, $line1X + 2, $line5Y + 2, $textColor, $font, $line5Text);
// 保存处理后的图像,覆盖原图像
if ($image === false) {
die('Unable to create image from file.');
}
if (!imagejpeg($image, $groundImage)) {
die('Unable to save processed image.');
}
// 生成水印后的图片
@unlink($groundImage);
switch ($ground_info[2]) // 取得背景图片的格式
{
case 1:
imagegif($image, $groundImage);
break;
case 2:
imagejpeg($image, $groundImage);
break;
case 3:
imagepng($image, $groundImage);
break;
default:
die($errorMsg);
}
// 释放内存
if (isset($image)) {
imagedestroy($image);
}
}
// 文件上传和水印添加成功
echo '' . $groundImage . '?' . uniqid() . '=' . $zhanghao . '';
?>
这是完整的代码,它能够根据上传的图像类型(JPEG 或 PNG)进行水印处理,并将结果保存。如果需要进一步修改或有其他问题,请随时告诉我。
@坏人,这个错误是因为在使用mime_content_type()
函数时出现问题,它是PHP的一个内置函数,用于获取文件的MIME类型。在一些PHP环境中,这个函数可能未启用或不可用。
你可以尝试使用fileinfo
扩展来替代mime_content_type()
函数,fileinfo
扩展可以提供类似的功能。首先,确保fileinfo
扩展已经在你的PHP环境中启用。然后,你可以使用以下代码来获取文件的MIME类型:
$mime = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $fileToUpload);
在你的代码中,将以下行:
$exif = exif_read_data($fileToUpload);
替换为:
$mime = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $fileToUpload);
if (strpos($mime, 'image/jpeg') !== false) {
$exif = exif_read_data($fileToUpload);
} else {
$orientation = 1; // 默认为正常方向
}
这将根据文件的MIME类型来确定是否使用EXIF数据来旋转图片。
如果你的PHP环境不支持fileinfo
扩展,你可能需要联系你的主机提供商或系统管理员来启用或安装这个扩展。
@坏人,如果你的PHP环境不支持finfo_file()
函数,你可以尝试使用getimagesize()
函数来判断文件的MIME类型。下面是如何修改你的代码:
if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($zhanghao)) {
$liebiao = 'shuiyinxiangji';
// 处理上传的文件
$fileToUpload = $_FILES['fileToUpload']['tmp_name'];
$imageInfo = getimagesize($fileToUpload);
// 获取文件的MIME类型
$mime = $imageInfo['mime'];
if (strpos($mime, 'image/jpeg') !== false) {
// 处理JPEG图片,使用EXIF数据
$exif = exif_read_data($fileToUpload);
} else {
$orientation = 1; // 默认为正常方向
}
// 其余代码不变
// ...
}
这段代码使用getimagesize()
函数获取文件的MIME类型,然后根据MIME类型来决定是否使用EXIF数据来旋转图片。这样,无论PHP环境是否支持finfo_file()
函数,都可以正常运行。
@坏人,抱歉,看起来出现了一些问题,导致错误信息。错误信息表明 GD 库无法处理图像文件,因此在使用
imagecreatefromjpeg
之前,你需要确保你正在处理的文件是有效的 JPEG 文件。你可以添加一个额外的检查,以确保文件是有效的 JPEG 文件,然后再尝试处理它。以下是修改后的代码:
这个代码添加了一个额外的检查来确保上传的文件是有效的 JPEG 文件。如果不是 JPEG 文件,它将输出一条相应的消息。如果是 JPEG 文件,它将继续处理并添加水印。这应该能够解决问题。