Random Image Rotator, that resizes images...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tonym
Forum Newbie
Posts: 3
Joined: Mon Apr 13, 2009 6:20 pm

Random Image Rotator, that resizes images...

Post by tonym »

Hi all!

I'm working on compiling a random image rotator, that will rotate through various files in a folder on my webpage. I've been trying to add in script that will automatically resize the pictures to a specified size, as they're being rotated through, so that the size of the pictures don't have to be changed manually. My image rotator works, but I've been having problems adding in the resizer. I'm new to PHP, so I've been learning a lot as I go, and haven't been able to correctly revise my code. Thanks in advance for any help. Here's the code:


<?php

$folder = '.';

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

$img = null;

if (substr($folder,-1) != '/') {
$folder = $folder.'/';
}

if (isset($_GET['img'])) {
$imageInfo = pathinfo($_GET['img']);
if (
isset( $extList[ strtolower( $imageInfo['extension'] ) ] ) &&
file_exists( $folder.$imageInfo['basename'] )
) {
$img = $folder.$imageInfo['basename'];
}
} else {
$fileList = array();
$handle = opendir($folder);
while ( false !== ( $file = readdir($handle) ) ) {
$file_info = pathinfo($file);
if (
isset( $extList[ strtolower( $file_info['extension'] ) ] )
) {
$fileList[] = $file;
}
}
closedir($handle);

if (count($fileList) > 0) {
$imageNumber = time() % count($fileList);
$img = $folder.$fileList[$imageNumber];
}
}

if ($img!=null) {
$imageInfo = pathinfo($img);
$contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ];
header ($contentType);


//Start of Resizer

$size = getimagesize ($img);
$wo = $size (0);
$ho = $size (1);
$hn = 158;
$wn = ($wo/$ho)*$hn;

$resize = imagecreatetruecolor ($wn, $hn)

switch ($size['mime'])
{
case 'image/jpeg':
$pic = imagecreatefromjpeg($img);
imagecopyresampled ($resize , $pic , 0 , 0 , 0 , 0 , $wn , $hn , $wo , $ho )
imagejpeg($resize, '/small/'.$img, 100);
break;

case 'image/png':
$pic = imagecreatefrompng($img);
imagecopyresampled ($resize , $pic , 0 , 0 , 0 , 0 , $wn , $hn , $wo , $ho)
imagepng ($resize, '/small/'.$img, 100);
break;
}


imagedestroy ($pic);

//imagejpeg ($resize)
//imagedestroy ($resize)

//End of Resize

include_path="/small/"
readfile ($img, true)

}

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);
}
}

?>
Post Reply