• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

PHP将html内容转换为image图片

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
/**
	 * 将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>"));
			}
		}
		

  


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
php没有开启Memcache扩展类时发布时间:2022-07-10
下一篇:
如何本地搭建php环境用来测试PHP程序教程!发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap