Reputation: 988
I have create a page to upload image where stored in folder locally named upload, I also have make it as a list but how to make it as a thumbnail (with fixed pixel)? What happen now it just viewed with their own size pixel (some big and some small).
My Code as below:
<?php
// open this directory
$myDirectory = opendir("upload");
// get each entry
while($entryName = readdir($myDirectory)) {
$dirArray[] = $entryName;
}
// close directory
closedir($myDirectory);
//count elements in array
$indexCount = count($dirArray);
?>
<ul>
<?php
// loop through the array of files and print them all in a list
for($index=0; $index < $indexCount; $index++) {
$extension = substr($dirArray[$index], -3);
if ($extension == 'jpg'){ // list only jpgs
echo '<li><img src="upload/' . $dirArray[$index] . '" alt="Image" /><span>' . $dirArray[$index] . '</span>';
}
}
?>
</ul>
Upvotes: 1
Views: 1952
Reputation: 3366
A simple code for resizing images.. you can use any height/width you want. For example set them to 150x150 for a thumbnail...
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$extension = getExtension($filename);
$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) {
echo ' Unknown Image extension ';
$errors=1;
}
if($extension=="jpg" || $extension=="jpeg" ){
$uploadedfile = $_FILES['form_file']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
}
else if($extension=="png")
{
$uploadedfile = $_FILES['form_file']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);
}
else
{
$src = imagecreatefromgif($uploadedfile);
}
list($width,$height)=getimagesize($uploadedfile);
// Set Height and Width Here
$newwidth=150;
$newheight=150;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
// Set the path here
$filename = "../uploads/thumbs/". $_FILES['form_file']['name'];
imagejpeg($tmp,$filename,100);
imagedestroy($src);
imagedestroy($tmp);
}
Upvotes: 0
Reputation: 1153
There is a class called class.upload.php
which is an amazing class for working with images in php:
http://www.verot.net/php_class_upload_samples.htm
Not only it creates thumbnails like what you need with many options, it can also do tens of other operations on your images.
If you want to learn how to create thumbnails then see this link:
http://davidwalsh.name/create-image-thumbnail-php
But if you are going to use it in your producing web pages, then including class.upload.php
in your core and using it would be a nice choice ...
Upvotes: 1
Reputation: 57332
You will need to compile PHP with the GD library of image functions for this to work . to on just remove the ;
before the extension=php_gd.dll
in php.ini
here How to create thumbnails with PHP and gd is a good and well explained tutorial
Upvotes: 3