Jamaal10 Posted October 5, 2007 Share Posted October 5, 2007 I have the following script running to show misc. images from a folder on my main page. <?php function random_image() { /* ** Directory */ $dir = "thumbs"; if(!is_dir($dir)) die("Directory does not exists"); /* ** Process the directory and read all files in it */ $dirHandle = opendir($dir); while(($im = readdir($dirHandle))) { /* ** Do no include sub directories as files */ if($im != ".." && $im != ".") { /* ** Create an array for all the files in the directory */ $image[] = $im; } } if(!is_array($image)) die("Unable to read files in directory"); /* ** Close working directory */ closedir($dirHandle); /* ** Make cache header expire, so its always fresh */ header("Cache-Control: no-cache, must-revalidate"); header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); $n = rand(0,(count($image)-1)); $random_image = $image[$n]; /* ** Get file extension */ $t_ext = explode(".", $random_image); $ext = $t_ext[count($t_ext)-1]; switch(strtolower($ext)) { case 'jpeg': case 'jpg': header("Content-type: image/jpeg"); break; case 'png': header("Content-type: image/png"); break; case 'gif': header("Content-type: image/gif"); break; default: /* ** Loop if its not an image */ random_image(); break; } $images_ext = array("jpg", "jpeg", "png", "gif"); if(!file_exists($dir."/".$random_image)) die("File does not exists"); /* ** Only display if they are images */ if(!readfile($dir."/".$random_image) && !in_array($ext, $images_ext)) { readfile($dir."error/error.gif"); } } random_image(); ?> I've been trying to set a max size limit on these images to 200x200 pixels (size ranges from 20x20 pixels to 1024x768), but am unable to get it done. Everything I've tried results in broken links or no effect. I'm almost positive that I'm missing something right in front of my eyes, but cannot figure it out. Any ideas on what I'm doing wrong? Appreciate any help! Quote Link to comment https://forums.phpfreaks.com/topic/72030-solved-need-help-thumbnailing-an-image/ Share on other sites More sharing options...
MadTechie Posted October 5, 2007 Share Posted October 5, 2007 for the most part it looks ok, can be cleaned up but still ok i'll change the last part to if( is_file($dir."/".$random_image) && in_array($ext, $images_ext) ) { readfile($dir."/".$random_image); }else{ readfile($dir."error/error.gif"); } exit; you may have a problem in the block switch(strtolower($ext)), as your calling the scrip again, which could, have a never ending effect Quote Link to comment https://forums.phpfreaks.com/topic/72030-solved-need-help-thumbnailing-an-image/#findComment-362941 Share on other sites More sharing options...
Jamaal10 Posted October 5, 2007 Author Share Posted October 5, 2007 Sorry for the newbness, but will that help with the size issue? Quote Link to comment https://forums.phpfreaks.com/topic/72030-solved-need-help-thumbnailing-an-image/#findComment-362953 Share on other sites More sharing options...
MadTechie Posted October 5, 2007 Share Posted October 5, 2007 ahh nope but heres an basic example that should (your need to update for PNG & GIF) (see the switch's) if( is_file($dir."/".$random_image) && in_array($ext, $images_ext) ) { resize($dir."/".$random_image, 100,"jpg"); }else{ readfile($dir."error/error.gif"); } exit; function resize($image, $thumbWidth, $type) { switch($type) { case "jpg": $img = imagecreatefromjpeg($image); break; } $width = imagesx( $img ); $height = imagesy( $img ); // calculate thumbnail size $new_width = $thumbWidth; $new_height = floor( $height * ( $thumbWidth / $width ) ); // create a new temporary image $tmp_img = imagecreatetruecolor( $new_width, $new_height ); // copy and resize old image into new image imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height ); switch($type) { case "jpg": imagejpeg( $tmp_img); break; } } Quote Link to comment https://forums.phpfreaks.com/topic/72030-solved-need-help-thumbnailing-an-image/#findComment-362973 Share on other sites More sharing options...
Jamaal10 Posted October 7, 2007 Author Share Posted October 7, 2007 That fixed everything. Thanks a lot for the help! I appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/72030-solved-need-help-thumbnailing-an-image/#findComment-363757 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.