Jump to content

Random image / resize / subfolder help


fpsbrian

Recommended Posts

Here is a random image script that resizes images and looks though subfolders, the problem I'm having is it doesn't resize with IE... NEED HELP!

 

(I'm just starting to learn PHP I'm sure its something simple and I'm looking for something else)

 

 

 

<?php

  $size = getimagesize($source); // Get the image dimensions and mime type
  $wo = $size[0];  //width original
  $ho = $size[1];  //height original
  if ($wo > $ho) {
	$w=min($scale,$wo);
	$h=floor($ho/($wo/$w));
  }  else {
	$h=min($scale,$ho);
	$w=floor($wo/($ho/$h));
  }
  $resize = imagecreatetruecolor($w, $h); // Create a blank image

    // files to search for
    $extList = array();
  $extList['gif'] = 'image/gif';
  $extList['jpg'] = 'image/jpeg';
  $extList['jpeg'] = 'image/jpeg';
  $extList['png'] = 'image/png';

    // set to specific image if needed
    // null will result in a random image
    $img = null;
    
    // path to the directory you want to scan
    $directory = './';
    if (substr($directory,-1) != '/') {
  $directory = $directory.'/';
    }

    // if img is set, show it
    if (isset($_GET['img'])) {
  $imageInfo = pathinfo($_GET['img']);
  if (
	isset( $extList[ strtolower( $imageInfo['extension'] ) ] ) &&
	file_exists( $directory.$imageInfo['basename'] )
  ) {
	$img = $directory.$imageInfo['basename'];
  }
    } 
    // if img isnt set, grab a random
    else {
  //cycle through directory and subfolders
  $it = new RecursiveDirectoryIterator("$directory");
  foreach(new RecursiveIteratorIterator($it) as $file)
  {
	$file_info = pathinfo($file);
	if (
	    isset( $extList[ strtolower( $file_info['extension'] ) ] )
	) {
	    $items[] = $file;
	}
  }
  sort($items);
  // get the random image
  if (count($items) > 0) {
	$imageNumber = time() % count($items);
	$img = $directory.$items[$imageNumber];
  }
    }

    // if img isnt null, display it
    if ($img!=null) {
  $imageInfo = pathinfo($img);
  $contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ];
  header ($contentType);
  readfile($img);
    } 
    // else, try to create one or give error
    else {
  if ( function_exists('imagecreate') ) {
	header ("Content-type: image/png");
	$im = @imagecreate (100, 100)
	    or die ("Cannot initialize new GD image stream");
	$background_color = imagecolorallocate ($im, 255, 255, 255);
	$text_color = imagecolorallocate ($im, 0,0,0);
	imagestring ($im, 2, 5, 5,  "IMAGE ERROR", $text_color);
	imagepng ($im);
	imagedestroy($im);
  }
    }
?> 

Link to comment
https://forums.phpfreaks.com/topic/151946-random-image-resize-subfolder-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.