用PHP實(shí)現(xiàn)banner輪換的程序代碼
Jun 21, 2016 am 09:15 AM程序
??? 我們?cè)?jīng)訪問過這樣的網(wǎng)站,每次刷新banner都隨機(jī)的變化,在這篇文章中,我們將給大家介紹用PHP來實(shí)現(xiàn)這個(gè)功能。
步驟
程序?qū)崿F(xiàn)的原理是:調(diào)用一個(gè)數(shù)組,每個(gè)圖象對(duì)應(yīng)一個(gè)數(shù)組中的元素,然后我們?cè)O(shè)置隨機(jī)數(shù),只要隨機(jī)得到一個(gè)數(shù)據(jù)就可以顯示一副圖象了。
第一個(gè)步是我們來產(chǎn)生一個(gè)隨機(jī)數(shù)。每次刷新時(shí)我們都得到不同的隨機(jī)數(shù),具體代碼為:
srand((float) microtime() * 10000000);
之后我們?cè)O(shè)置一個(gè)數(shù)組為image,然后再設(shè)置5個(gè)數(shù)組元素,代碼如下:
$image[1]='/location/of/image1.jpg';
$image[2]='/location/of/image2.jpg';
$image[3]='/location/of/image3.jpg';
$image[4]='/location/of/image4.jpg';
$image[5]='/location/of/image5.jpg';
下面的代碼實(shí)現(xiàn)的功能是從數(shù)組中隨機(jī)選擇一個(gè)元素:
$rn = array_rand($image);
然后我們來顯示一個(gè)隨機(jī)的圖片:
echo '';
把上面的代碼組合起來就可以了。
srand((float) microtime() * 10000000);
$image[1]='/location/of/image1.jpg';
$image[2]='/location/of/image2.jpg';
$image[3]='/location/of/image3.jpg';
$image[4]='/location/of/image4.jpg';
$image[5]='/location/of/image5.jpg';
$rn = array_rand($image);
echo '';
以上的代碼是我們隨機(jī)顯示圖片的代碼,如果我們想使每個(gè)圖片再加上各自的連接地址那么我們把上述的代碼稍微改動(dòng)下就可以了!把上述的數(shù)組改為二維數(shù)組:
$image[1]['pic']='/location/of/image1.jpg';
$image[1]['link']='/location/of/link1.php';
相應(yīng)的顯示代碼為:
echo '';
echo '';
那么我們就可以完成我們標(biāo)題的功能了,隨機(jī)顯示圖片并且連接到不同的指定的地址:
srand((float) microtime() * 10000000);
$image[1]['pic']='/location/of/image1.jpg';
$image[1]['link']='/location/of/link1.php';
$image[2]['pic']='/location/of/image2.jpg';
$image[2]['link']='/location/of/link2.php';
$image[3]['pic']='/location/of/image3.jpg';
$image[3]['link']='/location/of/link3.php';
$image[4]['pic']='/location/of/image4.jpg';
$image[4]['link']='/location/of/link4.php';
$image[5]['pic']='/location/of/image5.jpg';
$image[5]['link']='/location/of/link5.php';
$rn = array_rand($image);
echo '';
echo '';
你可以把上面的代碼拷到你的網(wǎng)頁中去運(yùn)行了。祝你好運(yùn)

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

How to Convert PNG to JPG on Windows 11 On Windows 10 and 11, you can use Microsoft's built-in Paint app to quickly convert image files. To convert a PNG image to JPG on Windows 11, use the following steps: Open File Explorer and navigate to the PNG image you want to convert. Right-click the image and select Open With > Draw from the menu. Your photo or image opens in the Paint app. Note the file size at the bottom of the screen. To convert a file from PNG to JPG, click File and select Save As > JPEG Image from the menu. When the file resource

JPG is one of the most popular image file formats along with PNG. This is a lossy compressed image format ideal for websites. Therefore, many users save or convert images to JPG format. However, some users have stated that they are having issues opening JPG files in Windows 11 using the platform’s default Photos app. Therefore, these users cannot open, view and edit JPG format images in Windows 11. This is what one user said in a Microsoft forum post: When double-clicking a JPG file to open in Microsoft Photo, I receive the error The specified program cannot be found. This was purchased new about a month ago

In recent years, with the continuous development of Web technology, WebP, a new image format, has gradually entered the field of vision of programmers and Web developers. The advantages of WebP cannot be ignored: small file size, high quality, high browser support, etc. But in some cases, we may need to convert WebP images into some other formats, such as JPG. So, how to use PHP to convert WebP to JPG?

svg can be converted to jpg format by using image processing software, using online conversion tools, and using the Python image processing library. Detailed introduction: 1. Image processing software includes Adobe Illustrator, Inkscape and GIMP; 2. Online conversion tools include CloudConvert, Zamzar, Online Convert, etc.; 3. Python image processing library, etc.

JPG is a JPEG format file. The JPEG format is the most commonly used image file format, with the suffix ".jpg" or ".jpeg". JPG format standards are divided into three types: JPEG, progressive JPEG, and JPEG2000. It is a standard generally used for continuous-tone static image compression. It is lossy compression, which compresses data images in a small storage space while causing loss of image data. Damage, if the user has higher requirements for image quality, it is recommended to abandon this image file format, especially for some high-definition pictures.

JPG generally refers to the JPEG format, which is an image format saved using a lossy compression method. It is a standard for continuous tone static image compression. Its file suffix is ??".jpg" or ".jpeg". JPG files mainly use a joint coding method of predictive coding (DPCM), discrete cosine transform (DCT) and entropy coding to remove redundant images and color data; it can compress images in a small storage space, but to a certain extent will cause damage to the image data.

Suppose you need to rename the extension of a file from one extension to another, say jpg to png. It's easy, of course! But what if you have multiple files whose extensions need to be changed? Or worse, what if these multiple files are also located in multiple folders and subfolders, within a single folder? Well, for a normal person, this can be a nightmare. But for a geek, absolutely not. The question now is, are you a geek? Well, with the help of Geek Page, you definitely are! In this article, we explain how to easily rename the extension of all files within a folder, including subfolders of your choice, from one extension to another through a batch script method. Notice:

png and jpg are two common image file formats, which differ in the compression, quality and transparency of the image. First, let's look at how they compress. JPG (also called JPEG) uses a lossy compression algorithm to reduce file size by reducing detail and color information in the image. This compression method can significantly reduce file size, but will lose some detail, especially at high compression ratios. PNG (also called Portable Network Graphics) uses a lossless compression algorithm, which does not lose any image details and provides higher
