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在想,一直在做文件上传,下载,或是图片剪切,都有进行过文 ...
随机推荐
- 常用的js工具函数
JS选取DOM元素的方法注意:原生JS选取DOM元素比使用jQuery类库选取要快很多1.通过ID选取元素document.getElementById('myid');2.通过CLASS选取元素do ...
- virtualenv模块安装
依赖包管理模块(virtualenv) virtualenv是Python的虚拟环境,可以在同一台PC隔离不同的python开发环境.virtualenv可以创建一个隔离的python环境(沙盒).使 ...
- Minor GC ,Full GC 触发条件
Minor GC ,Full GC 触发条件 Minor GC触发条件:当Eden区满时,触发Minor GC. Full GC触发条件: (1)调用System.gc时,系统建议执行Full GC, ...
- Unity3d中3D Text对模型的穿透显示
昨晚,好友在电话里问我在Unity3d中使用3D Text,不想让其穿透模型显示,即想让场景中的3D Text与模型有正确的遮挡关系,怎么解? 今早谷歌上查了查,明白了原因,因为3D Text的默认材 ...
- 1979 第K个数
1979 第K个数 时间限制: 1 s 空间限制: 1000 KB 题目等级 : 黄金 Gold 题目描述 Description 给定一个长度为N(0<n<=10000) ...
- 关于Kendo UI 开发教程
Kendo UI 开发教程 jQuery UI 是一套 JavaScript 函式库,提供抽象化.可自订主题的 GUI 控制项与动画效果.基于 jQuery JavaScript 函式库,可用来建构互 ...
- BaseAdapter获取View之三重境界
在BaseAdapter获取View之前,BaseAdapter需要与数据源相关联. 可以使用构造方法: private List<ItemBean> baseListItems; pri ...
- Hadoop 2.7.0模拟分布式实验环境搭建[亲测]
实验目的: 本实验通过在PC电脑上同时运行3个虚拟机,一个为master节点,两个slave节点. 搭建环境: 主机:mac os 10.10 OS:CenOS 6.5 虚拟机:VMware ...
- Android商城开发系列(七)—— 使用RecyclerView展示首页数据
前面我们讲到了使用OkHttp请求网络和FastJson解析数据了,接下来我们就开始把获取到的数据通过数据适配器展示在页面上了.Adapter是用来帮助填充数据的中间桥梁,简单点说就是:将各种数据以合 ...
- 新萝卜家园GHOST WIN7系统32,64位官方版下载
来自系统妈:http://www.xitongma.com 新萝卜家园GHOST win7系统64位官方经典版 V2016年3月 系统概述 新萝卜家园ghost win7系统64位官方经典版加快“网上 ...