/**
* 将html内容转换为image图片
* @param $htmlcontent
* @param $toimagepath
* @author james.ou 2011-11-1
*/
public static function html2image($htmlcontent, $toimagepath, $toimagewidth = '400', $toimageheight = '300', $toimagetype = 'png') {
$str = $htmlcontent;
$str = strtolower($str);
//$str = mb_convert_encoding($str, "html-entities", "utf-8");
//Get the original HTML string
//Declare <h1> and </h1> arrays
$h1_start = array();
$h1_end = array();
//Clear <h1> and </h1> attributes
$str = preg_replace("/<h1[^>]*>/", "<h1>", $str);
$str = preg_replace("/<\/h1[^>]*>/", "</h1>", $str);
$str = preg_replace("/<h1>\s*<\/h1>/", "", $str); //Declare <img> arrays
$img_pos = array();
$imgs = array();
//If we have images in the HTML
if (preg_match_all("/<img[^>]*src=\"([^\"]*)\"[^>]*>/", $str, $m)) {
//Delete the <img> tag from the text
//since this is not plain text
//and save the position of the image
$nstr = $str;
$nstr = str_replace("\r\n", "", $nstr);
$nstr = str_replace("<h1>", "", $nstr);
$nstr = str_replace("</h1>", "", $nstr);
$nstr = preg_replace("/<br[^>]*>/", str_repeat(chr(1), 2), $nstr);
$nstr = preg_replace("/<div[^>]*>/", str_repeat(chr(1), 2), $nstr);
$nstr = preg_replace("/<\/div[^>]*>/", str_repeat(chr(1), 2), $nstr);
$nstr = preg_replace("/<p[^>]*>/", str_repeat(chr(1), 4), $nstr);
$nstr = preg_replace("/<\/p[^>]*>/", str_repeat(chr(1), 4), $nstr);
$nstr = preg_replace("/<hr[^>]*>/", str_repeat(chr(1), 8), $nstr); foreach ($m[0] as $i => $full) {
$img_pos[] = strpos($nstr, $full);
$str = str_replace($full, chr(1), $str);
}
//Save the sources of the images
foreach ($m[1] as $i => $src) {
$imgs[] = $src;
}
//Get image resource of the source
//according to its extension and save it in array
foreach ($imgs as $i => $image) {
$ext = end(explode(".", $image));
$im = null;
switch ($ext) {
case "gif":
$im = imagecreatefromgif($image);
break;
case "png":
$im = imagecreatefrompng($image);
break;
case "jpeg":
$im = imagecreatefromjpeg($image);
break;
}
$imgs[$i] = $im;
}
}
//If there is <h1> or </h1>s
while (strpos($str, "<h1>") != false || strpos($str, "</h1>") != false) {
while (strpos($str, "<h1>") !== false) {
$p = strpos($str, "<h1>");
$h1_start[] = $p;
$str = substr($str, 0, $p) . substr($str, $p + strlen("<h1>"));
}
while (strpos($str, "</h1>") !== false) {
$p = strpos($str, "</h1>");
$h1_end[] = $p;
$str = substr($str, 0, $p) . substr($str, $p + strlen("</h1>"));
}
}

  

PHP将html内容转换为image图片的更多相关文章

  1. 将PPT文件内容转换为图片放在Email邮件正文中发送

    通过Email推送统计报告.一般除了要求将PPT报告文件作为附件发给用户,同时希望将报告内容在邮件中直观展示. 一份统计报告中经常包含柱状图.饼图.好看的图表,这些信息要直接在Email中展示比较复杂 ...

  2. [MVC]如何删除文章内容中的图片

    1.实现代码 if (!string.IsNullOrWhiteSpace(entity.Content)) { var immgList = TextHelper.GetImgUrlList(ent ...

  3. 如何把PDF文件转换为JPG图片

    大家有遇到需要将PDF文件转换为JPG的情况么,文档资料里面经常会有一些图片类型的内容,有的时候我们需要获取这些图片内容,但是PDF文件又是不可编辑的,那如何把PDF文件转换为JPG图片呢,跟着小编一 ...

  4. C# 将容器内容转成图片导出

    ///   将容器内容转成图片导出,这里的controller就是this         /// </summary>         private void OutTheContro ...

  5. 将HTML5 Canvas的内容保存为图片借助toDataURL实现

    将HTML5 Canvas的内容保存为图片主要思想是借助Canvas自己的API - toDataURL()来实现,具体实现如下,感兴趣的朋友可以参考下哈,希望对你有所帮助 <html> ...

  6. 将XML文件中的内容转换为Json对象

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xml;u ...

  7. 用JavaScript将Canvas内容转化成图片的方法

    上周我们花了半天时间开发下一个准备放进Mozilla Marketplace的应用.有一个应用现在非常的火热,那就是Instagram,Facebook花了100万美元收购了它.我们也想有100万美元 ...

  8. Delphi RichEdit的内容保存为图片

    uses RichEdit; {将RichEdit1的内容保存为图片,此函数也适合于RxRichEdit,即RichEdit: TRxRichEdit}procedure RichEditToCanv ...

  9. BMP图片转换为JPEG图片

    原文:BMP图片转换为JPEG图片 昨天在家学习,发现很多人把BMP图片转换为其它图片格式,有些人写得简单,有些人写得复杂. Insus.NET在想,一直在做文件上传,下载,或是图片剪切,都有进行过文 ...

随机推荐

  1. Django中间件的执行流程

    Django中间件的执行流程. 请求到达中间件之后,先按照正序执行每个注册中间件的process_reques方法,process_request方法返回的值是None,就依次执行, 如果返回的值是H ...

  2. scrapy-redis 分布式哔哩哔哩网站用户爬虫

    scrapy里面,对每次请求的url都有一个指纹,这个指纹就是判断url是否被请求过的.默认是开启指纹即一个URL请求一次.如果我们使用分布式在多台机上面爬取数据,为了让爬虫的数据不重复,我们也需要一 ...

  3. ES6:string.raw浅析

    当前正学习ES6 ,遇到string.raw费心思,现将试验后的结果整理如下: 网上得来的试验: 语法 String.raw`templateStr`; String.raw(obj, ...subs ...

  4. var type = $('#<%=DropDownListRateType.ClientID %>').val();DropDownListRateType.ClientID是什么意思

    <%=DropDownListRateType.ClientID %>这个是C#绑定服务器控件在客户端ID, 比如你的DropDownListRateType你定义一个id,如果你用了模板 ...

  5. Echarts获取数据绘制图表

    这次是利用mui框架实现一个手机移动端的项目.基本的框架已经实现,主要来获取数据实现一个图表的展示. 首先引入插件:echarts.js <script src="../resourc ...

  6. MapReduce的编程思想(1)

    MapReduce的编程思想(1) MapReduce的过程(2) 1. MapReduce采用分而治之的思想,将数据处理拆分为主要的Map(映射)与Reduce(化简)两步,MapReduce操作数 ...

  7. ios 身份证照片识别信息

    一个近乎完整的可识别中国身份证信息的Demo就问问你霸气不

  8. Vuforia切换回识别场景后黑屏解决

    使用Vuforia SDK开发时,如果从其他非识别场景切换回识别场景,可能会出现黑屏问题. 解决方法是在切换到其他场景时,先将当前场景的Tracker信息全部Stop.代码如下: IEnumerato ...

  9. Ionic 2 中的创建一个闪视卡片组件

    闪视卡片是记忆信息的重要工具,它的使用可以追溯到19世纪.我们将要创建一个很酷的短暂动画来实现它.看起来像是这个样子的: 闪视卡片示例 Ionic 2 实例开发 新增章节将为你介绍如何在Ionic 2 ...

  10. 参考消息 Android 读报

    <参考消息>是新华通讯社主办,参考消息报社编辑出版的日报,创刊于1931年,历史长达80年.<参考消息>每天及时选载世界各国(地区)通讯社.报刊及因特网上的最新消息.评论的精华 ...