Home >Backend Development >PHP Tutorial >Experience sharing PHP display pictures_PHP tutorial
First prepare a few title images and name them:
img_1.jpg, img_2.jpg, img_3.jpg, img_4. jpg,img_5.jpg,img_6.jpg.
Of course you can also use other types of images, such as: gif. Then, upload these images to a folder, for example: "/images/random".
Next, just use the following PHP code to display images:
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><</span><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></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>
Isn’t it very simple to display images in PHP?
The slightly troublesome thing about this method is that you have to rename the image according to the above rules. If you don't want to rename the image, you can also use the following method, but it requires writing a few more lines of PHP code. :-)
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><</span><span> ?php </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></span></li> <li class="alt"> <span class="tag"><</span><span> </span><span class="tag-name">img</span><span> </span><span class="attribute">src</span><span>=</span><span class="attribute-value">"< ?php echo ("</span><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> </li> </ol>
Calling pictures on other websites (online albums)
<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></span></li></ol>
The above code examples are related implementation methods for displaying pictures in PHP. Hope it helps everyone.