PHP将html内容转换为image图片

2021-07-02 12:04

阅读:685

标签:cti   gety   rto   from   each   [1]   ges   param   logs   

/**
	 * 将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 

and

arrays $h1_start = array(); $h1_end = array(); //Clear

and

attributes $str = preg_replace("/

]*>/", "

", $str); $str = preg_replace("/]*>/", "

", $str); $str = preg_replace("/

\s*/", "", $str); //Declare arrays $img_pos = array(); $imgs = array(); //If we have images in the HTML if (preg_match_all("/]*src=\"([^\"]*)\"[^>]*>/", $str, $m)) { //Delete the 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("

", "", $nstr); $nstr = str_replace("

", "", $nstr); $nstr = preg_replace("/
]*>/", str_repeat(chr(1), 2), $nstr); $nstr = preg_replace("/
]*>/", str_repeat(chr(1), 2), $nstr); $nstr = preg_replace("/]*>/", str_repeat(chr(1), 2), $nstr); $nstr = preg_replace("/

]*>/", str_repeat(chr(1), 4), $nstr); $nstr = preg_replace("/]*>/", str_repeat(chr(1), 4), $nstr); $nstr = preg_replace("/


]*>/", 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

or

s while (strpos($str, "

") != false || strpos($str, "

") != false) { while (strpos($str, "

") !== false) { $p = strpos($str, "

"); $h1_start[] = $p; $str = substr($str, 0, $p) . substr($str, $p + strlen("

")); } while (strpos($str, "

") !== false) { $p = strpos($str, ""); $h1_end[] = $p; $str = substr($str, 0, $p) . substr($str, $p + strlen("")); } }

  

PHP将html内容转换为image图片

标签:cti   gety   rto   from   each   [1]   ges   param   logs   

原文地址:http://www.cnblogs.com/blog-dyn/p/7127748.html


评论


亲,登录后才可以留言!