Jump to content

Recommended Posts

Hi!  :D

 

I'm currently developing a photography portfolio for a friend. To display his images, rather than using a database and allowing it to get massive, I've decided to go ahead and stick them into a directory and then cycle through it. As it's currently set up, I open the directory and read it, use preg_split() to divide the files up according to file type, and then use a foreach() statement to display them.

 

It works like a charm. But now I have a problem: I want to limit the amount of files that are echoed out. I've tried everything. Using this:

$i = 0; 
foreach($images as $image){ 
// blahblahblah 
$i++ 
if($i > 10){ 
break; 
} 
}

 

And that doesn't work. :|

And I also decided to try and count() the array and stuff. Nothing worked.

 

Furthermore, print_r() returns this:

Array ( [0] => [1] => a_message.jpg [2] => ) Array ( [0] => [1] => cold_call.jpg [2] => ) Array ( [0] => [1] => ring_of_love.jpg [2] => ) Array ( [0] => thumbs ) Array ( [0] => [1] => unclear.jpg [2] => )

 

Isn't it supposed to return ONE array with different keys?! >.>

 

Here is the code:

$img_dir = "images/photographs/"; 
		if(is_dir($img_dir)){ 
			if($img_handle = opendir($img_dir)){
				echo "<div id=\"index_thumbnails\"><div id=\"hidden_images\">"; 
				while(($img = readdir($img_handle)) !== false){
					if($img !== "Thumbs.db" && $img !== "." && $img !== ".."){
						$images = preg_split('/([a-zA-Z0-9_-]*\.jpg$)/', $img, 0, PREG_SPLIT_DELIM_CAPTURE); 
							if(empty($images)){ 
								echo "<b>Sorry, there are no images to display!</b>"; 
							} 
							else{						
								foreach($images as $img_display){									
									if($img_display !== "" && $img_display !== "thumbs"){ 
										$img_name = "images/photographs/{$img_display}"; 
										$thumb_path = "images/photographs/thumbs/"; 
										$size = getimagesize($img_name); 
										$width = $size['0']; 
										$height = $size['1']; 
										$nwidth = round($width/20); 
										$nheight = round($height/20);
										$img_thumb = imagecreatefromjpeg("{$img_name}"); 
										$resized = imagecreatetruecolor($nwidth, $nheight); 
										imagecopyresampled($resized, $img_thumb, 0, 0, 0, 0, $nwidth, $nheight, $width, $height); 
										imagejpeg($resized, "{$thumb_path}{$img}_thumb.jpg"); 
										echo "<img src=\"{$thumb_path}{$img}_thumb.jpg\" height=\"{$nheight}\" width=\"{$nwidth}\"/>";
										} 
									}	

							}

						print_r($images);
					} 
				} 
				closedir($img_handle);
				echo "</div></div>"; 
			} 
		}

 

Bear with the fact that I'm not using a function to shrink my images. xD I'm getting all of the work out of the way before I make a function and link it to a database to get image information.

 

 

All help is appreciated! Thank you!

Link to comment
https://forums.phpfreaks.com/topic/228298-preg_split-on-file-names/
Share on other sites

 

<?php
// image and thumb paths
$img_dir = "images/photographs/"; 
$thumb_path = "images/photographs/thumbs/";

// get first $amount of images from dir
$amount=3;
$images = array_map('basename',glob($img_dir . "*.jpg"));
$images = array_slice($images,0,$amount);

// create and output thumbs
foreach ($images as $image) {
  $size = getimagesize($img_dir.$image); 
  $width = $size['0']; 
  $height = $size['1']; 
  $nwidth = round($width/20); 
  $nheight = round($height/20);
  $img_thumb = imagecreatefromjpeg($img_dir.$image); 
  $resized = imagecreatetruecolor($nwidth, $nheight); 
  imagecopyresampled($resized, $img_thumb, 0, 0, 0, 0, $nwidth, $nheight, $width, $height); 
  imagejpeg($resized, $thumb_path.$image); 
  echo "<img src='{$thumb_path.$image}' height='{$nheight}' width='{$nwidth}'/>";
} // end foreach
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.