Rumah > Artikel > pembangunan bahagian belakang > 经验分享 PHP显示图片_PHP教程
首先准备几张标题图片,把它们命名为:
img_1.jpg, img_2.jpg,img_3.jpg,img_4.jpg,img_5.jpg,img_6.jpg。
当然你还可以使用其它类型的图片,例如:gif。然后,把这些图片上传到某个文件夹,例如:"/images/random"。
接下来使用下面的PHP显示图片代码就可以了:
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span class="tag-name">img</span><span> </span><span class="attribute">src</span><span>="/images/random/img_<br></span><span class="tag"></span><span class="tag-name">php</span><span> echo rand(1,6)</span><span class="tag">?></span><span> </span></span></span></li> <li> <span>.jpg" </span><span class="attribute">border</span><span>=</span><span class="attribute-value">"0"</span><span> </span><span class="tag">/></span><span> </span> </li> <li class="alt"> <span class="comments"><!-- 注意: "/images/random"<br />保存随机图片的文件夹路径,你应该<br />使用实际路径替换--></span><span> </span> </li> </ol>
PHP显示图片是不是很简单?
这种方法稍微有点麻烦的地方是你要把图片根据上面的规则重新命名。如果你不想重新命名图片,你还可以使用下面的方法,但是要多写几行PHP代码。 :-)
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> ?php </span></span></span></li> <li> <span>$</span><span class="attribute">dir</span><span> = $_SERVER['DOCUMENT_ROOT']<br> . "images/random"; </span> </li> <li class="alt"><span>chdir ($dir); </span></li> <li> <span>$</span><span class="attribute">images</span><span> = </span><span class="attribute-value">glob</span><span>("*.{gif,png,jpg}<br>", GLOB_BRACE); </span> </li> <li class="alt"> <span>$</span><span class="attribute">file</span><span> = $images[array_rand($images)]; </span> </li> <li> <span class="tag">?></span><span> </span> </li> <li class="alt"><span class="tag"><span> </span><span class="tag-name">img</span><span> </span><span class="attribute">src</span><span>=</span><span class="attribute-value">"<span>/<br>images/random/$file"); </span><span class="tag">?></span><span>"<br> </span><span class="attribute">border</span><span>=</span><span class="attribute-value">"0"</span><span> </span><span class="tag">/></span><span> </span></span></span></li> </ol>
调用其他网站(网络相册)上的图片
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> ? </span></span></span></li> <li> <span>$</span><span class="attribute">id</span><span>=</span><span class="attribute-value">rand</span><span>(1,5); </span> </li> <li class="alt"><span>$image[1]='http://farm.static.flickr.com/1.png'; </span></li> <li><span>$image[2]='http://farm.static.flickr.com/2.png'; </span></li> <li class="alt"><span>$image[3]='http://farm.static.flickr.com/3.png'; </span></li> <li><span>$image[4]='http://farm.static.flickr.com/4.png'; </span></li> <li class="alt"><span>$image[5]='http://farm.static.flickr.com/5.png'; </span></li> <li><span>header("location:$image[$id]"); </span></li> <li class="alt"> <span class="tag">?></span><span> </span> </li> </ol>
以上的代码范例就是PHP显示图片的相关实现方法,希望对大家有些帮助。