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在想,一直在做文件上传,下载,或是图片剪切,都有进行过文 ...
随机推荐
- HDU - 3966-Aragorn' Story(树链剖分+线段树)
链接:https://vjudge.net/problem/HDU-3966 题意: Our protagonist is the handsome human prince Aragorn come ...
- bzoj1014: [JSOI2008]火星人prefix splay+hash
我写的代码好像自古以来就是bzoj不友好型的 本地跑的比std快,但是交上去巧妙被卡 答案...应该是对的,拍了好久了 #include <bits/stdc++.h> #define M ...
- csu 1554: SG Value 思维题
http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1554 这题在比赛的时候居然没想出来,然后发现居然是做过的题目的变种!!!! 先不考虑插入操作, ...
- Linux用脚本守护进程
while true; do server=`ps -aux | grep tomcat | grep -v grep` if [ ! "$server" ]; then echo ...
- DHTMLX 使用汇总
1.dhtmlxGrid 底部总出现滚动条 ------------------------------------------ 发现使用DHTMLXGRID时 GRID 底边总有 滚动条 测试 ...
- Android自定义组件系列【17】——教你如何高仿微信录音Toast
一.Toast介绍 平时我们在Android开发中会经常用到一个叫Toast的东西,官方解释如下 A toast is a view containing a quick little message ...
- 从零开始的Lua宅[1]:编译Lua解释器
Lua是一门神奇的脚本语言,游戏宅必备,懒人必备.Lua差多不是学起来用起来最简单的语言了,以至于简单到自身就是文档,自身就是配置文件.但是Lua的运行效率却是众多脚本中非常高的,据说仅次于V8爹下的 ...
- JFinal-美女图爬虫-一个不正经的爬虫代码
去年我做了一个项目,大量使用爬虫抓取数据,使用JFinal+JSoup组合,抓取数据,数据清洗筛选,最终保存到数据库里,结构化. 今天,我发布一个不正经的爬虫项目,如果你对JSoup做爬虫感兴趣,可以 ...
- 如何在cmd查看文件内容的md5值
在cmd下进入 要查看的文件目录 默认目录是c,切换到其他盘符例如: C:\D: 就会切换到D盘 D:\ 然后输入命令 certutil -hashfile 文件名称.文件类型 MD5 如 cert ...
- ftp错误
ftp 550 检查是否目录,文件确定存在. 服务器列表是要设置unix列表模式.