测试工程费用

回复列表(336|隐藏机器人聊天)
  • @Ta / 2023-10-17 / /

    @坏人,抱歉,看起来出现了一些问题,导致错误信息。错误信息表明 GD 库无法处理图像文件,因此在使用 imagecreatefromjpeg 之前,你需要确保你正在处理的文件是有效的 JPEG 文件。

    你可以添加一个额外的检查,以确保文件是有效的 JPEG 文件,然后再尝试处理它。以下是修改后的代码:

    <?php
    include("config_1.php");
    
    if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($zhanghao)) {
        $liebiao = 'shuiyinxiangji'; // 水印相机
    
        // 处理上传的文件
        // 处理其他表单字段
        // ...
    
        $fileToUpload = $_FILES['fileToUpload']['tmp_name'];
    
        // 检查文件是否是有效的 JPEG 文件
        $isJpeg = exif_imagetype($fileToUpload) === IMAGETYPE_JPEG;
    
        if ($isJpeg) {
            $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';
    
            // 保存旋转后的图片
            imagejpeg($image, $groundImage);
    
            // 释放内存
            if (isset($image)) {
                imagedestroy($image);
            }
    
            // 继续处理其他表单字段和添加水印
            // ...
            
            // 文件上传和水印添加成功
            echo '' . $groundImage . '?' . uniqid() . '=' . $zhanghao . '';
        } else {
            // 处理非JPEG文件的逻辑
            echo "需要加水印的图片不是有效的JPEG文件。";
        }
    }
    ?>
    

    这个代码添加了一个额外的检查来确保上传的文件是有效的 JPEG 文件。如果不是 JPEG 文件,它将输出一条相应的消息。如果是 JPEG 文件,它将继续处理并添加水印。这应该能够解决问题。

  • @Ta / 2023-10-17 / /
    @ChatGPT,是JPEG时正常处理图片!如果是PNG图片,根据文件类型扩展代码,添加对应的处理方式
  • @Ta / 2023-10-17 / /

    @坏人,如果你需要在处理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图像的处理部分,包括添加水印和其他所需的操作。这个代码片段提供了处理不同图像类型的框架,你可以根据具体要求进行定制。

  • @Ta / 2023-10-17 / /
    把完整代码出来!@ChatGPT
  • @Ta / 2023-10-17 / /

    @坏人,下面是整体代码,包括处理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图像。你可以根据需要进一步完善不同图像类型的处理逻辑和添加水印等操作。

  • @Ta / 2023-10-17 / /
    @ChatGPT,把    imagejpeg($image, $groundImage);
        $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);
        }修改后决进去
  • @Ta / 2023-10-17 / /

    @坏人,以下是修改后的完整代码,包括根据图像类型添加水印的处理:

    <?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
    
  • @Ta / 2023-10-17 / /
    @ChatGPT,把下半部分发出来
  • @Ta / 2023-10-17 / /

    @坏人,以下是代码的下半部分:

        // 阴影颜色
        $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)进行水印处理,并将结果保存。如果需要进一步修改或有其他问题,请随时告诉我。

  • @Ta / 2023-10-17 / /
    @ChatGPT,<br />
    <b>Fatal error</b>:  Uncaught Error: Call to undefined function mime_content_type() in /www/wwwroot/88.162118.cn/shuiyintupian.php:27
    Stack trace:
    #0 {main}
      thrown in <b>/www/wwwroot/88.162118.cn/shuiyintupian.php</b> on line <b>27</b><br />
  • @Ta / 2023-10-17 / /

    @坏人,这个错误是因为在使用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扩展,你可能需要联系你的主机提供商或系统管理员来启用或安装这个扩展。

  • @Ta / 2023-10-17 / /
    @ChatGPT,<br />
    <b>Fatal error</b>:  Uncaught Error: Call to undefined function finfo_file() in /www/wwwroot/88.162118.cn/shuiyintupian.php:27
    Stack trace:
    #0 {main}
      thrown in <b>/www/wwwroot/88.162118.cn/shuiyintupian.php</b> on line <b>27</b><br />
  • @Ta / 2023-10-17 / /

    @坏人,如果你的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()函数,都可以正常运行。

  • @Ta / 2023-10-17 / /
    @ChatGPT,<?php
    function bd09_to_gcj02($bd_lon, $bd_lat) {
        $x_pi = 3.14159265358979324 * 3000.0 / 180.0;
        $x = $bd_lon - 0.0065;
        $y = $bd_lat - 0.006;
        $z = sqrt($x * $x + $y * $y) - 0.00002 * sin($y * $x_pi);
        $theta = atan2($y, $x) - 0.000003 * cos($x * $x_pi);
        $gg_lon = $z * cos($theta);
        $gg_lat = $z * sin($theta);
        return array($gg_lon, $gg_lat);
    }

    // 用百度地图坐标来测试
    $bd_lon = 116.404;
    $bd_lat = 39.915;
    list($gcj02_lon, $gcj02_lat) = bd09_to_gcj02($bd_lon, $bd_lat);
    echo "高德坐标:", $gcj02_lon, " ", $gcj02_lat;
    ?>
    用坐标生成高德连接
  • @Ta / 2023-10-17 / /
    待审核
    发言待审核,仅管理员和作者本人可见。
  • @Ta / 2023-12-06 / /
    @ChatGPT,加入一个显示条件和不显示!用来做删除功能<?php
    session_start(); // 启动会话

    // 设置认证超时时间为一分钟
    $timeout = 59;

    // 检查是否存在 authenticated cookie
    if (!isset($_COOKIE['authenticated'])) {
        // 如果不存在,要求用户重新登录
        header("Location: login.php"); // 将 login.php 替换为您的认证页面
        exit();
    } else {
        // 如果存在 authenticated cookie,检查上一次访问时间
        if (isset($_SESSION['last_activity']) && (time() - $_SESSION['last_activity'] > $timeout)) {
            // 超过一分钟,要求用户重新登录
            setcookie('authenticated', '', time() - 3600); // 清除cookie
            header("Location: login.php"); // 将 login.php 替换为您的认证页面
            exit();
        }

    // 设置持久性cookie,保存用户认证状态,时长为一分钟
    setcookie('authenticated', 'true', time() + 60);
        // 更新最后一次访问时间
        $_SESSION['last_activity'] = time();

        // 其他页面逻辑...
    }
            include("config.php");
    echo '
        <form method="post" action="" enctype="multipart/form-data">
            <input type="text" name="user_name" placeholder="'.$_SESSION['username'].'" value="'.$_SESSION['username'].'" required><br>
            <textarea name="message" placeholder="输入消息"value="" required></textarea><br>
            <input type="file" name="image[]" accept="image/*" multiple><br>
            <input type="submit" name="submit" value="发送">
        </form>
        <ul class="time-line">';
            // 处理文件上传
            if (isset($_POST['submit'])) {
                // 获取用户输入的用户名和消息
                $user_name = $_POST['user_name'];
                $message = $_POST['message'];

                // 处理文件上传
                $imagePaths = [];
                if (!empty($_FILES['image']['name'][0])) {
                    $uploadDir = 'uploads/';
                    if (!file_exists($uploadDir)) {
                        mkdir($uploadDir, 0777, true);
                    }

                    // 循环处理每个上传的文件
                    for ($i = 0; $i < count($_FILES['image']['name']); $i++) {
                        $fileName = $_FILES['image']['name'][$i];
                        $fileTmpName = $_FILES['image']['tmp_name'][$i];
                        $newFileName = uniqid() . '_' . $fileName;
                        $uploadPath = $uploadDir . $newFileName;
                        if (move_uploaded_file($fileTmpName, $uploadPath)) {
                            $imagePaths[] = $newFileName;
                        }
                    }
                }

                // 插入消息和图片路径到数据库
                $imagePathsStr = implode(',', $imagePaths);
                $sql = "INSERT INTO messages (user_name, message, image_path) VALUES ('$user_name', '$message', '$imagePathsStr')";
                $conn->query($sql);
            }

            // 设置每页显示的消息数量
            $messagesPerPage = 10;
            // 获取当前页码
            if (isset($_GET['page'])) {
                $page = intval($_GET['page']);
            } else {
                $page = 1;
            }
            // 计算查询的偏移量
            $offset = ($page - 1) * $messagesPerPage;

            // 修改查询语句,按时间降序排列
            $sql = "SELECT * FROM messages ORDER BY created_at DESC LIMIT $offset, $messagesPerPage";

            $result = $conn->query($sql);
            $currentDay = '';
            while ($row = $result->fetch_assoc()) {
                $messageDate = date('Y年m月d日', strtotime($row['created_at']));
                $messageTime = date('H:i', strtotime($row['created_at']));
                if ($messageDate !== $currentDay) {
                    echo "<li class='tl-header'>";
                    echo "<h2>$messageDate</h2>";
                    echo "</li>";
                    $currentDay = $messageDate;
                }
                echo "<ul class='tl-body'>";
                echo "<li>";
                echo "<span>$messageTime</span>";
                echo "<h3>" . htmlspecialchars($row['user_name']) . ": " . htmlspecialchars($row['message']) . "</h3>";
                // 显示图片,如果有的话
                if (!empty($row['image_path'])) {
                    // 因为支持多个文件上传,所以需要循环显示图片
                    $imagePaths = explode(',', $row['image_path']);
                    foreach ($imagePaths as $imagePath) {
                        echo "<br/><img src='uploads/" . $imagePath . "' alt='图片' width='50'>";
                    }
                }
                echo "</li>";
                echo "</ul>";
            }
            echo('</ul>

            <div class="pagination">'); // 显示分页导航
    echo('</ul><div class="pagination">'); // 显示分页导航
    $sql = "SELECT COUNT(*) AS total FROM messages";
    $totalResult = $conn->query($sql);
    $totalRow = $totalResult->fetch_assoc();
    $totalMessages = $totalRow['total'];
    $totalPages = ceil($totalMessages / $messagesPerPage);

    for ($i = 1; $i <= $totalPages; $i++) {
        if ($i === $page) {
            echo "<span class='active'>$i</span>";
        } else {
            echo "<a href='?page=$i'>$i</a>";
        }
    }

    echo '</div></body></html>';
            $conn->close();
    ?> 

  • @Ta / 2023-12-12 / /
    @ChatGPT,加入一个显示条件和不显示!用来做删除功能<?php
    session_start(); // 启动会话

    // 设置认证超时时间为一分钟
    $timeout = 59;

    // 检查是否存在 authenticated cookie
    if (!isset($_COOKIE['authenticated'])) {
        // 如果不存在,要求用户重新登录
        header("Location: login.php"); // 将 login.php 替换为您的认证页面
        exit();
    } else {
        // 如果存在 authenticated cookie,检查上一次访问时间
        if (isset($_SESSION['last_activity']) && (time() - $_SESSION['last_activity'] > $timeout)) {
            // 超过一分钟,要求用户重新登录
            setcookie('authenticated', '', time() - 3600); // 清除cookie
            header("Location: login.php"); // 将 login.php 替换为您的认证页面
            exit();
        }

    // 设置持久性cookie,保存用户认证状态,时长为一分钟
    setcookie('authenticated', 'true', time() + 60);
        // 更新最后一次访问时间
        $_SESSION['last_activity'] = time();

        // 其他页面逻辑...
    }
            include("config.php");
    echo '
        <form method="post" action="" enctype="multipart/form-data">
            <input type="text" name="user_name" placeholder="'.$_SESSION['username'].'" value="'.$_SESSION['username'].'" required><br>
            <textarea name="message" placeholder="输入消息"value="" required></textarea><br>
            <input type="file" name="image[]" accept="image/*" multiple><br>
            <input type="submit" name="submit" value="发送">
        </form>
        <ul class="time-line">';
            // 处理文件上传
            if (isset($_POST['submit'])) {
                // 获取用户输入的用户名和消息
                $user_name = $_POST['user_name'];
                $message = $_POST['message'];

                // 处理文件上传
                $imagePaths = [];
                if (!empty($_FILES['image']['name'][0])) {
                    $uploadDir = 'uploads/';
                    if (!file_exists($uploadDir)) {
                        mkdir($uploadDir, 0777, true);
                    }

                    // 循环处理每个上传的文件
                    for ($i = 0; $i < count($_FILES['image']['name']); $i++) {
                        $fileName = $_FILES['image']['name'][$i];
                        $fileTmpName = $_FILES['image']['tmp_name'][$i];
                        $newFileName = uniqid() . '_' . $fileName;
                        $uploadPath = $uploadDir . $newFileName;
                        if (move_uploaded_file($fileTmpName, $uploadPath)) {
                            $imagePaths[] = $newFileName;
                        }
                    }
                }

                // 插入消息和图片路径到数据库
                $imagePathsStr = implode(',', $imagePaths);
                $sql = "INSERT INTO messages (user_name, message, image_path) VALUES ('$user_name', '$message', '$imagePathsStr')";
                $conn->query($sql);
            }

            // 设置每页显示的消息数量
            $messagesPerPage = 10;
            // 获取当前页码
            if (isset($_GET['page'])) {
                $page = intval($_GET['page']);
            } else {
                $page = 1;
            }
            // 计算查询的偏移量
            $offset = ($page - 1) * $messagesPerPage;

            // 修改查询语句,按时间降序排列
            $sql = "SELECT * FROM messages ORDER BY created_at DESC LIMIT $offset, $messagesPerPage";

            $result = $conn->query($sql);
            $currentDay = '';
            while ($row = $result->fetch_assoc()) {
                $messageDate = date('Y年m月d日', strtotime($row['created_at']));
                $messageTime = date('H:i', strtotime($row['created_at']));
                if ($messageDate !== $currentDay) {
                    echo "<li class='tl-header'>";
                    echo "<h2>$messageDate</h2>";
                    echo "</li>";
                    $currentDay = $messageDate;
                }
                echo "<ul class='tl-body'>";
                echo "<li>";
                echo "<span>$messageTime</span>";
                echo "<h3>" . htmlspecialchars($row['user_name']) . ": " . htmlspecialchars($row['message']) . "</h3>";
                // 显示图片,如果有的话
                if (!empty($row['image_path'])) {
                    // 因为支持多个文件上传,所以需要循环显示图片
                    $imagePaths = explode(',', $row['image_path']);
                    foreach ($imagePaths as $imagePath) {
                        echo "<br/><img src='uploads/" . $imagePath . "' alt='图片' width='50'>";
                    }
                }
                echo "</li>";
                echo "</ul>";
            }
            echo('</ul>

            <div class="pagination">'); // 显示分页导航
    echo('</ul><div class="pagination">'); // 显示分页导航
    $sql = "SELECT COUNT(*) AS total FROM messages";
    $totalResult = $conn->query($sql);
    $totalRow = $totalResult->fetch_assoc();
    $totalMessages = $totalRow['total'];
    $totalPages = ceil($totalMessages / $messagesPerPage);

    for ($i = 1; $i <= $totalPages; $i++) {
        if ($i === $page) {
            echo "<span class='active'>$i</span>";
        } else {
            echo "<a href='?page=$i'>$i</a>";
        }
    }

    echo '</div></body></html>';
            $conn->close();
    ?> 
添加新回复
回复需要登录