php源码 文字生成128*32像素的单色bmp图片并取模 可以输入文字、调节字体、字体大小 位置可以调节靠左 居中(水平和垂直都居中) 靠右。生存预览,点放大可以多倍放大预览,保存为单色bmp图片

需求

php 文字生成128*32像素的单色bmp图片 可以输入文字,可以调节字体、字体大小 位置可以调节靠左 居中(水平和垂直都居中) 靠右。生存预览,点放大可以2倍放大预览,保存为单色bmp图片。

最终效果【源码下载】

在这里插入图片描述

步骤

1.文字生成png图片
2.png图片转为单色1位bmp
3.对bmp取模 输出字节数组 :可以按各种扫描顺序 从上到下 从左在右 高位在前 反向等(设置我隐藏了)

确认PHP环境是否已经配置了GD库:

  1. 创建一个名为phpinfo.php的文件,并在其中写入以下代码:
<?php
phpinfo();
?>
  1. 将该文件上传到你的服务器上,并通过浏览器访问它。
  2. 在打开的页面中搜索“gd”。如果找到了与GD库相关的信息,那么就说明你的PHP环境已经配置了GD库。

如果你没有找到与GD库相关的信息,那么你可能需要安装并启用GD库。在大多数情况下,PHP默认安装是已经启用了GD库的。
在这里插入图片描述

工程下载

代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Image Generator</title>
    <style>
        #imagePreview {
            width: 128px;
            height: 32px;
            image-rendering: pixelated;
        }
    </style>
</head>
<body>
    <input type="text" id="textInput" placeholder="Enter text" value="小黄人">
    <select id="fontSelect">
        <option value="simsun.ttc">SimSun</option>
        <option value="hwxkjc.ttf">HWXKJC</option>
        <option value="STLITI.TTF">STLITI.TTF</option>
        <option value="arial.ttf">arial.ttf</option>
        <!-- Add more font options here -->
    </select>
    <input type="number" id="fontSizeInput" placeholder="Font Size" value="12">
    <select id="textAlign">
        <option value="left">Left</option>
        <option value="center">Center</option>
        <option value="right">Right</option>
    </select>
    <button onclick="generateImage()">Generate Image</button>
    <button onclick="zoomIn()">Zoom In</button>
    <button onclick="dotMatrix()">Dot Matrix</button>
    <div id="imageContainer">
        <img id="imagePreview" src="output.bmp?date=${new Date().getTime()}" alt="Preview Image">
    </div>
    <pre id="modResult"></pre>


    <form id="uploadForm" enctype="multipart/form-data" style="display:none;"">
        <input type="text" id="filePath" placeholder="Enter file path" value="output.bmp">
        <br>
		<input type="checkbox" name="horizontal"> 横向取模(不勾选为纵向)
        <br>
		<input type="checkbox" name="secondByteOnRight" checked> 第二字节在右(不勾选为第二字节在在下)
        <br>
		<input type="checkbox" name="topToBottom" checked> 从上到下(无效)
        <input type="checkbox" name="leftToRight" checked> 从左到右(无效)
        <br>
        <input type="checkbox" name="highBitFirst" checked> 高位在前(即大端模式)
        <input type="checkbox" name="pixelReverse" checked> 像素反转(即黑白颠倒)
        <br>
        <button type="button" οnclick="submitForm()">生成取模</button>
    </form>
    <script>
        function generateImage() {
            const text = document.getElementById('textInput').value;
            const font = document.getElementById('fontSelect').value;
            const fontSize = document.getElementById('fontSizeInput').value;
            const textAlign = document.getElementById('textAlign').value;


            document.getElementById('imagePreview').src = `generate_image.php?text=${text}&font=${font}&fontSize=${fontSize}&textAlign=${textAlign}`;
            // // Send a request to generate_image.php with provided parameters
			// fetch(`generate_image.php?text=${text}&font=${font}&fontSize=${fontSize}&textAlign=${textAlign}`)
			// .then(response => {
			// 	// Handle response here
			// })
			// .catch(error => {
			// 	console.error('Error:', error);
			// });
			// 	// Call PHP function to generate BMP image with provided parameters
			// 	// Display the generated image on the page
			//  document.getElementById('imagePreview').src = `output.bmp?date=${new Date().getTime()}`;

	  
			// const image = document.getElementById('imagePreview');
			// const imageUrl = image.src;
			// image.src = '';

			// setTimeout(() => {
			// 	image.src = imageUrl;
			// }, 10);
        }

        function zoomIn() {
            const image = document.getElementById('imagePreview');
            const currentWidth = image.width;
            const currentHeight = image.height;
            image.style.width = `${currentWidth * 2}px`;
            image.style.height = `${currentHeight * 2}px`;
        }
        
        function dotMatrix() {
            var formData = new FormData(document.getElementById('uploadForm'));
            fetch('process_image.php', {
                method: 'POST',
                body: formData,
            })
            .then(response => response.json())
            .then(data => {
                document.getElementById('imagePreview').src = data.imageUrl+`?date=${new Date().getTime()}`;
                //document.getElementById('imagePreview').style.display = 'block';
                document.getElementById('modResult').textContent = data.modData;
            });
        }
    </script>
</body>
</html>

效果:失败

在这里插入图片描述

  • 25
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小黄人软件

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值