简体   繁体   中英

How to make thumbnails using PHP

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>

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

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

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);
                }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM