php 实现图片转字符画图片

这篇博客介绍了如何使用PHP实现将图像转换为字符画的过程。通过创建一个名为`Drawing`的类,利用`imagecreatetruecolor`、`imagecolorallocate`和`imagestring`等函数,结合灰度值计算公式选取字符,最终输出字符画。示例代码中展示了读取图片、创建画布、设置背景和字体颜色,以及将图像内容写入字符画的步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

综合参考了两篇博客做的

https://blog.youkuaiyun.com/somehow1002/article/details/77600186

https://blog.youkuaiyun.com/qq_15096707/article/details/48918461

源代码如下:

class Drawing
{
    /**
     * @var:图像句柄
     */
    protected $im;

    /**
     * @var:字体颜色
     */
    protected $fontColor;

    /**
     * @var:背景颜色
     */
    protected $backColor;

    /**
     * @var:源图资源
     */
    protected $source_img;

    /**
     * 字符画输出到图片
     * @param $imgName
     */
    public function createImg($imgName)
    {
        $this->source_img = getimagesize($imgName);
        if (empty($this->source_img) || !isset($this->source_img[0]) || !isset($this->source_img[1])) {
            die('图片资源有误!');
        }
        //创建画布
        $this->im = imagecreatetruecolor($this->source_img[0], $this->source_img[1]);
        //设置画布背景颜色
        $this->backColor = imagecolorallocate($this->im, 255, 255, 255);
        imagefill($this->im, 0, 0, $this->backColor);
        //创建字体颜色
        $this->fontColor = imagecolorallocate($this->im, 0, 0, 0);
        //写入内容
        $this->output($imgName);
        //输出图像到网页
        header("content-type:image/png");
        imagepng($this->im);
        //销毁该图片
        imagedestory($this->im);
    }

    /**
     * 获得图像资源
     * @param $imgName
     * @return false|resource
     */
    public function getImg($imgName)
    {
        if ($this->source_img[2] == 1) {
            return imagecreatefromgif($imgName);
        } else if ($this->source_img[2] == 2) {
            return imagecreatefromjpeg($imgName);
        } else if ($this->source_img[2] == 3) {
            return imagecreatefrompng($imgName);
        } else {
            die("对不起,暂不支持该格式!");
        }
    }

    /**
     * 图像转字符画
     * @param $imgName :图像名称
     */
    public function output($imgName)
    {
        $im = $this->getImg($imgName);
        $str = '@80GCLft1i;:,. ';//填充字符
        //步长:选取每个像素块的代表点
        $stepx = 8;
        $stepy = 16;
        $x = imagesx($im);
        $y = imagesy($im);
        for ($j = 0; $j < $y; $j += $stepy) {
            $tmp = '';
            for ($i = 0; $i < $x; $i += $stepx) {
                //获取像素块的代表点RGB信息
                $colors = imagecolorsforindex($im, imagecolorat($im, $i, $j));
                //灰度值计算公式:Gray=R*0.3+G*0.59+B*0.11
                $greyness = (0.3 * $colors["red"] + 0.59 * $colors["green"] + 0.11 * $colors["blue"]) / 255;
                //根据灰度值选择合适的字符
                $offset = (int)ceil($greyness * (strlen($str) - 1));
                if ($offset == (strlen($str) - 1)) {
                    $tmp .= " ";
                } else {
                    $tmp .= $str[$offset];
                }
            }
            //使用imagestring写中文会出现乱码
            imagestring($this->im, 4, 0, $j, $tmp, $this->fontColor);
        }
        imagedestroy($im);
    }
}

调用:

$Drawing = new Drawing();
$Drawing->createImg('1.jpg');

效果图:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

JSON_L

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

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

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

打赏作者

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

抵扣说明:

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

余额充值