导航:首页 > 编程语言 > php文字转图片

php文字转图片

发布时间:2024-10-07 02:05:43

‘壹’ php图片生成

给你一个php 图像处理类,完全能实现你的功能,你自己研究一下吧

<?php
class image
{
var $w_pct = 50; //透明度
var $w_quality = 80; //质量
var $w_minwidth = 300; //最小宽
var $w_minheight = 300; //最小高
var $thumb_enable; //是否生成缩略图
var $watermark_enable; //是否生水印
var $interlace = 0; //图像是否为隔行扫描的
var $fontfile; //字体文件
var $w_img ; //默认水印图

function __construct()
{
global $SITE_CONFING;
$this->thumb_enable = $SITE_CONFING['thumb_enable'];
$this->watermark_enable = $SITE_CONFING['watermark_enable'];
$this->set($SITE_CONFING['watermark_minwidth'], $SITE_CONFING['watermark_minheight'], $SITE_CONFING['watermark_quality'], $SITE_CONFING['watermark_pct'], $SITE_CONFING['watermark_fontfile'],$SITE_CONFING['watermark_img']);
}

function image()
{
$this->__construct();
}

function set($w_minwidth = 300, $w_minheight = 300, $w_quality = 80, $w_pct = 100,$fontfile,$w_img)
{
$this->w_minwidth = $w_minwidth;
$this->w_minheight = $w_minheight;
$this->w_quality = $w_quality;
$this->w_pct = $w_pct;
$this->fontfile = $fontfile;
$this->w_img = $w_img;
}

function info($img)
{
$imageinfo = getimagesize($img); //返回图像信息数组 0=>宽的像素 1=>高的像素 2=>是图像类型的标记 3 =>是文本字符串,内容为“height="yyy" width="xxx"”,
if($imageinfo === false) return false;
$imagetype = strtolower(substr(image_type_to_extension($imageinfo[2]),1)); //获取图像文件类型 $imageinfo[2]是图像类型的标记
$imagesize = filesize($img); //图像大小
$info = array(
'width'=>$imageinfo[0],
'height'=>$imageinfo[1],
'type'=>$imagetype,
'size'=>$imagesize,
'mime'=>$imageinfo['mime']
);
return $info;
}

function thumb($image, $filename = '', $maxwidth = 200, $maxheight = 50, $suffix='_thumb', $autocut = 0)
{
if(!$this->thumb_enable || !$this->check($image)) return false;
$info = $this->info($image); //获取图片信息
if($info === false) return false;
$srcwidth = $info['width']; //源图宽
$srcheight = $info['height']; //源图高
$pathinfo = pathinfo($image);
$type = $pathinfo['extension']; //取得扩展名
if(!$type) $type = $info['type']; //如果没有取到,用$info['type']
$type = strtolower($type);
unset($info);
$scale = min($maxwidth/$srcwidth, $maxheight/$srcheight); //获取缩略比例
//获取按照源图的比列
$createwidth = $width = (int)($srcwidth*$scale); //取得缩略宽
$createheight = $height = (int)($srcheight*$scale); //取得缩略高
$psrc_x = $psrc_y = 0;
if($autocut) //按照缩略图的比例来获取
{
if($maxwidth/$maxheight<$srcwidth/$srcheight && $maxheight>=$height) //如果缩略图按比列比源图窄的话
{
$width = $maxheight/$height*$width; //宽按照相应比例做处理
$height = $maxheight; //高不变
}
elseif($maxwidth/$maxheight>$srcwidth/$srcheight && $maxwidth>=$width)//如果缩略图按比列比源图宽的话
{
$height = $maxwidth/$width*$height;
$width = $maxwidth;
}
$createwidth = $maxwidth;
$createheight = $maxheight;
}
$createfun = 'imagecreatefrom'.($type=='jpg' ? 'jpeg' : $type); //找到不同的图像处理函数
$srcimg = $createfun($image); //新建图像
if($type != 'gif' && function_exists('imagecreatetruecolor'))
$thumbimg = imagecreatetruecolor($createwidth, $createheight); //新建一个真彩色图像
else
$thumbimg = imagecreate($width, $height); //新建一个基于调色板的图像

if(function_exists('imageresampled')) //重采样拷贝部分图像并调整大小,真彩
//imageresampled(新图,源图,新图左上角x距离,新图左上角y距离,源图左上角x距离,源图左上角y距离,新图宽,新图高,源图宽,源图高)
imageresampled($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $width, $height, $srcwidth, $srcheight);
else //拷贝部分图像并调整大小,调色板
imageresized($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $width, $height, $srcwidth, $srcheight);
if($type=='gif' || $type=='png')
{
//imagecolorallocate 为一幅图像分配颜色
$background_color = imagecolorallocate($thumbimg, 0, 255, 0); // 给基于调色板的图像填充背景色, 指派一个绿色
// imagecolortransparent 将某个颜色定义为透明色
imagecolortransparent($thumbimg, $background_color); // 设置为透明色,若注释掉该行则输出绿色的图
}
// imageinterlace 激活或禁止隔行扫描
if($type=='jpg' || $type=='jpeg') imageinterlace($thumbimg, $this->interlace);
$imagefun = 'image'.($type=='jpg' ? 'jpeg' : $type);
//imagejpeg imagegif imagepng
if(empty($filename)) $filename = substr($image, 0, strrpos($image, '.')).$suffix.'.'.$type; //获取文件名
//aaa.gif aaa_thumb.gif
$imagefun($thumbimg, $filename); //新建图像
imagedestroy($thumbimg); //销毁缩略图
imagedestroy($srcimg); //销毁源图
return $filename;
}
//watermark(源图,生成文件,生成位置,水印文件,水印文本,背景色)
function watermark($source, $target = '', $w_pos = 0, $w_img = '', $w_text = '', $w_font = 12, $w_color = '#cccccc')
{
if(!$this->watermark_enable || !$this->check($source)) return false;
if(!$target) $target = $source;
if ($w_img == '' && $w_text == '')
$w_img = $this->w_img;
$source_info = getimagesize($source);
$source_w = $source_info[0]; //获取宽
$source_h = $source_info[1]; //获取高
if($source_w < $this->w_minwidth || $source_h < $this->w_minheight) return false; //宽和高达不到要求直接返回
switch($source_info[2]) //新建图片
{
case 1 :
$source_img = imagecreatefromgif($source);
break;
case 2 :
$source_img = imagecreatefromjpeg($source);
break;
case 3 :
$source_img = imagecreatefrompng($source);
break;
default :
return false;
}
if(!empty($w_img) && file_exists($w_img)) //水印文件
{
$ifwaterimage = 1; //是否水印图
$water_info = getimagesize($w_img); //水印信息
$width = $water_info[0];
$height = $water_info[1];
switch($water_info[2])
{
case 1 :
$water_img = imagecreatefromgif($w_img);
break;
case 2 :
$water_img = imagecreatefromjpeg($w_img);
break;
case 3 :
$water_img = imagecreatefrompng($w_img);
break;
default :
return;
}
}
else
{
$ifwaterimage = 0;
//imagettfbbox 本函数计算并返回一个包围着 TrueType 文本范围的虚拟方框的像素大小。
//imagettfbbox ( 字体大小, 字体角度, 字体文件,文件 )
$temp = imagettfbbox(ceil($w_font*1.2), 0, $this->fontfile, $w_text);//取得使用 truetype 字体的文本的范围
$width = $temp[4] - $temp[6]; //右上角 X 位置 - 左上角 X 位置
$height = $temp[3] - $temp[5]; //右下角 Y 位置- 右上角 Y 位置
unset($temp);
}
switch($w_pos)
{
case 0: //随机位置
$wx = rand(0,($source_w - $width));
$wy = rand(0,($source_h - $height));
break;
case 1: //左上角
$wx = 5;
$wy = 5;
break;
case 2: //上面中间位置
$wx = ($source_w - $width) / 2;
$wy = 0;
break;
case 3: //右上角
$wx = $source_w - $width;
$wy = 0;
break;
case 4: //左面中间位置
$wx = 0;
$wy = ($source_h - $height) / 2;
break;
case 5: //中间位置
$wx = ($source_w - $width) / 2;
$wy = ($source_h - $height) / 2;
break;
case 6: //底部中间位置
$wx = ($source_w - $width) / 2;
$wy = $source_h - $height;
break;
case 7: //左下角
$wx = 0;
$wy = $source_h - $height;
break;
case 8: //右面中间位置
$wx = $source_w - $width;
$wy = ($source_h - $height) /2;
break;
case 9: //右下角
$wx = $source_w - $width;
$wy = $source_h - $height ;
break;
default: //随机
$wx = rand(0,($source_w - $width));
$wy = rand(0,($source_h - $height));
break;
}
if($ifwaterimage) //如果有水印图
{
//imagemerge 拷贝并合并图像的一部分
//参数(源图,水印图,拷贝到源图x位置,拷贝到源图y位置,从水印图x位置,从水印图y位置,高,宽,透明度)
imagemerge($source_img, $water_img, $wx, $wy, 0, 0, $width, $height, $this->w_pct);
}
else
{
if(!empty($w_color) && (strlen($w_color)==7))
{
$r = hexdec(substr($w_color,1,2)); //获取红色
$g = hexdec(substr($w_color,3,2)); //获取绿色
$b = hexdec(substr($w_color,5)); //获取蓝色
}
else
{
return;
}
//imagecolorallocate 基于调色板的图像填充背景色
//imagestring 水平地画一行字符串
//imagestring(源图,字体大小,位置X,位置Y,文字,颜色)
//参数($image, float $size, float $angle, int $x, int $y, int $color, string $fontfile, string $text)
imagettftext($source_img,$w_font,0,$wx,$wy,imagecolorallocate($source_img,$r,$g,$b),$this->fontfile,$w_text);
//imagestring($source_img,$w_font,$wx,$wy,$w_text,imagecolorallocate($source_img,$r,$g,$b));
}
//输出到文件或者浏览器
switch($source_info[2])
{
case 1 :
imagegif($source_img, $target); //以 GIF 格式将图像输出到浏览器或文件
break;
case 2 :
imagejpeg($source_img, $target, $this->w_quality); //以 JPEG 格式将图像输出到浏览器或文件
break;
case 3 :
imagepng($source_img, $target); //以 PNG 格式将图像输出到浏览器或文件
break;
default :
return;
}
if(isset($water_info))
{
unset($water_info); //销毁
}
if(isset($water_img))
{
imagedestroy($water_img); //销毁
}
unset($source_info);
imagedestroy($source_img);
return true;
}
//gd库必须存在,后缀为jpg|jpeg|gif|png,文件存在,imagecreatefromjpeg或者imagecreatefromgif存在
function check($image)
{
return extension_loaded('gd') &&
preg_match("/\.(jpg|jpeg|gif|png)/i", $image, $m) &&
file_exists($image) &&
function_exists('imagecreatefrom'.($m[1] == 'jpg' ? 'jpeg' : $m[1]));
//imagecreatefromjpeg
//imagecreatefromgif
//imagecreatefrompng
}
}

/**
缩略图
1.新建一个图像资源 通过 imagecreatefromgif imagecreatefromjpeg imagecreatefrompng
2.imageresampled 拷贝图像,并调整大小

水印:图片水印,文字水印
1. 创建图像
2.加水印
图片水印:imagemerge 把2张图合并在一起
文字水印:imagettftext 向图像写入文字

*/
?>

‘贰’ php 或 js 怎么在图片上添加文字和图片,

在图片上添加文件,可以的,不过需要借助ocr文字识别技术,方法如下:

打开ocr---高级识别---添加文件---识别;

然后呢,在右边直接打上文字,最后,可以保持为图片或者是word!

对上面的方法有所疑问的欢迎提问哦!!!

‘叁’ php怎么做图片识别

个人建议:楼主使用php进行图片识别,不如使用ocr文字识别技术来进行图片识别,这样更方便,请看下面的方法:

  1. 首先,在电脑上安装ocr文字识别软件(迅捷ocr文字识别软件)。

  2. 接着,运行ocr,选择上面的‘极速识别’功能。

  3. 然后,点击左上角的‘添加文件’,不需要识别的图片添加进去。

  4. 最后,点击操作下面的‘开始识别’按钮。

希望上方的方法可以帮助到你。

‘肆’ php如何生成加粗或者斜体的文字样式图片

  1. 加粗或者斜体的文字可以用php的函数控制.我想你是想生成验证码图片是吗?

  2. 如果是想生成验证么图片有几个函数可以考虑

    imagecreate($length,$height)创建图片.参数是图片的宽度和高度

    imagecolorallocate($image,$r,$g,$b)设置背景色,r b g就是图片的三色rgb参数.这个可以由传入0-255的随机数决定随机的背景色.还可以生成字体色

    imagettftext($_image,$fontSize,mt_rand(-40,70),$codeNX,$fontSize*1.5,$_color,$ttf,$code[$i]);写入随机的文字,这里要一个字一个字写.所以这个函数要循环调用.

    网络了一下 找到了一个类...如下


  3. <?php
    /**
    *安全验证码
    *
    *安全的验证码要:验证码文字扭曲、旋转,使用不同字体,添加干扰码。
    *如果用中文做验证码(我这里不是哦,有兴趣你来改成用中文的),安全度会更好些,但验证码扭曲和旋转是王道,用了字体也算是已经给字体扭曲了,我就不再去给他添一只扭曲的足了。
    *可配置的属性都是一些简单直观的变量,我就不用弄一堆的setter/getter了
    *
    *@author流水孟春<cmpan(at)qq.com>
    *@rightNEWBSD
    *@linkhttp://labs.yulans.cn/YL_Security_Secoder
    *@linkhttp://wiki.yulans.cn/docs/yl/security/secoder
    */
    classYL_Security_Secoder{
    /**
    *验证码的session的下标
    *
    *@varstring
    */
    publicstatic$seKey='sid.sekey.ylans.cn';
    publicstatic$expire=3000;//验证码过期时间(s)
    /**
    *验证码中使用的字符,01IO容易混淆,建议不用
    *
    *@varstring
    */
    publicstatic$codeSet='346789ABCDEFGHJKLMNPQRTUVWXY';
    publicstatic$fontSize=25;//验证码字体大小(px)
    publicstatic$useCurve=true;//是否画混淆曲线
    publicstatic$useNoise=true;//是否添加杂点
    publicstatic$imageH=0;//验证码图片宽
    publicstatic$imageL=0;//验证码图片长
    publicstatic$length=4;//验证码位数
    publicstatic$bg=array(243,251,254);//背景

    protectedstatic$_image=null;//验证码图片实例
    protectedstatic$_color=null;//验证码字体颜色

    /**
    *输出验证码并把验证码的值保存的session中
    *验证码保存到session的格式为:$_SESSION[self::$seKey]=array('code'=>'验证码值','time'=>'验证码创建时间');
    */
    publicstaticfunctionentry(){
    //图片宽(px)
    self::$imageL||self::$imageL=self::$length*self::$fontSize*1.5+self::$fontSize*1.5;
    //图片高(px)
    self::$imageH||self::$imageH=self::$fontSize*2;
    //建立一幅self::$imageLxself::$imageH的图像
    self::$_image=imagecreate(self::$imageL,self::$imageH);
    //设置背景
    imagecolorallocate(self::$_image,self::$bg[0],self::$bg[1],self::$bg[2]);
    //验证码字体随机颜色
    self::$_color=imagecolorallocate(self::$_image,mt_rand(1,120),mt_rand(1,120),mt_rand(1,120));
    //验证码使用随机字体
    $ttf=dirname(__FILE__).'/ttfs/'.mt_rand(1,20).'.ttf';

    if(self::$useNoise){
    //绘杂点
    self::_writeNoise();
    }
    if(self::$useCurve){
    //绘干扰线
    self::_writeCurve();
    }

    //绘验证码
    $code=array();//验证码
    $codeNX=0;//验证码第N个字符的左边距
    for($i=0;$i<self::$length;$i++){
    $code[$i]=self::$codeSet[mt_rand(0,27)];
    $codeNX+=mt_rand(self::$fontSize*1.2,self::$fontSize*1.6);
    //写一个验证码字符
    imagettftext(self::$_image,self::$fontSize,mt_rand(-40,70),$codeNX,self::$fontSize*1.5,self::$_color,$ttf,$code[$i]);
    }

    //保存验证码
    isset($_SESSION)||session_start();
    $_SESSION[self::$seKey]['code']=join('',$code);//把校验码保存到session
    $_SESSION[self::$seKey]['time']=time();//验证码创建时间

    header('Cache-Control:private,max-age=0,no-store,no-cache,must-revalidate');
    header('Cache-Control:post-check=0,pre-check=0',false);
    header('Pragma:no-cache');
    header("content-type:image/png");

    //输出图像
    imagepng(self::$_image);
    imagedestroy(self::$_image);
    }

    /**
    *画一条由两条连在一起构成的随机正弦函数曲线作干扰线(你可以改成更帅的曲线函数)
    *
    *高中的数学公式咋都忘了涅,写出来
    * 正弦型函数解析式:y=Asin(ωx+φ)+b
    *各常数值对函数图像的影响:
    *A:决定峰值(即纵向拉伸压缩的倍数)
    *b:表示波形在Y轴的位置关系或纵向移动距离(上加下减)
    *φ:决定波形与X轴位置关系或横向移动距离(左加右减)
    *ω:决定周期(最小正周期T=2π/∣ω∣)
    *
    */
    protectedstaticfunction_writeCurve(){
    $A=mt_rand(1,self::$imageH/2);//振幅
    $b=mt_rand(-self::$imageH/4,self::$imageH/4);//Y轴方向偏移量
    $f=mt_rand(-self::$imageH/4,self::$imageH/4);//X轴方向偏移量
    $T=mt_rand(self::$imageH*1.5,self::$imageL*2);//周期
    $w=(2*M_PI)/$T;

    $px1=0;//曲线横坐标起始位置
    $px2=mt_rand(self::$imageL/2,self::$imageL*0.667);//曲线横坐标结束位置
    for($px=$px1;$px<=$px2;$px=$px+0.9){
    if($w!=0){
    $py=$A*sin($w*$px+$f)+$b+self::$imageH/2;//y=Asin(ωx+φ)+b
    $i=(int)((self::$fontSize-6)/4);
    while($i>0){
    imagesetpixel(self::$_image,$px+$i,$py+$i,self::$_color);//这里画像素点比imagettftext和imagestring性能要好很多
    $i--;
    }
    }
    }

    $A=mt_rand(1,self::$imageH/2);//振幅
    $f=mt_rand(-self::$imageH/4,self::$imageH/4);//X轴方向偏移量
    $T=mt_rand(self::$imageH*1.5,self::$imageL*2);//周期
    $w=(2*M_PI)/$T;
    $b=$py-$A*sin($w*$px+$f)-self::$imageH/2;
    $px1=$px2;
    $px2=self::$imageL;
    for($px=$px1;$px<=$px2;$px=$px+0.9){
    if($w!=0){
    $py=$A*sin($w*$px+$f)+$b+self::$imageH/2;//y=Asin(ωx+φ)+b
    $i=(int)((self::$fontSize-8)/4);
    while($i>0){
    imagesetpixel(self::$_image,$px+$i,$py+$i,self::$_color);//这里(while)循环画像素点比imagettftext和imagestring用字体大小一次画出(不用这while循环)性能要好很多
    $i--;
    }
    }
    }
    }

    /**
    *画杂点
    *往图片上写不同颜色的字母或数字
    */
    protectedstaticfunction_writeNoise(){
    for($i=0;$i<10;$i++){
    //杂点颜色
    $noiseColor=imagecolorallocate(
    self::$_image,
    mt_rand(150,225),
    mt_rand(150,225),
    mt_rand(150,225)
    );
    for($j=0;$j<5;$j++){
    //绘杂点
    imagestring(
    self::$_image,
    5,
    mt_rand(-10,self::$imageL),
    mt_rand(-10,self::$imageH),
    self::$codeSet[mt_rand(0,27)],//杂点文本为随机的字母或数字
    $noiseColor
    );
    }
    }
    }

    /**
    *验证验证码是否正确
    *
    *@paramstring$code用户验证码
    *@returnbool用户验证码是否正确
    */
    publicstaticfunctioncheck($code){
    isset($_SESSION)||session_start();
    //验证码不能为空
    if(empty($code)||empty($_SESSION[self::$seKey])){
    returnfalse;
    }
    //session过期
    if(time()-$_SESSION[self::$seKey]['time']>self::$expire){
    unset($_SESSION[self::$seKey]);
    returnfalse;
    }

    if($code==$_SESSION[self::$seKey]['code']){
    returntrue;
    }

    returnfalse;
    }
    }


    //useage
    /*
    YL_Security_Secoder::$useNoise=false;//要更安全的话改成true
    YL_Security_Secoder::$useCurve=true;
    YL_Security_Secoder::entry();
    */

    /*
    //验证验证码
    if(!YL_Security_Secoder::check(@$_POST['secode'])){
    print'errorsecode';
    }
    */
  4. 这是效果

‘伍’ 在php图片生成中怎么控制生成出来的图片的文字方向(右到左)

若是单行的话,那比较简单,直接把文字逆过来,然后象平常一样把字符写到图片上面。

多行的话,又要比较整齐的话,可以用 imagettfbbox 或imagefontwidth 等函数 取得字符的大小。从头向后取,把他们的字符宽度加起,当宽度和大于图片宽度的时候就截取一次,然后可象单行那样处理。

若是字符里面有英文单词或类似的,又要保持可读取性的话,那需要加一定的策略处理这些,如遇上英文单词之类的不逆序

‘陆’ 求代码示例:php将数据库读取出来的文字转成图片显示在页面上

<?php
$Phone=18907975647;#手机号码,具体从数据库怎么读出来,你自己写代码
$im=imagecreate(300,30);#建立一个宽300,高30像素的图片对象
imagecolorallocate($im,255,255,255);#将图片背景填充为白色
$Color=imagecolorallocate($im,0,0,0);#在生成一黑色色颜色,以便写入字符串
imagestring($im,16,0,0,$Phone,$Color);#将字符串写到图片上
header('content-type:image/*');//设置文件头为图片格式
imagepng($im);//输出一个png格式的图片
imagedestroy($im);//销毁图片对象

下面写效果图:

‘柒’ 用php代码怎么以背景图片加上文字生成新的图片,然后在标题处绝对调用该图片

<?php
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=你好"/>

阅读全文

与php文字转图片相关的资料

热点内容
脉脉app干什么用的 浏览:357
拽姐是哪个app 浏览:858
云服务器删除了还有吗 浏览:232
macbook可以用单片机嘛 浏览:307
南阳php招聘 浏览:814
去哪里找按摩师很漂亮的app 浏览:818
86x99用简便算法计算 浏览:830
php截图flash 浏览:273
卸载联想app哪个好 浏览:720
php文字转图片 浏览:332
豆客后台怎么加密码 浏览:574
jpg转换pdf破解版 浏览:978
php基础书籍推荐 浏览:778
服务器与外网不通如何验证 浏览:352
电子版是不是就是文件夹 浏览:51
游戏属性文件加密 浏览:464
如何让安卓手机桌面图标下移 浏览:530
ubuntuphp5环境搭建 浏览:101
赌瘾解压视频 浏览:919
晋城移动dns服务器地址 浏览:296