Jump to content

image file extension in thumbnail


mdvignesh

Recommended Posts

<?php
function generateThumbs()	{
$pathToScreens = "D:/wamp/www/vignesh_2/up_images/"; // Directory to your images you want converted to thumbnails.
$pathToThumbs = "D:/wamp/www/vignesh_2/up_images/thumb/"; // Directory to your thumbnails.
$thumbWidth = 70; // Width of the thumbnails generated.

$dir = opendir($pathToScreens) or die("Could not open directory");
$counter = 0;

while(($fname = readdir($dir)) !== false)	{
	if($fname != "." && $fname != "..") {
		// Remove folders.
		$valid_extensions = array("jpg","jpeg","gif","png"); // Only jpeg images allowed.
		$info = pathinfo($pathToScreens . $fname);
//			print_r($info);

			if( in_array (strtolower ($info["extension"]),$valid_extensions)) {
			// Make sure the file is an image file by checking its extension to the array of image extensions.
			$img = imagecreatefromjpeg($pathToScreens . $fname); // Select the file as an image from the directory.
			$width = imagesx($img);
			$height = imagesy($img);
			// Collect its width and height.
			$newHeight = floor($height * ($thumbWidth / $width)); // Calculate new height for thumbnail.

			$tempImage = imagecreatetruecolor($thumbWidth,$newHeight); // Create a temporary image of the thumbnail.
			// Copy and resize old image into new image.
			imagecopyresized($tempImage,$img, 0, 0, 0, 0, $thumbWidth,$newHeight,$width,$height);

			$genThumb = imagejpeg($tempImage,$pathToThumbs . $fname);

			// Create the thumbnail with the new width and height in the thumbnails directory.
			// I added a rand 3 digit number in front of the file name to avoid overwrite.
			$counter++; // Increment.
		}
	}
}
if($counter > 0) {
	return $counter . " thumbnails generated from the directory \"".$pathToScreens."\".";
}
else {
	return "No image files could be processed.";
}
closedir($dir); // Close the directory.
}

//echo "<img src='up_images/thumb/650Blue hills.jpg' />";
?>

17526_.php

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.