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在想,一直在做文件上传,下载,或是图片剪切,都有进行过文 ...
随机推荐
- mysql读取不同位置配置文件顺序
读取顺序为: /etc/my.cnf basedir/my.cnf datadir/my.cnf --defaults-extra-file #在读取全局配置文件之后,读取用户配置文件(~/.m ...
- Hibernate5 四种数据源配置
1.需要知道的: DBCP在hibernate3中以及不再被支持了,由于作者提出过bug,后续版本没有加入对其的支持. 推荐使用proxool,负面新闻最少的连接池 下面的图显示了,Hibernate ...
- CF438D The Child and Sequence 线段树
给定数列,区间查询和,区间取模,单点修改. n,m小于10^5 ...当区间最值小于模数时,就直接返回就好啦~ #include<cstdio> #include<iostream& ...
- Codeforces Round #563 (Div. 2) C. Ehab and a Special Coloring Problem
链接:https://codeforces.com/contest/1174/problem/C 题意: You're given an integer nn. For every integer i ...
- Hive进阶_内置函数
Hive数学函数 round : 四舍五入 ceil : 向下取整 floor : 向上取整 ),),),),); Hive字符函数 select lower('Hello World'), uppe ...
- 用canvas裁剪图片
var selectObj = null; function ImageCrop(canvasId, imageSource, x, y, width, height) { var canvas = ...
- MyBatis配置文件之properties属性
MyBatis提供3个方式使用properties: 1.property子元素. 2.properties文件. 3.程序代码传递. properties属性系给系统配置一些运行参数,一般放在XML ...
- 客户端rsyslog配置文件详解
客户端rsyslog配置文件详解 最近再开发一个rsyslog的接收服务端,支持udp,tcp和tls三种协议.所以去仔细研究了一下rsyslog.conf的配置文件,下面来详细说一下. 因为我这儿重 ...
- 如何在CSS中解决长英文单词的页面显示问题?CSS3
简言 在页面排版中,经常遇到长英文单词溢出段落容器的情况,如何解决该问题?现编制如下对比演示程序: 演示程序 42du.cn-在线演示程序 部分html代码 <div class="b ...
- mui实现图片更换(暂未上传)
页面中有默认的图片,触发type为file的input时,更换图片,这个是mui移动端的项目,算了,不多说,开码 首先,先在html页面中设置样式,样式我就不给了,贴个布局 <div class ...