php 简单的学习GD库绘制图片并传回给前端实现方式

1、基本的GD库绘制图片汇总

2、后台实现小案例

<?php

// $img = imagecreatetruecolor(200,40);
// var_dump($img);
// 利用GD库来绘制图片

// 1、创建一个背景图片  
$img = ImageCreateFromJpeg('./images/timg.jpg');

$white = imagecolorallocate($img,0,0,0);
//2、绘制了矩形的轮廓
// imagerectangle($img, 10, 10, 30, 30, $white);

//3、填写文字
imagettftext($img, 50, 0, 480, 520, $white, './public/STHUPO.TTF', '大家好,这是我的网站!欢迎光临!');

// The End,生成图片
header('Content-type:image/png');
ImagePng($img);
ImageDestroy($img);

3、前端接收方法

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .box {
            width: 100px;
            height: 100px;
            background: red;
        }
    </style>
</head>

<body>
    <div class='box' onclick="getPic()">生成图片</div>
    <img src="" alt="" class="img">
</body>

</html>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
    function getPic() {
        $('.img').attr('src', 'gd.php');
    }
</script>

小结:前端接收的方式可以是直接把图片的src的路径写成我们后台生成图片的接口地址即可!

原文地址:https://www.cnblogs.com/teamemory/p/9412454.html