PHP將html內容轉換為image圖片

來源:互聯網
上載者:User

標籤: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 <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 HTMLif (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 imagesforeach ($m[1] as $i => $src) {$imgs[] = $src;}//Get image resource of the source//according to its extension and save it in arrayforeach ($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>swhile (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圖片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.