
ob_clean() //清除输出缓存
header("Content-type:image/jpeg") //设置输出类型
$img="images/test.jpg" //背景图片名
if(isset($_GET["img"]))$img=$_GET["img"] //也可以通过img参数传入
$im=imagecreatefromjpeg($img) //读入背景图片
$text="文字内容" //要加上的文字内容
if(isset($_GET["text"]))$text=$_GET["text"] //也可以通过text参数传入
$fontFile="xxx.ttf" //字体文件名,必须要
$fontSize=36 //字体尺寸
$fontColor=ImageColorAllocate($im,0,0,0) //字体颜色,这里是黑色
$textAngle=0 //文字显示的角度,0表示水平显示
$textLeft=20 //文字显示的x坐标
$textTop=60 //文字显示的y坐标
imagefttext($im,$fontSize,$textAngle,$textLeft,$textTop,$fontColor,$fontFile,$text) //把文字覆盖到图片上
Imagejpeg($im) //输出图片
ImageDestroy($im) //销毁图片
?>
把以上文字保存为php文件,比如 img.php
然后在需要调用图片的地方用 <img src="img.php?img=背景图片文件路径&text=要加上的文字"/>来调用
比如 <img src="img.php?img=images/back.jpg&text=你好"/>
1、假设 phplot_img.php 即生成饼状图的程序,直接可以这样子调用:<img src="phplot_img.php" />
2、在打开页面能看到,就要通过 header 函数发送头信息,告知浏览器要显示的是图片,同时输出图片内容,那么就可以看到了:
$p = new PHPlot(800, 400)
//
$p->DrawGraph()
// 该 DrawGraph() 里调用了 PrintImage() 方法
// 在 PrintImage() 方法里,有这句代码,Header("Content-type: $mime_type")
// 就是告知浏览器接收到的数据是一张图片.
使用以下的代码就可以了
<html><head>
<meta http-equiv="Content-Type" content="text/html charset=utf-8" />
<title>上传demo</title>
</head>
<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />"
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />"
echo "Type: " . $_FILES["file"]["type"] . "<br />"
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. "
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"])
echo "Stored in: " . "upload/" . $_FILES["file"]["name"]
}
}
}
else
{
echo "Invalid file"
}
?>
<body>
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
<label> 上传图片<br />
<input type="file" name="file" />
</label>
<p>
<label>
<input type="submit" name="Submit" value="提交" />
</label>
</p>
</form>
</body>
</html>
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)