php在图片上写指定字体颜色的文字函数

php在图片上写指定字体颜色的文字函数,第1张

bool imagestring ( resource $image, int $font, int $x, int $y, string $s, int $col )

imagestring() 用 col 颜色将字符串 s 画到 image 所代表的图像的 x,y 坐标处(这是字符串左上角坐标,整幅图像的左上角为 0,0)。如果 font 是 1,2,3,4 或 5,则使用内置字体。

最后一个参数,表示颜色

$black = ImageColorAllocate($im, 0,0,0);

imagestring(,black)

给你一个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('imagecopyresampled')) //重采样拷贝部分图像并调整大小,真彩

//imagecopyresampled(新图,源图,新图左上角x距离,新图左上角y距离,源图左上角x距离,源图左上角y距离,新图宽,新图高,源图宽,源图高)

imagecopyresampled($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $width, $height, $srcwidth, $srcheight);

else //拷贝部分图像并调整大小,调色板

imagecopyresized($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; //获取文件名

//aaagif aaa_thumbgif

$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_font12), 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) //如果有水印图

{

//imagecopymerge 拷贝并合并图像的一部分

//参数(源图,水印图,拷贝到源图x位置,拷贝到源图y位置,从水印图x位置,从水印图y位置,高,宽,透明度)

imagecopymerge($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

2imagecopyresampled 拷贝图像,并调整大小

水印:水印,文字水印

1 创建图像

2加水印

水印:imagecopymerge 把2张图合并在一起

文字水印:imagettftext 向图像写入文字

/

>

你的底图设置为黑色的,然后文字设置为白色的

//$bg= imagecolorallocate($im,0,0,0);//第一次使用调色板时候的背景颜色

//$te= imagecolorallocate($im,255,255,255);//文字颜色

看看这个代码

for($i=0;$i<4;$i++){

$rand=dechex( rand(1,15));

}

$im = imagecreatetruecolor(100,30);//宽,高

//设置颜色

$bg= imagecolorallocate($im,0,0,0);//第一次使用调色板时候的背景颜色

$te= imagecolorallocate($im,255,255,255);

//把字符串写在图像左上角

imagestring($im,5,0,0,$rand,$te);

//输出图像

header("Content-type: image/jpeg");

imagejpeg($im);

$im=imagecreatetruecolor(100, 30);这个只是绘制一个大小,但是什么也没有

$bg=imagecolorallocate($im, 222,184,135);//第一次使用该函数时为背景颜色,你没有背景颜色

imagefill($im, 0, 0, $bg);

你这样试试

您好!

21、所谓灰度值是指色彩的浓淡程度灰度直方图是指一幅数字图像中,对应每一个灰度值统计出具有该灰度值的象素数。

22、对黑白图像,R,G,B值均相等,称为灰度值,每一个像素有一个灰度值对于8位的灰度图像,其灰度值范围为0~255。

23、灰度也可认为是亮度,简单的说就是色彩的深浅程度。实际上在我们的日常生活中,通过三原色色彩深浅的组合,可以组成各种不同的颜色。产品能够展现的灰度数量越多,也就意味着这款产品的色彩表现力更加丰富,能够实现更强的色彩层次。例如三原色16级灰度,能显示的颜色就是16×16×16=4096色。不过目前的产品256级灰度已经非常地普遍了。

所谓颜色或灰度级指黑白显示器中显示像素点的亮暗差别,在彩色显示器中表现为颜色的不同,灰度级越多,图像层次越清楚逼真。灰度级取决于每个像素对应的刷新存储单元的位数和显示器本身的性能。如每个象素的颜色用16位二进制数表示,我们就叫它16位图,它可以表达2的16次方即65536种颜色。如每一个象素采用24位二进制数表示,我们就叫它24位图,它可以表达2的24次方即16777216种颜色。

灰度就是没有色彩,RGB色彩分量全部相等。如果是一个二值灰度图象,它的象素值只能为0或1,我们说它的灰度级为2。用个例子来说明吧: 一个256级灰度的图象,RGB(100,100,100)就代表灰度为100,RGB(50,50,50)代表灰度为50。

灰度是指黑白图像中点的颜色深度,范围一般从0到255,白色为255 ,黑色为0,故黑白也称灰度图像,在医学、图像识别领域有很广泛的用途

彩色图象的灰度其实在转化为黑白图像后的像素值(是一种广义的提法),转化的方法看应用的领域而定,一般按加权的方法转换,R , G ,B 的比一般为3:6:1。

任何颜色都有红、绿、蓝三原色组成,假如原来某点的颜色为RGB(R,G,B),那么,我们可以通过下面几种方法,将其转换为灰度:

1浮点算法:Gray=R03+G059+B011

2整数方法:Gray=(R30+G59+B11)/100

3移位方法:Gray =(R28+G151+B77)>>8;

4平均值法:Gray=(R+G+B)/3;

5仅取绿色:Gray=G;

通过上述任一种方法求得Gray后,将原来的RGB(R,G,B)中的R,G,B统一用Gray替换,形成新的颜色RGB(Gray,Gray,Gray),用它替换原来的RGB(R,G,B)就是灰度图了。

光标移到哪个行 那个行背景颜色变黄 光标移开恢复原来颜色的方法:一HTML:<tr>二jQuery:1先写好CSS:2jQuery方法: //记得包含jQuery插件文件3html代码:<tr><td width="10%">姓名</td></tr><tr><td width="10%">密码</td></tr><tr><td width="10%">姓名</td></tr><tr><td width="10%">密码</td></tr>

以上就是关于php在图片上写指定字体颜色的文字函数全部的内容,包括:php在图片上写指定字体颜色的文字函数、PHP图片生成、php生成图片验证码为什么是张黑色的等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9725165.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-01
下一篇2023-05-01

发表评论

登录后才能评论

评论列表(0条)

    保存