<?php
/*
param $image 图象资源
param size 字体大小
param angle 字体输出角度
param showX 输出位置x坐标
param showY 输出位置y坐标
param font 字体文件位置
param content 要在图片里显示的内容
*/
class showChinaText {
var $text = 'php网站程序开发';
var $font = 'fonts/simsun.ttc'; //如果没有要自己加载到相应的目录下(本地www)
var $angle = 0;
var $size = 15;
var $showX = 100;
var $showY = 160; var $text0 = '2011 年 12 月 12 日';
var $angle0 = 0;
var $showX0 = 230;
var $showY0 = 200; var $text1 = '新郎';
var $angle1 = 20;
var $showX1 = 135;
var $showY1 = 285; var $text2 = '新娘';
var $angle2 = 20;
var $showX2 = 300;
var $showY2 = 285; var $text3 = '北京市海淀区香格里拉酒店';
var $angle3 = 0;
var $showX3 = 120;
var $showY3 = 445; var $text4 = '上午十一点整';
var $angle4 = 0;
var $showX4 = 305;
var $showY4 = 480; function showChinaText($showText = '') {
$this->text = ! isset ( $showText ) ? $showText : $this->text;
$this->show ();
}
function createText($instring) {
$outstring = "";
$max = strlen ( $instring );
for($i = 0; $i < $max; $i ++) {
$h = ord ( $instring [$i] );
if ($h >= 160 && $i < $max - 1) {
$outstring .= substr ( $instring, $i, 2 );
$i ++;
} else {
$outstring .= $instring [$i];
}
}
return $outstring;
}
function show() {
//输出头内容
Header ( "Content-type: image/png" );
//建立图象
//$image = imagecreate(400,300);
$image = imagecreatefromjpeg ( "01.jpg" ); //这里的图片,换成你的图片路径
//定义颜色
$red = ImageColorAllocate ( $image, 255, 0, 0 );
$white = ImageColorAllocate ( $image, 255, 255, 255 );
$black = ImageColorAllocate ( $image, 0, 0, 0 );
//填充颜色
//ImageFilledRectangle($image,0,0,200,200,$red);
//显示文字
$txt = $this->createText ( $this->text );
$txt0 = $this->createText ( $this->text0 );
$txt1 = $this->createText ( $this->text1 );
$txt2 = $this->createText ( $this->text2 );
$txt3 = $this->createText ( $this->text3 );
$txt4 = $this->createText ( $this->text4 );
//写入文字
imagettftext ( $image, $this->size, $this->angle, $this->showX, $this->showY, $white, $this->font, $txt );
imagettftext ( $image, $this->size, $this->angle0, $this->showX0, $this->showY0, $white, $this->font, $txt0 );
imagettftext ( $image, $this->size, $this->angle1, $this->showX1, $this->showY1, $white, $this->font, $txt1 );
imagettftext ( $image, $this->size, $this->angle2, $this->showX2, $this->showY2, $white, $this->font, $txt2 );
imagettftext ( $image, $this->size, $this->angle3, $this->showX3, $this->showY3, $white, $this->font, $txt3 );
imagettftext ( $image, $this->size, $this->angle4, $this->showX4, $this->showY4, $white, $this->font, $txt4 );
//ImageString($image,5,50,10,$txt,$white);
//显示图形
imagejpeg ( $image );
imagegif ( $image, "a2.jpg" );
ImageDestroy ( $image );
}
}
?>
<?php //使用
$s = new showChinaText ();
?>

改造后版本:背景图片自定义上传

<?php
class ChinaText { var $font = 'simsun.ttc'; //如果没有要自己加载到相应的目录下(本地www)
var $size = 15; var $multexts = null;
var $bg=''; function ChinaText($title_text=null,$bg='11.jpg') {
$this->multexts = $title_text;
$this->bg = $bg; $this->show ();
} function show() {
//输出头内容
Header ( "Content-type: image/png" );
//建立图象
//$image = imagecreate(400,300);
$image = imagecreatefromjpeg ( "./data/upload/".$this->bg); //这里的图片,换成你的图片路径
//定义颜色
$red = ImageColorAllocate ( $image, 255, 0, 0 );
$white = ImageColorAllocate ( $image, 255, 255, 255 );
$black = ImageColorAllocate ( $image, 0, 0, 0 );
//填充颜色
//ImageFilledRectangle($image,0,0,200,200,$red); foreach ($this->multexts as $tx){
imagettftext (
$image,
$this->size,
$tx['angle'],
$tx['showX'],
$tx['showY'],
$black,
$this->font,
$tx['text'] ); } //显示图形
imagejpeg ( $image );
//imagegif ( $image, "a2.jpg" );
//ImageDestroy ( $image );
}
}

控制器部分:

public function test2(){
$array = array(
array('text'=>'aaaopop','angle'=>0,'showX'=>100,'showY'=>160),
array('text'=>'bbb成果,没有任何','angle'=>0,'showX'=>100,'showY'=>190),
array('text'=>'cccqqqqq踩踩','angle'=>0,'showX'=>100,'showY'=>220),
array('text'=>'ddd 踩踩踩踩踩','angle'=>0,'showX'=>100,'showY'=>250),
); $s = new ChinaText($array); } public function test3(){
//$image = $_FILES['bg'];
$text = $_POST['text']; //上传图片
//上传目录
$imagebg = $this->_upload($_FILES['bg'], '/');
if ($imagebg['error']) {
$this->error($imagebg['info']);
} else {
$data['imagebg'] = $imagebg['info'][0]['savename'];
//echo $data['imagebg'];
} $title['showY']=100;
$title_text = array(); for ( $i=0;$i<count($text);$i++){
$title['text']=$text[$i];
$title['angle']=0;
$title['showX']=100;
$title['showY']+=50*$i;
$title_text[] = $title;
} $s=new ChinaText($title_text,$data['imagebg']); }

html :

<form action="{:U('test/test3')}" method="post"  enctype="multipart/form-data">
<span>背景图片:</span>
<input type="file" name='bg'><br><br> <span>文字区域:</span>
<input type="text" name="text[]"><br><br>
<input type="text" name="text[]"><br><br>
<input type="text" name="text[]"><br><br> <input type="submit" value="生成"> </form>

php图片上面写文字,输出图片的更多相关文章

  1. 函数putText()在图片上写文字

    #include <iostream> #include <opencv2/opencv.hpp> using namespace std; using namespace c ...

  2. 使用Qpaint在图片上写文字

    开发过程中需要实现在图片上叠加文字,可以采用Qpaint在图片上写文字,然后将图片显示在上面.再将Qlabel加到Qwidget中.效果如下 //创建对象,加载图片 QPixmap pix; pix. ...

  3. thinkphp 利用GD库在图片上写文字

    <?php /** * Created by PhpStorm. * User: Administrator */ namespace Home\Event; use \Think\Image; ...

  4. C# GDI+ 简单实现图片写文字和图片叠加(水印)(转)

    using System; using System.Collections; using System.Configuration; using System.Data; using System. ...

  5. python生成透时图片and 写文字

    import Image from get_png import getpng def transparent(infile): #open png,covert it into 'RGBA mode ...

  6. Swift - 给图片添加文字水印(图片上写文字,并可设置位置和样式)

    想要给图片添加文字水印或者注释,我们需要实现在UIImage上写字的功能. 1,效果图如下: (在图片左上角和右下角都添加了文字.) 2,为方便使用,我们通过扩展UIImage类来实现添加水印功能 ( ...

  7. C#图片上写文字

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Dr ...

  8. PHP图片加水印文字及图片合成缩放

    <?php //图片添加文字水印 /*$bigImgPath = 'background.png'; $img = imagecreatefromstring(file_get_contents ...

  9. android图片加水印,文字

    两种方法: 1.直接在图片上写文字 String str = "PICC要写的文字"; ImageView image = (ImageView) this.findViewByI ...

随机推荐

  1. ECSTORE AJAX提交的实现

    今天向大家介绍在ecstore中怎样使用ajax提交数据 1 <script> //JAVASCRIPT代码 $$(".BB").ADDEVENT('CHANGE',F ...

  2. PHP的抽象类和接口

    抽象类与接口相似,都是一种比较特殊的类.抽象类是一种特殊的类,而接口也是一种特殊的抽象类.它们通常配合面向对象的多态性一起使用.虽然声明和使用都比较容易,但它们的作用在理解上会困难一点. ①抽象类 在 ...

  3. htmlspecialchars()函数

    htmlspecialchars() 函数把一些预定义的字符转换为 HTML 实体. 预定义的字符是: & (和号) 成为 & " (双引号) 成为 " ' (单引 ...

  4. IOS动画总结

    IOS动画总结   一.基本方式:使用UIView类的UIViewAnimation扩展 + (void)beginAnimations:(NSString *)animationID context ...

  5. 深入理解querySelector(All)

          querySelector和querySelectorAll同属于Selectors API Level 1规范,该规范早在2006年就已经开始发展,并在2007年10月形成querySe ...

  6. 转:解决方案your project contains error s please fix them before running your application

    文章来自于:http://www.mythroad.net/2013/08/05/%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88your-project-contains-e ...

  7. CentOS下安装postgresql

    一.说明 postgresql版本:9.4.1 安装包: postgresql94-server-9.4.1-1PGDG.rhel6.x86_64.rpm postgresql94-libs-9.4. ...

  8. css之marquee,让你的文字跳起来

    当你看到别人的网页文字动态效果美美哒,而你却为不会使用js而遗憾时,不妨看看这篇文章,教你如何只用css即可实现漂亮的文字滑动效果. 1.问题提出: 在一个特定大小的div中,如何让p标签内的内容动态 ...

  9. Manor

    Description Bob有n个正整数,他将这n个整数根据大小划分成两部分.对于小于等于k的整数放在集合A中,其余的放在集合B中.每次他从集合B中取出一个最大的值,将其变成0放入A集合中.然后将A ...

  10. makefile工程管理

    个人理解吧,makefile就是写一个指定格式的文件,将一系列的编译.链接.转换等操作打包在一起,方便以后一键生成可执行的二进制文件而产生的.下面记录一下这种文件的写法,方便以后忘了来查询. make ...