PHP将html内容转换为image图片
/**
* 将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图片的更多相关文章
- 将PPT文件内容转换为图片放在Email邮件正文中发送
通过Email推送统计报告.一般除了要求将PPT报告文件作为附件发给用户,同时希望将报告内容在邮件中直观展示. 一份统计报告中经常包含柱状图.饼图.好看的图表,这些信息要直接在Email中展示比较复杂 ...
- [MVC]如何删除文章内容中的图片
1.实现代码 if (!string.IsNullOrWhiteSpace(entity.Content)) { var immgList = TextHelper.GetImgUrlList(ent ...
- 如何把PDF文件转换为JPG图片
大家有遇到需要将PDF文件转换为JPG的情况么,文档资料里面经常会有一些图片类型的内容,有的时候我们需要获取这些图片内容,但是PDF文件又是不可编辑的,那如何把PDF文件转换为JPG图片呢,跟着小编一 ...
- C# 将容器内容转成图片导出
/// 将容器内容转成图片导出,这里的controller就是this /// </summary> private void OutTheContro ...
- 将HTML5 Canvas的内容保存为图片借助toDataURL实现
将HTML5 Canvas的内容保存为图片主要思想是借助Canvas自己的API - toDataURL()来实现,具体实现如下,感兴趣的朋友可以参考下哈,希望对你有所帮助 <html> ...
- 将XML文件中的内容转换为Json对象
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xml;u ...
- 用JavaScript将Canvas内容转化成图片的方法
上周我们花了半天时间开发下一个准备放进Mozilla Marketplace的应用.有一个应用现在非常的火热,那就是Instagram,Facebook花了100万美元收购了它.我们也想有100万美元 ...
- Delphi RichEdit的内容保存为图片
uses RichEdit; {将RichEdit1的内容保存为图片,此函数也适合于RxRichEdit,即RichEdit: TRxRichEdit}procedure RichEditToCanv ...
- BMP图片转换为JPEG图片
原文:BMP图片转换为JPEG图片 昨天在家学习,发现很多人把BMP图片转换为其它图片格式,有些人写得简单,有些人写得复杂. Insus.NET在想,一直在做文件上传,下载,或是图片剪切,都有进行过文 ...
随机推荐
- Python模块之re
re模块 准备: flags有很多可选值: re.I(IGNORECASE)忽略大小写,括号内是完整的写法 re.M(MULTILINE)多行模式,改变^和$的行为 re.S(DOTALL)点可以匹配 ...
- 题解 P1016 旅行家的预算
题目传送门(以纪念调了两个半小时的单调队列) emmm这题单调队列可海星... 因为每个点有油量无限的,但是油箱容量是无限的(正好反的一道题 SP348 EXPEDI - Expedition) 所以 ...
- Auto yes to the License Agreement on sudo apt-get -y install oracle-java7-installer
参考一 参考二 我自己的做法是: && add-apt-repository ppa:webupd8team/java \ && apt-get update \ &a ...
- C. Epidemic in Monstropolis
http://codeforces.com/contest/733/problem/C 一道很恶心的模拟题. 注意到如果能凑成b[1],那么a的前缀和一定是有一个满足是b[1]的,因为,如果跳过了一些 ...
- columns分栏与flex弹性盒模型
columns 分栏 值:column-width:设置每列的宽度 column-count:设置列数 例:columns{200px 3} 列数和宽度固定 co ...
- JavaScript中登录名的正则表达式及解析(0基础)
简言 在JavaScript中,经常会用到正则表达式来进行模式匹配.例如,登录名验证,密码强度验证,字符串查找或替换等操作.现在就开始吧,零基础写出你的第一个正则表达式! 在做用户注册时,都会用到登录 ...
- Map-HashMap-LinkedHashMap-Map.Entry-Collections-可变参数
一.Map 接口(java.util) 定义:public interface Map<K,V> 介绍: (1)Map是一个接口,含有两个泛型,创建子类对象的时候,需要传递两个泛型 ...
- linux mount命令详解(iso文件挂载)
挂载命令: mount [-t vfstype] [-o options] device dir mount 是挂载命令 -t + 类型 -o + 属性 device iso的文件 dir 挂 ...
- Android RecyclerView使用GridLayoutManager间距设置
使用RecyclerView设置间距,需要重写RecyclerView.ItemDecoration这个类.有如下的效果图需要实现,间距只有中间的格子和底部的格式之间有. Paste_Image. ...
- 《Ionic 2 实例开发》发布
Ionic 2系列教程集结成册,在百度阅读上架发布,名为<Ionic 2实例开发>(点击书名将打开地址:http://yuedu.baidu.com/ebook/ba1bca51e4189 ...