Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

php gd库

作者:无声飞雪   发布日期:2025-04-09   浏览:110

<?php
// 创建一个空白图像并填充颜色
$im = imagecreatetruecolor(100, 100);
$white = imagecolorallocate($im, 255, 255, 255);
imagefill($im, 0, 0, $white);

// 创建一个红色的矩形
$red = imagecolorallocate($im, 255, 0, 0);
imagefilledrectangle($im, 40, 40, 60, 60, $red);

// 输出图像到浏览器
header('Content-Type: image/png');
imagepng($im);

// 释放内存
imagedestroy($im);
?>

解释说明:

  1. 创建图像imagecreatetruecolor(100, 100) 创建一个 100x100 像素的空白图像。
  2. 分配颜色imagecolorallocate($im, 255, 255, 255) 分配白色(RGB: 255, 255, 255)给图像,并使用 imagefill 将整个图像填充为白色。
  3. 绘制矩形imagefilledrectangle($im, 40, 40, 60, 60, $red) 在图像中绘制一个红色的矩形,位置从 (40, 40) 到 (60, 60)。
  4. 输出图像header('Content-Type: image/png') 设置响应头为 PNG 图像格式,imagepng($im) 将图像输出到浏览器。
  5. 释放内存imagedestroy($im) 销毁图像资源,释放内存。

这个示例展示了如何使用 PHP 的 GD 库创建和操作图像。

上一篇:php 图片压缩

下一篇:php parse_str

大家都在看

php session用法

phpisset函数

php后端

php爬虫框架

php读取csv文件

php 三元表达式

php文件加密

php 拆分字符串

php pcntl

php ||

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站