{"id":174,"date":"2008-02-06T03:17:16","date_gmt":"2008-02-05T19:17:16","guid":{"rendered":"http:\/\/learn-house.idv.tw\/?p=174"},"modified":"2010-01-17T18:52:32","modified_gmt":"2010-01-17T10:52:32","slug":"php%e5%9c%a8html%e4%b8%ad%e9%a1%af%e7%a4%bajpgraph","status":"publish","type":"post","link":"https:\/\/learn-house.idv.tw\/?p=174","title":{"rendered":"[PHP]\u5728HTML\u4e2d\u986f\u793aJpGraph"},"content":{"rendered":"<p>\u4ee5\u4e0b\u662fDocument\u6587\u4ef6\u8aaa\u7684\u4ee5\u5716\u7247\u7684html tag &lt;IMG&gt;\u65b9\u5f0f\u986f\u793a<\/p>\n<h2><a name=\"4_2\" title=\"4_2\"><\/a>4.2 How to generate images with PHP<\/h2>\n<p> As a general rule each PHP script which generates an image must be  specified in a separate file which is then called in an HTML &lt;IMG&gt; tag.  For example, the following HTML excerpt includes the image generated by  the PHP script in &quot;fig1.php&quot;.<\/p>\n<div class=\"phpscript\"><code><font color=\"#000000\"> <font color=\"#0000bb\"> &nbsp;<\/font><font color=\"#007700\">&lt;<\/font><font color=\"#0000bb\">img&nbsp;src<\/font><font color=\"#007700\">=<\/font><font color=\"#dd0000\">&quot;fig1.php&quot;&nbsp;<\/font><font color=\"#0000bb\"> border<\/font><font color=\"#007700\">=<\/font><font color=\"#0000bb\">0&nbsp;align<\/font><font color=\"#007700\">=<\/font><font color=\"#0000bb\">center&nbsp;width<\/font><font color=\"#007700\"> =<\/font><font color=\"#0000bb\">300&nbsp;height<\/font><font color=\"#007700\">=<\/font><font color=\"#0000bb\">200<\/font><font color=\"#007700\">&gt;<\/font><font color=\"#0000bb\"> <\/font><\/font><\/code><\/div>\n<p> Strictly speaking the &quot;align&quot;, &quot;width&quot; and &quot;height&quot; are not  necessary but helps the browser position the image correctly before the  image has been fully sent back to the browser.<\/p>\n<p> The library will automatically generate the necessary headers to be  sent back to the browser so that it correctly recognize the data stream  received as an image of either PNG\/GIF\/JPEG format. The browser can  then correctly decode the image<\/p>\n<p> Observe that you<strong> can&#39;t<\/strong> return anything else than an  image from the image script. By definition each HTML page (or more  correctly each HTTP stream) can only consist of one mime type which is  determined by the header for that particular stream.<\/p>\n<p> A common mistake is to have a space in the beginning of the image  script which the HTTP server will send back to the browser. The browser  now assumes that the data coming back from this script is text since it  hasn&#39;t received an explicit header. When then the image headers get  sent back to the browser to forewarn the browser of the forthcoming  image the browser will not like that as it has already assumed the data  stream was a text stream. The browser will then give the infamous  &quot;Headers already sent error&quot;.<\/p>\n<p> To include several images together with text on a page you need to  have a parent page with several &lt;IMG&gt; tags which each refers to an  image script (or the same image script with GET\/POST data).<\/p>\n<p>\u6309\u7167doc\u7684\u4f5c\u6cd5\uff0c\u6bcf\u8981\u5728html\u7e6a\u88fd\u4e00\u5f35\u5716\u8868\uff0c\u5c31\u7528\u4e00\u500b&lt;IMG&gt;\u6a19\u7c64\u6307\u5b9a\u8a72\u7a0b\u5f0f<\/p>\n<p>\u4f46\u6211\u8a66\u4e86\u5e7e\u500b\u4e00\u6a23\u4e5f\u7121\u6cd5\u986f\u793a\uff0c\u56e0\u6b64\u5c31\u4fee\u6539\u4e86\u4e00\u4e0b\uff0c\u6539\u8b93\u4ed6\u5148\u8f38\u51fapng\u7684\u5716\u7247\u6a94\uff0c\u518d\u7528&lt;IMG&gt;<\/p>\n<p><strong>\u9996\u5148\u5148\u4fee\u6539\u4e4b\u524d\u7de8\u5beb\u7684PHP script\uff1a<\/strong><\/p>\n<p>\u5c07\u539f\u672c\u7684$graph-&gt;StrokeCSIM();&nbsp; \/\/\u5f9eHTML\u4e2d\u76f4\u63a5\u8f38\u51fa\u5716\u7247<\/p>\n<p>\u6539\u6210<br \/>$graph_name = &quot;\u8def\u5f91\/\u6a94\u540d.png&quot;;<br \/>$graph-&gt;Stroke($graph_name);<\/p>\n<p><strong>\u518d\u5230\u8981\u986f\u793a\u7684html\u9801\u9762\u4e2d\u52a0\u5165&lt;IMG&gt;\uff1a<\/strong><\/p>\n<p>\u518d\u52a0\u5165&lt;IMG&gt;\u524d\u8981\u5148\u628a\u652f\u524d\u7de8\u5beb\u7684PHP script\u5148include\u9032\u4f86 <\/p>\n<p>&lt;img src=&quot;images\/test.png&quot; width=&quot;600&quot; height=&quot;850&quot; \/&gt;&nbsp;<\/p>\n<p>\u5bec\u5ea6\u8207\u9ad8\u5ea6\u8981\u8ddf\u4f60\u5728\u7e6a\u88fd\u5716\u8868\u7684PHP script\u6307\u5b9a\u7684\u5927\u5c0f\u4e00\u6a23&nbsp;<\/p>\n<p><font color=\"#008000\"><strong>Tips: <\/strong>\u5728\u958b\u59cbhtml\u4e4b\u524d\u8981\u5148\u628a\u5beb\u597d\u7684\u7e6a\u5716\u7684PHP script\u5148include\u9032\u4f86\uff0c\u800c\u4e14\u6700\u597d\u7528require_once(&quot;XXXX.php&quot;)\u7684\u65b9\u5f0f\uff0c\u907f\u514d\u91cd\u8907include<\/font><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee5\u4e0b\u662fDocument\u6587\u4ef6\u8aaa\u7684\u4ee5\u5716\u7247\u7684html tag &lt;IMG&gt;\u65b9\u5f0f\u986f\u793a 4.2 How to  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-174","post","type-post","status-publish","format-standard","hentry","category-5"],"_links":{"self":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/174"}],"collection":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=174"}],"version-history":[{"count":0,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/174\/revisions"}],"wp:attachment":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=174"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}