PHP网页截图工具

输入网页地址,选择截图选项,即可生成网页截图。支持自定义截图尺寸和格式。

使用说明: 输入完整的网页地址(包含http://或https://),选择截图选项,然后点击"生成截图"按钮。

正在生成截图,请稍候...

截图生成成功!

网页截图
4000) { sendJsonResponse(false, '宽度必须在100到4000像素之间'); exit; } if ($height < 100 || $height > 4000) { sendJsonResponse(false, '高度必须在100到4000像素之间'); exit; } // 生成截图 try { $screenshotPath = generateScreenshot($url, $width, $height, $format); sendJsonResponse(true, '截图生成成功', $screenshotPath); } catch (Exception $e) { sendJsonResponse(false, '生成截图时出错: ' . $e->getMessage()); } exit; } function generateScreenshot($url, $width, $height, $format) { // 创建截图保存目录 $screenshotsDir = 'screenshots'; if (!is_dir($screenshotsDir)) { mkdir($screenshotsDir, 0755, true); } // 生成唯一文件名 $filename = 'screenshot_' . md5($url . time()) . '.' . $format; $filepath = $screenshotsDir . '/' . $filename; // 在实际应用中,这里需要使用无头浏览器(如Headless Chrome)或第三方服务 // 由于环境限制,这里我们模拟生成一个占位图片 // 创建示例图片(实际应用中应替换为真实的截图生成代码) $image = imagecreate($width, $height); // 设置背景色 $backgroundColor = imagecolorallocate($image, 240, 240, 240); imagefill($image, 0, 0, $backgroundColor); // 添加文本 $textColor = imagecolorallocate($image, 100, 100, 100); $text = "网页截图: " . $url; $fontSize = 5; // 内置字体大小 $textWidth = imagefontwidth($fontSize) * strlen($text); $textX = ($width - $textWidth) / 2; $textY = $height / 2; imagestring($image, $fontSize, $textX, $textY, $text, $textColor); // 添加边框 $borderColor = imagecolorallocate($image, 200, 200, 200); imagerectangle($image, 0, 0, $width-1, $height-1, $borderColor); // 保存图片 if ($format === 'jpg' || $format === 'jpeg') { imagejpeg($image, $filepath, 90); } else { imagepng($image, $filepath); } imagedestroy($image); // 在实际应用中,您可能需要使用以下方法之一: // 1. 使用Headless Chrome和puppeteer // 2. 使用第三方API服务 // 3. 使用wkhtmltoimage工具 return $filepath; } function sendJsonResponse($success, $message, $imagePath = '') { header('Content-Type: application/json'); echo json_encode([ 'success' => $success, 'message' => $message, 'image_path' => $imagePath ]); } ?>