PHP Thumbnail Generator

[font=arial][size=14pt]Hi,
I am pretty new to PHP. I am working on a Thumbnail Generator script. It reads the images in a dir selected by the user, then determines there file type. It then makes a thumbs dir within the photo dir to place the thumbnails in if one isnt already created. It then should create a thumbnail of each image with a size based on the user input. Unfortunately it only works with some images. Will this work on all image sizes? Will different aspect ratios throw this off? Do I need to use jquery or ajax to give it time to process? I am unsure of what is causing the issue.[/size][/font]

HTML that calls the PHP code (used on Foundation Framework)

<div class="row">
	<div class="small-12 medium-6 large-5 columns">
		 <div class="callout panel">
			<h3>Thumbnail Generator</h3>
				<a href="#" data-reveal-id="help" data-reveal>Click for Help</a>
                                 <br><br>
				<form method="post" action="fileManager.php" data-abide>
				       <label for="imgDir">Original Image Directory:</label>
							<div class="small-12 columns">
								<?php
							$pattern = "img/*";
							$dirs = glob($pattern, GLOB_ONLYDIR);
							
                                                        echo '<select name="imgDir">';
							
                                                        foreach($dirs as $val)
							{
								if($val != "img/_thumbs") 
								{
									echo '<option value="'.$val.'">'.$val."</option>\n";
									$pattern = "$val/*";
									
									$dirs = glob($pattern, GLOB_ONLYDIR);
										foreach($dirs as $valsub)
										{
											echo '<option value="'.$valsub.'">'.$valsub."</option>\n";
										}
								} 	
							} 
							echo '</select>';     						
								?>
							</div>
							
							<div class="width-field">
							        <div class="small-12 columns">
								        <div class="small-3 columns">
							 		        <label for="imgWidth" class="right">Thumbnail Width:</label>
									</div>
									<div class="row collapse">
										<div class="small-5 columns">
											<input type="text" name="imgWidth"  placeholder="200" pattern="integer" required>
										<small class="error">An integer is required.</small>
										</div>
									         <div class="small-4 columns">
										<span class="postfix">Pixels</span>
									</div>
								</div>
						        </div>
						</div>
							
						<br>
						<input class="button" type="submit" value="Create Thumbnails" name="createThumbnails"/>
						 
					</form>
				</div>
			</div>
			<div class="small-12 medium-6 large-7 columns">
			<div class="panel">
				<p>Generated Output:</p>
				<?PHP include (ABSLPATH.'/thumbnails.php') ?>
			</div>
		</div>
	</div>

Here is my PHP Code thumbnails.php
[php]

<?php //Functions function alert($message) { echo '
'; echo '
' . $message . '×'; echo '
'; } function warning($message) { echo '
'; echo '
' . $message . '×'; echo '
'; } function imageCreateFromAny($filepath) { $type = exif_imagetype($filepath); // [] if you don't have exif you could use getImageSize() $allowedTypes = array( 1, // [] gif 2, // [] jpg 3, // [] png 6 // [] bmp ); if (!in_array($type, $allowedTypes)) { return false; } switch ($type) { case 1 : $im = imageCreateFromGif($filepath); break; case 2 : $im = imageCreateFromJpeg($filepath); break; case 3 : $im = imageCreateFromPng($filepath); break; case 6 : $im = imageCreateFromBmp($filepath); break; } return $im; } function createThumbnail($imageDirectory, $thumbDirectory, $thumbWidth) { $files = glob($imageDirectory.'/*.{jpg,png,gif}', GLOB_BRACE); echo '
Thumbnails Generated:

'; if(! empty( $files) ) { foreach($files as $file) { echo ''.$file . '
'; $srcImg = @imagecreatefromAny($file); $origWidth = imagesx($srcImg); $origHeight = imagesy($srcImg); $ratio = $origHeight/ $origWidth; $thumbHeight = $thumbWidth * $ratio; $thumbImg = imagecreatetruecolor($thumbWidth, $thumbHeight); imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $origWidth, $origHeight); if (!file_exists($imageDirectory."/thumbs/")) { mkdir($imageDirectory."/thumbs/", 0777, true); } imagejpeg($thumbImg, $imageDirectory."/thumbs/".basename($file)); } echo "

"; alert("Thumbnails Generated Successfully"); } else { echo "

"; warning("No images in directory!"); } } if (isset($_POST['createThumbnails'])) { createThumbnail($_POST['imgDir'],$_POST['imgThumbDir'],$_POST['imgWidth']); } ?>

[/php]

Any help would be greatly appreciated. Thanks again!

Please do not create a new thread for the same issue.

Sponsor our Newsletter | Privacy Policy | Terms of Service