图像生成器Width = $width;

$this->Height = $height;

$this->BackColor = $backColor;

$this->_image=imagecreatetruecolor($this->Width, $this->Height);

if (headers_sent()){

throw new RuntimeException('Header sent.');

}

// 初始化背景

$backColor=imagecolorallocate($this->_image,

(int)($this->BackColor%0x1000000/0x10000),

(int)($this->BackColor%0x10000/0x100),

$this->BackColor%0x100);

imagefilledrectangle($this->_image, 0, 0, $this->Width, $this->Height, $this->BackColor);

imagecolordeallocate($this->_image, $this->BackColor);

if($transparent){

// 设置透明

imagecolortransparent($this->_image, $this->BackColor);

}

}

/**

* 呈现的图片或文本,数组,按定义的顺序。

* 元素为 x, y, type=text|image, content=图片路径或文本

* @param $renders 要呈现的内容。

* 模式如下:

* array(

* array(

* 'x' => 0,

* 'y' => 0,

* 'type' => 'text',

* 'content'=>'图片路径或文本内容',

* // 下面是文本的附加属性

* 'font' => null,

* 'fontsize' => 30,

* 'angle' => 0,

* 'color' => 0x000000

* )

* );

*/

public function Render(array $renders){

foreach ($renders as $r){

if (!is_array($r)|| !isset($r['type'])|| !isset($r['content'])) {

continue;

}

$type = $r['type'];

$x = isset($r['x'])?$r['x']:0;

$y = isset($r['y'])?$r['y']:0;

$ResizeRate = isset($r['ResizeRate'])?$r['ResizeRate']:1;

$AutoResize = isset($r['AutoResize'])?$r['AutoResize']:0;

$content = $r['content'];

if ($type==='text'){

$fontsize = isset($r['fontsize'])?$r['fontsize']:30;

$angle = isset($r['angle'])?$r['angle']:0;

$foreColor = isset($r['color'])?$r['color']:0x000000;

$fontfile = isset($r['font'])?$r['font']:null;

$this->RenderText($content, $x, $y, $fontsize, $angle, $foreColor, $fontfile);

}

else{

$this->RenderImage($content, $x, $y,$ResizeRate,$AutoResize);

}

}

}

/**

* 呈现文本。

* @param string $text 文本。

* @param integer $x 横坐标。

* @param integer $y 纵坐标。

* @param integer $size 字体尺寸,像素。

* @param integer $angle 角度。

* @param integer $foreColor 文本颜色。

* @param string $fontfile 字体文件路径。

*/

public function RenderText($text, $x=0, $y=0, $size=30, $angle=0, $foreColor=0x000000, $fontfile=null){

$c = func_num_args();

if ($c===1 && is_array($text)){

extract($text);

}

if ($text===''){

return;

}

$fontfile = is_null($fontfile)?$this->FontFile:$fontfile;

$foreColor=imagecolorallocate($this->_image,

(int)($foreColor%0x1000000/0x10000),

(int)($foreColor%0x10000/0x100),

$foreColor%0x100);

imagettftext($this->_image, $size, $angle, $x, $y, $foreColor, $fontfile, $text);

imagecolordeallocate($this->_image, $foreColor);

}

/**

* 呈现图像,按 1:1 大小简单输出。

* @param string $src 图像文件。

* @param integer $x 横坐标。

* @param integer $y 纵坐标。

*/

public function RenderImage($src, $x=0, $y=0,$ResizeRate=1,$AutoResize=1){

$c = func_num_args();

if ($c===1 && is_array($src)){

extract($src);

}

if ($src===''){

return;

}

$meta = getimagesize($src);

$w = $meta[0];

$h = $meta[1];

$new_width=$w/$ResizeRate;

$new_height=$h/$ResizeRate;

switch($meta[2]){

case IMAGETYPE_GIF:

$image=imagecreatefromgif($src);

break;

case IMAGETYPE_PNG:

$image=imagecreatefrompng($src);

break;

case IMAGETYPE_JPEG:

default:

$image=imagecreatefromjpeg($src);

break;

}

//imagecopy($this->_image, $image, $x, $y, 0, 0, $w, $h); //简单输出

if($AutoResize==1){

$this->Width;//当前底图的宽

$this->Height;//当前底图的高

$MaxSizeWidth = $this->Width-(2*$x);//缩小后的图片的最大宽度

$MaxSizeHeight = $this->Height-(2*$y);//缩小后的图片的最大高度

// echo $MaxSizeWidth;

// echo $MaxSizeHeight;

if( ($MaxSizeWidth>=$w) && ($MaxSizeHeight>=$h))//宽高都小,可以不缩小的情况,直接居中{

$outputSx=($this->Width-$w)/2;

$outputSy=($this->Height-$h)/2;

imagecopyresized($this->_image, $image, $outputSx, $outputSy, 0, 0, $w, $h, $w, $h);

}

else{

$DstWHRate= $MaxSizeWidth/$MaxSizeHeight;//缩小后的长宽比

$SrcWhRate= $w/$h;//需要缩小的图片

if ($DstWHRate>=$SrcWhRate){

$ResizeRate=($h/$MaxSizeHeight);//获得缩放比例

}

else{

$ResizeRate=($w/$MaxSizeWidth);//获得缩放比例

}

$new_width=ceil($w/$ResizeRate);

$new_height=ceil($h/$ResizeRate);

$outputSx=ceil(($this->Width-$new_width)/2);

$outputSy=ceil(($this->Height-$new_height)/2);

imagecopyresized($this->_image, $image, $outputSx, $outputSy, 0, 0, $new_width, $new_height, $w, $h);

}

// echo $w;

// echo $h;

}

else{

imagecopyresampled($this->_image, $image, $x, $y, 0, 0, $new_width, $new_height, $w, $h);

}

imagedestroy($image);

}

/**

* 输出内容到浏览器。

*/

public function Flush(){

switch (strtoupper($this->Type)){

case 'JPG':

imagejpeg($this->_image);

break;

case 'GIF':

imagegif($this->_image);

break;

default:

imagepng($this->_image);

break;

}

}

/**

* 输出到文件或返回内容。

* @param string $filename

* @Return void 如果未提供文件名,则返回图像内容。如果提供了文件名则输出到文件中。

*/

public function Output($filename=null){

if (!empty($filename)){

switch (strtoupper($this->Type)){

case 'JPG':

imagejpeg($this->_image, $filename);

break;

case 'GIF':

imagegif($this->_image, $filename);

break;

default:

imagepng($this->_image, $filename);

break;

}

}

else{

ob_start();

switch (strtoupper($this->Type)){

case 'JPG':

imagejpeg($this->_image);

break;

case 'GIF':

imagegif($this->_image);

break;

default:

imagepng($this->_image);

break;

}

$r = ob_get_contents();

ob_end_clean();

return $r;

}

}

/**

* 释放资源,当对象实例卸载时也被隐式调用。

*/

public function Dispose(){

if (!is_null($this->_image)){

imagedestroy($this->_image);

$this->_image = null;

}

}

public function __destruct(){

$this->Dispose();

}

/**

* 直接呈现。

* @param integer $width 图像宽度。

* @param integer $height 图像高度。

* @param integer $backColor 背景颜色。

* @param array $renders 呈现内容,同 Render 方法定义。

*/

public static function DirectRender($width=100, $height=50, $backColor=0xFFFFFF, array $renders, $type="jpg"){

header('Pragma: public');

header('Expires: 0');

header('Cache-Control: must-revalidate, post-check=0, pre-check=0');

header('Content-Transfer-Encoding: binary');

header("Content-type: image/".($type==='JPG'?'jpeg':strtolower($type)));

$b = new self($width, $height, $backColor);

$b->Render($renders);

$b->Flush();

$b->Dispose();

}

/**

* 呈现到文件或返回图像数据。

* @param integer $width 图像宽度。

* @param integer $height 图像高度。

* @param integer $backColor 背景颜色。

* @param array $renders 呈现内容,同 Render 方法定义。

* @param string $filename 呈现到的文件名,不提供则直接返回内容。

*/

public static function RenderTo($width=100, $height=50, $backColor=0xFFFFFF, array $renders, $filename=null){

$b = new self($width, $height, $backColor);

$b->Render($renders);

$r = $b->Output($filename);

$b->Dispose();

return $r;

}

}

// 简单组合文本和图片,并且返回图像数据。

ImageBuilder::DirectRender(900, 700, 0xF0F0F0, array(

array('x'=>0,'y'=>0,'type'=>'image', 'content'=>'bg/asw.gif'),

array('x'=>260,'y'=>30,'type'=>'image', 'content'=>'1.jpg'),

array('x'=>75, 'y'=>54, 'type'=>'text', 'content'=>'大水车', 'fontsize'=> 12, 'font'=>'ARIALUNI.TTF', 'color'=>0x000000),

array('x'=>75, 'y'=>86, 'type'=>'text', 'content'=>'中性', 'fontsize'=> 12, 'font'=>'ARIALUNI.TTF', 'color'=>0x000000),

array('x'=>155, 'y'=>86, 'type'=>'text', 'content'=>'宅族', 'fontsize'=> 12, 'font'=>'ARIALUNI.TTF', 'color'=>0x000000),

array('x'=>75, 'y'=>116, 'type'=>'text', 'content'=>'1980', 'fontsize'=> 12, 'font'=>'ARIALUNI.TTF', 'color'=>0x000000),

array('x'=>135, 'y'=>116, 'type'=>'text', 'content'=>'11', 'fontsize'=> 12, 'font'=>'ARIALUNI.TTF', 'color'=>0x000000),

array('x'=>170, 'y'=>116, 'type'=>'text', 'content'=>'11', 'fontsize'=> 12, 'font'=>'ARIALUNI.TTF', 'color'=>0x000000),

array('x'=>75, 'y'=>146, 'type'=>'text', 'content'=>'湖北省仙桃市靠山屯村', 'fontsize'=> 12, 'font'=>'ARIALUNI.TTF', 'color'=>0x000000),

array('x'=>75, 'y'=>166, 'type'=>'text', 'content'=>'六蛋七巷 8 弄 110 号', 'fontsize'=> 12, 'font'=>'ARIALUNI.TTF', 'color'=>0x000000),

array('x'=>145, 'y'=>219, 'type'=>'text', 'content'=>'312306198011113298', 'fontsize'=> 12, 'font'=>'ARIALUNI.TTF', 'color'=>0x000000)

)

);

相关标签:图像生成器

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

php图片全屏代码生成器,图像生成器相关推荐

  1. 全屏背景:15个jQuery插件实现全屏背景图像或媒体

    动态网站通常利用背景图像或预加载屏幕,以保证所有资源都加载到页面上,在浏览器中充分呈现.现在很多网站都炫耀自己的图像作为背景图像全屏背景,追溯到旧的Flash网站却用自己的方式在HTML资源重布局. ...

  2. 3种CSS实现背景图片全屏铺满自适应的方式

    来源 | https://www.fly63.com/ 一张清晰漂亮的背景图片能给网页加分不少,设计师也经常会给页面的背景使用大图,我们既不想图片因为不同分辨率图片变形,也不希望当在大屏的情况下,背景 ...

  3. CSS:实现background-image背景图片全屏铺满自适应

    body {/* 加载背景图 */background-image: url(images/bg.jpg);/* 背景图垂直.水平均居中 */background-position: center c ...

  4. js大屏导出图片_超大图片全屏动态展示js插件

    intense-images是一款非常实用的超大图片全屏动态展示js插件.该图片查看插件可以全屏显示超大图片,可以使用鼠标来和图片进行交互,上下左右移动鼠标会相应的移动图片,对于超大图片的展示是非常好 ...

  5. 手机端点击图片全屏预览

    <!doctype html> 手机端点击图片全屏预览 <div class="category"><img src="1.jpg" ...

  6. 微信小程序背景图片全屏显示

    微信小程序背景图片全屏显示 很多人在写小程序界面的时候希望背景图片是全屏覆盖显示的(包括顶部导航栏,如下图),那该如何实现呢? 以下是实现代码: wxml代码: <view class=&quo ...

  7. 微信小程序页面添加背景图,图片全屏显示

    前言 微信的wxss里面不允许使用使用 background: url(),只能另外找方法进行背景图片显示. 方法 1.wxss页面里面设置页面的全屏宽高,以及view添加宽高 page{height ...

  8. android从九宫格全屏预览,仿微信朋友圈展示图片的九宫格图片展示控件,支持点击图片全屏预览大图...

    AssNineGridView 仿微信朋友圈展示图片的九宫格图片展示控件,支持点击图片全屏预览大图(可自定义). 写在前面 这是一个九宫格控件,本来是很久之前就写好了,现在才开源出来,也是看了很多优秀 ...

  9. css 全屏显示一张图片_css 如何让图片全屏的问题

    1:一个很小的条状图,通过repeat后,形成一个很规则的大图背景. 但是css3出现以后,这个情况被改善了.background-size 属性可以让我们之前的希望成真.而且这个属性在firefox ...

最新文章

  1. 蓝桥杯 兰顿蚂蚁(模拟)
  2. python增量赋值是什么_python学习记录20190122_增量赋值
  3. SQL点滴12—SQL Server备份还原数据库中的小把戏
  4. font-size用VW来写的方法
  5. 预告片:裸指关节SOA
  6. 第五章数理统计--样本和抽样分布
  7. 文件隐藏工具Funter for Mac使用方法
  8. 20190218每日一句
  9. jersey2 java_无废话Jersey构建RESTful服务之WebService系统教程 --2 [JAVA对象转换成XML输出]...
  10. android之LitePal 3.0 的基本使用
  11. 安卓开发——做一个能定时唤起其他APP的闹钟程序
  12. 平凡之路_2022年
  13. IBM笔记本使用法语输入法,如何键入法语特殊字符?
  14. 【adb】cmd命令行输入adb时始终提示adb为非内部命令
  15. 实验3-11 求一元二次方程的根
  16. 数据结构-循环双链表
  17. .xb文件腾讯云备份恢复
  18. 深入浅出SQL读书笔记
  19. 【09年特长生第四题】开发区规划
  20. Syslog-ng3.5 mysql 日志服务器

热门文章

  1. Python项目实战-Gensim手动实现LDA算法玩转情感分析
  2. 互联网“反垄断”:十年争议,今朝枪响
  3. 水果店圈子:水果店生意越来越难做吗,小区门口的水果店生意怎么样
  4. poen实现 把小写字母换成大写字母
  5. C/C++ 实现目录监视器(详解版)
  6. 周末的好时光用来做什么
  7. C++高级编程 -知识点笔记(自用)
  8. PCM及一些音频数据知识--学习笔记
  9. 如何利用计算机打德文,电脑安装德语输入法的详细步骤
  10. 关于集成墙板商家营销的十个禁忌,你都懂吗?