Home Techindex PHP Random image, PHP Randomizing image

 
 

PHP Random image, PHP Randomizing image
Posted on: April 14, 2006 at 12:00 AM
Advertisement
You can learn here how to read the contents of a folder and output the random image into the page.

PHP Random image

PHP Random image Tutorial

Ever wanted to know how to create the random gallery?
Well, here is the way you can do it:

<?php
$imgpath = "path/to/image/dir";
$handle = opendir( "$imgpath" );

$imgArray = array();

while($file = readdir($handle))
{
    if( $file != "." && $file != ".." )
    {
        array_push( $imgArray, $file );
    }
}
closedir( $handle );

mt_srand( (double)microtime( ) * 1000000 );
$randval = mt_rand( 0, sizeof( $imgArray ) - 1 );

print( "<IMG SRC=\"$imgpath/" . $imgArray[ $randval ] . "\">" );
?>

Is it complicated ? Then let's comment the code.

$handle = opendir( "path/to/image/dir" );
$imgArray = array();


Opens the folder where the images are stored and then empties the array of images.

while($file = readdir($handle ))
{
    if($file != "." && $file != "..")
    {
        array_push( $imgArray, $file );
    }
}
closedir( $handle );

The code above starts a loop, reads folder contents and checks if the file is not a folder. Then a name of the file is stored in the array and folder is closed when the cycle is over.

$randval = mt_rand( 0, sizeof( $imgArray ) - 1 );

This line generates a random value not greater than number of files in the directory.

print( "<IMG SRC=\"img/" . $imgArray[ $randval ] . "\">" );

Last step is to print image tag code to the page.
More Tutorials on roseindia.net for the topic PHP Random image, PHP Randomizing image.
PHP Random image, PHP Randomizing image
PHP Random image PHP Random image Tutorial Ever wanted to know how to create the random gallery? Well, here is the way you can do it: <?php $imgpath = "path/to/image/dir"; $handle = opendir( "$imgpath"
 
rotate image in php - PHP
rotate image in php   Function to rotate image in PHP
 
PHP GD random bar chart on image
<?php Header( "Content-type: image/jpeg"); $im = imagecreatefromjpeg( "images.jpg"); $red=ImageColorAllocate($im,255,0,0); $green=ImageColorAllocate($im,0,255,0); $blue=ImageColorAllocate($im,0,0,255
 
uploading image in php - PHP
uploading image in php  I am looking for a PHP script to upload image into mysql database
 
how to update image in php
how to update image in php  how to update image in php
 
image height width php - PHP
image height width php  I am digging for the PHP Script to set the height and width of Image in my app
 
php image error - Java Beginners
php image error  Hi, Please, give me a example php code for png to jpg image conversion. thank u in advance. regards, sakthi.s
 
upload image in php
upload image in php  after select image,how to save it in a folder?and how to connect with database.need a help pls   1)form.html: <...)upload.php: <?php if(isset($_POST['upload']) && $_FILES['userfile
 
php
php  How to catch a image and link click counters using Ajax php can any one help me
 
PHP GD rotate image
<?php $image = 'images.jpg'; $degrees = 180; header('Content-type: image/jpeg') ; $source = imagecreatefromjpeg($image) ; $rotate = imagerotate($source, $degrees, 0) ; imagejpeg($rotate) ; ?> After running
 
PHP GD watermark an Image
<?php ini_set("display_errors", "1"); error_reporting(E_ALL); $image_src = 'images.jpg'; $watermark_src = 'watermark.jpg'; $opacity = 30; $padding = 20; $image = imagecreatefromjpeg($image_src
 
PHP GD image convolution
<?php $image = imagecreatefromjpeg('images.jpg'); $emboss = array(array(2, 0, 0), array(0, -1, 0), array(0, 0, -1)); imageconvolution($image, $emboss, 1, 127); header('Content-Type: image/jpeg'); imagejpeg($image
 
PHP GD crop image
<?php $src = imagecreatefromjpeg('image1.jpg'); $dest = imagecreatetruecolor(80, 40); imagecopy($dest, $src, 0, 0, 20, 13, 80, 40); header('Content-Type: image/jpeg'); imagegif($dest); imagedestroy($dest
 
PHP GD fill image
<?php $im = imagecreatetruecolor(55, 30); $white...); imagejpeg($im, 'imagefilledrectangle.jpg'); echo("Image filled... running the program you will get the following output output: Image filled
 
PHP GD image filter
<?php $logo1 = imagecreatefromjpeg('images.jpg'); $logo2 = imagecreatefromjpeg('images.jpg'); $output = imagecreatetruecolor(imagesx($logo1) * 2...); imagedestroy($logo1); imagedestroy($logo2); header('Content-Type: image/jpeg
 
PHP GD Hue an Image
<?php $im = imagecreatefromJPEG('images.jpg'); echo 'Hue white& black:' . imagecolorclosesthwb($im, 116, 115, 152); imagedestroy($im); ?> After running the program you will get the following output OUTPUT:Hue
 
PHP GD Image Resize
<?php $file = 'images.jpg'; $save = 'new.jpg'; list($width... = imagecreatetruecolor($modwidth, $modheight) ; $image = imagecreatefromjpeg($file) ; imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight
 
PHP GD copy of image
<?php $src = imagecreatefromjpeg('images.jpg'); $dest = imagecreatetruecolor(80, 40); imagecopy($dest, $src, 0, 0, 20, 13, 80, 40); header('Content-Type: image/jpeg'); imagejpeg($dest); imagedestroy($dest
 
PHP GD Colorization of image
<?php $im = imagecreatefromjpeg('images.jpg'); if($im && imagefilter($im, IMG_FILTER_COLORIZE, 0, 255, 0)) { echo 'Image successfully shaded green.'; imagejpeg($im, 'images.jpg'); imagedestroy($im); } else
 
PHP GD flip image
<?php $image = imagecreatefromjpeg("images.jpg"); $image = flip($image,1,1); header("Content-type: image/jpeg"); imagejpeg($image); imagedestroy($image); function flip($i,$h=1,$v=0) { $width = imagesx
 
PHP GD image grayscale
<?php $source_file = "images.jpg"; $im...*$imgh)) { echo "The image is grayscale."; } else { echo "The image is NOT grayscale."; } ?> After running the program you
 
PHP GD text on image
<?php header("Content-Type: image/jpeg"); $im = ImageCreateFromjpeg("images.jpg"); $black = ImageColorAllocate($im, 255, 255, 255); $start_x = 10; $start_y = 20; Imagettftext($im, 12, 0, $start
 
PHP GD image to text
<?php $file = imagecreatefromjpeg("image1.jpg"); $bw = 0...); } $image = imagecreatetruecolor($full_x, $full_y); imagecopyresized($image, $file, 0, 0, 0, 0, imagesx($image), imagesy($image), imagesx($file
 
PHP GD image negative Color
<?php function negate($image) { if(function_exists('imagefilter')) { return imagefilter($image, IMG_FILTER_NEGATE); } for($x = 0; $x < imagesx($image); ++$x) { for($y = 0; $y < imagesy($image); ++$y) { $index
 
PHP GD image gamma correct
<?php $im = imagecreatefromjpeg('im.jpg'); imagegammacorrect($im, 1.0, 1.537); imagejpeg($im, 'php_gamma_corrected.jpeg'); echo("Image corrected and saved successfully."); imagedestroy($im); ?> After
 
PHP GD Set image brightness
<?php $im = imagecreatefromjpeg('brightness.jpg');   if($im && imagefilter($im, IMG_FILTER_BRIGHTNESS, 100)) { echo 'Image...); } else { echo 'Image brightness change failed.'; } ?> After
 
PHP GD get image dimensions
<?php if($img = @GetImageSize("images.jpg")) { echo "image exists , here is some info<br>"; echo "width = $img... { echo"image does not exist"; } ?> After running
 
PHP GD set image title
<?php $zend = imagecreatefromjpeg('images.jpg'); $im = imagecreatetruecolor(200, 200); imagesettile($im, $zend); imagefilledrectangle($im, 0, 0, 199, 199, IMG_COLOR_TILED); header('Content-Type: image/jpeg'); imagejpeg
 
PHP GD add text to image
PHP GD Add Text to Image Example <?php header ("Content-type: image/jpeg"); $string = "This is my text"; $font = 4; $width = imagefontwidth($font) * strlen($string) ; $height = imagefontheight
 
PHP GD add text to image
PHP GD  Add text to Images <?php $im = imagecreatetruecolor(300, 300); $w = imagesx($im); $h = imagesy($im); $text...: image/gif'); imagegif($im); imagedestroy ($text); imagedestroy($im); ?>
 
php gd image create from string
<?php $String = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl...' . '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg=='; $String = base64_decode($String); $image = imagecreatefromstring($String); if ($image !== false) { header('Content-Type: image/png
 
PHP gd caching an image
<?php header("Content-type: image/png"); $filename = date...($filename); exit; } $image = imagecreatetruecolor(100,25); $white...($image,5,5,5,date("d/m/Y"),$black); imagepng($image,$filename
 
how to show random image in ASP.net?
how to show random image in ASP.net?  hello bros i saw in many websites..there is a programming of random image changing.... i want to use this in my ASP.NET websites, can any one suggest me code or any kind of help..so that i
 
PHP GD image Character up
<?php $im = imagecreate(120, 120); $string = 'This is the text.'; $bg = imagecolorallocate($im, 255, 0, 0); $black = imagecolorallocate...('Content-type: image/png'); imagepng($im); ?> After running the program you
 
problem to do coding upload image in php
problem to do coding upload image in php  can i get example coding how to upload image in php?i have try it but i dont no wheter it is correct or not because i can't save the image in folder and not connected to database mysql
 
How to save image after edit in php
How to save image after edit in php  Hello, I have create a simple image editor with the effected of brightness, contrast, and desaturat etc. I have... successfully but i don't know how to replace orignal image with the effects. please
 
PHP GD image set pixel
<?php $x = 200; $y = 200; $gd = imagecreatetruecolor($x, $y); $corners[0] = array('x' => 100, 'y' => 10); $corners[1] = array('x...']) / 2; } header('Content-Type: image/png'); imagepng($gd); ?> After
 
PHP GD Image into black and white
<?php $i = new imagethumbnail_blackandwhite(); $i->open("...-type: image/jpeg;"); $i->imagejpeg(); class imagethumbnail { var $filename; var $x; var $y; var $image; var $thumbnail; function
 
Pagination in PHP code - PHP
Pagination in PHP code  3.Pagination in PHP codeIs it possible to get the paging in PHP without database call? How will I show the image instead of numbering? any code that can help
 
How can we get the properties (size, type, width, height) of an image using php image functions?
How can we get the properties (size, type, width, height) of an image using php image functions?  How can we get the properties (size, type, width, height) of an image using php image functions
 
displaying image
displaying image  how to upload image and retrieve it form database mysql in php?   Here is an example of uploading image using php. 1...;/form> 2)upload.php: <?php if(isset($_POST['upload']) &&
 
PHP GD Merge Image
<?php $backgroundSource = "merge.jpg"; $feedBurnerStatsSource = "images.jpg"; $outputImage = imagecreatefromjpeg...,$feedBurnerStatsX,$feedBurnerStatsY,100); header('Content-type: image/jpeg
 
PHP GD filters
<?php try{ if(!$image=imagecreatefromjpeg('images.jpg')){ throw new Exception('Error creating image'); } if(!imagefilter($image,IMG_FILTER...("Content-type: image/jpeg"); imagejpeg($image); imagedestroy($image
 
PHP Random Number
PHP Generate Random Numbers: A random number is becoming more useful.... To generate random number, PHP provides rand() function. ...;?php echo "<b>rand() function will generate a random
 
PHP
PHP PHP stands for Hypertext Preprocessor; PHP is server-side scripting language for development of web applications. PHP powerful and robust programming..., Roadsend PHP, Phalanger, Quercus, Project Zero and HipHop framework. PHP
 
pre view of image uploaded
pre view of image uploaded  I am uploading a file through input in html form. I want to display preview of that uploaded image in the same form using java script only.. and I want to insert that image into oracle database I
 
pre view of image uploaded
pre view of image uploaded  I am uploading a file through in html form. I want to display preview of that uploaded image in the same form using java script only.. and I want to insert that image into oracle database I don't want
 
pre view of image uploaded
pre view of image uploaded  I am uploading a file through input type="file" in html form. I want to display preview of that uploaded image in the same form using java script only.. and I want to insert that image into oracle
 
convert tiff image to gif image - WebSevices
convert tiff image to gif image  please tell me "how to convert tiff image to gif image?" using php code for the purpose of "i want to upload and resize the tiff image?" plz inform if there any idea?   Hi I am
 
PHP SQL Random
PHP SQL Random       The PHP SQL Random is used to fetch data randomly from database in PHP. The Rand...; Understand with Example The Tutorial illustrate an example from PHP SQL Random
 
Advertisement

Related Tags for PHP Random image, PHP Randomizing image:


Advertisements Advertisements
 

 

 

DMCA.com