Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-12-2005, 06:35 PM   PM User | #1
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
Random GD Image in Directory

Well since I lost all my PHP data in my drive E which dissapeared I have been starting to write different small scripts to pass time since I have nothing of mine on this computer anymore.

This script will display a random thumbnail image from the directory the script is in.

PHP Code:
<?php

if ($handle opendir('.')) {
  
$images = array();
  while (
false !== ($file readdir($handle))) {
      if (
preg_match("/.(jpg|jpeg|gif|png)/i" $file)) {
         
$images[] = $file;
      }
  }
  
closedir($handle);
}
  
$file array_rand($iamges);
  if(
preg_match("/.png/i"$file)) { $quality 100; } else { $quality 75; } 
  
$max_width 200;
  
$max_height 200
  
header('Content-type: image/jpeg'); 
    list(
$width$height) = getimagesize($file); 
  
$ratio = ($width $height) ? $max_width $width $max_height $height;  
  if(
$width $max_width || $height $max_height) {  
    
$new_width $width $ratio;  
    
$new_height $height $ratio;  
  } else { 
    
$new_width $width;  
    
$new_height $height
  }  
  
$image_p imagecreatetruecolor($new_width$new_height); 
  
$image imagecreatefromjpeg($file);  
  
imagecopyresampled($image_p$image0000$new_width$new_height$width$height); 
  
imagejpeg($image_pnull$quality);  
  
imagedestroy($image_p);

?>
If you want to display a full random image its a matter of getting rid of the new widths and simply passing the old width and height in $image_p and imagecopyresampled()
Element is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:26 AM.


Advertisement
Log in to turn off these ads.