如何将php缩略图创建器与多个图像上传器脚本集成?

如何将php缩略图创建器与多个图像上传器脚本集成?,php,image-uploading,phpthumb,Php,Image Uploading,Phpthumb,我有这个代码来上传多个图像。它工作得很好 <?php include('db.php'); session_start(); $session_id='1'; //$session id $path = "uploads/"; function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext =

我有这个代码来上传多个图像。它工作得很好

<?php
include('db.php');
session_start();
$session_id='1'; //$session id
$path = "uploads/";

function getExtension($str) 
{

     $i = strrpos($str,".");
     if (!$i) { return ""; } 

     $l = strlen($str) - $i;
     $ext = substr($str,$i+1,$l);
     return $ext;
   }

$valid_formats = array("jpg", "png", "gif", "bmp","jpeg");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
    {
        $name = $_FILES['photoimg']['name'];
        $size = $_FILES['photoimg']['size'];

        if(strlen($name))
            {
                 $ext = getExtension($name);
                if(in_array($ext,$valid_formats))
                {
                if($size<(1024*1024))
                    {
                         $inputFileName = $_FILES['photoimg']['name'];



                        $actual_image_name = time().substr(str_replace(" ", "_", $ext), 5).".".$ext;
                        $tmp = $_FILES['photoimg']['tmp_name'];
                        if(move_uploaded_file($tmp, $path.$actual_image_name))
                            {
                            mysql_query("UPDATE users SET profile_image='$actual_image_name' WHERE uid='$session_id'");

                                echo "<img src='uploads/".$actual_image_name."'  class='preview'>";
                            }
                        else
                            echo "Fail upload folder with read access.";
                    }
                    else
                    echo "Image file size max 1 MB";                    
                    }
                    else
                    echo "Invalid file format..";   
            }

        else
            echo "Please select image..!";

        exit;
    }
  ?>
但我想为每个图像创建缩略图。要做到这一点,我有另一个。我从一个网站上找到了它……但我不知道如何与我原来的网站整合

  function thumbnail($inputFileName, $maxSize = 100)
 {
 $info = getimagesize($inputFileName);

 $type = isset($info['type']) ? $info['type'] : $info[2];

 // Check support of file type
 if ( !(imagetypes() & $type) )
 {
 // Server does not support file type
 return false;
 }

  $width = isset($info['width']) ? $info['width'] : $info[0];
 $height = isset($info['height']) ? $info['height'] : $info[1];

 // Calculate aspect ratio
  $wRatio = $maxSize / $width;
$hRatio = $maxSize / $height;

// Using imagecreatefromstring will automatically detect the file type
 $sourceImage = imagecreatefromstring(file_get_contents($inputFileName));

// Calculate a proportional width and height no larger than the max size.
 if ( ($width <= $maxSize) && ($height <= $maxSize) )
 {
 // Input is smaller than thumbnail, do nothing
   return $sourceImage;
 }
 elseif ( ($wRatio * $height) < $maxSize )
 {
 // Image is horizontal
 $tHeight = ceil($wRatio * $height);
 $tWidth = $maxSize;
  }
  else
 {
 // Image is vertical
 $tWidth = ceil($hRatio * $width);
 $tHeight = $maxSize;
  }

  $thumb = imagecreatetruecolor($tWidth, $tHeight);

 if ( $sourceImage === false )
 {
 // Could not load image
 return false;
  }

  // Copy resampled makes a smooth thumbnail
  imagecopyresampled($thumb, $sourceImage, 0, 0, 0, 0, $tWidth, $tHeight, $width, $height);
 imagedestroy($sourceImage);

 return $thumb;
 }

   /**
    * Save the image to a file. Type is determined from the extension.
     * $quality is only used for jpegs.
      * Author: mthorn.net
        */
  function imageToFile($im, $fileName, $quality = 80)
  {
 if ( !$im || file_exists($fileName) )
 {
return false;
}

$ext = strtolower(substr($fileName, strrpos($fileName, '.')));

switch ( $ext )
{
 case '.gif':
 imagegif($im, $fileName);
break;
case '.jpg':
case '.jpeg':
imagejpeg($im, $fileName, $quality);
break;
case '.png':
imagepng($im, $fileName);
break;
case '.bmp':
imagewbmp($im, $fileName);
break;
default:
return false;
}

return true;
}

$im = thumbnail('temp.jpg', 100);
imageToFile($im, 'temp-thumbnail.jpg');
非常感谢您的帮助……

您可以使用以下命令:

<?php
....
if(move_uploaded_file($tmp, $path.$actual_image_name)) {
    // Create thumbnail here
    $thumb = thumbnail($path.$actual_image_name, "80"); // image name, and size
    imageToFile($thumb, 'path/to/thumb/thumbnail_' . $session_id . '.jpg');

    mysql_query("UPDATE users SET profile_image='$actual_image_name' WHERE uid='$session_id'");
    echo "<img src='uploads/".$actual_image_name."'  class='preview'>";
}
...
?>
不要忘记添加缩略图和imageToFile以放入php文件中

<?php    
// thumbnail configuration
$thumb_width = 50;
$thumb_height = 50;
$thumb_method = 'crop';
$thumb_bgColour = null;          //array(255,240,240);
$thumb_quality = 70;

// Let script run for as long as it needs to when creating thumbnails.
// Some web hosts won't let you use this function. In that case you'll need
// to comment it out and just refresh the script until it has thumbnailed all the images.
set_time_limit(0);

// include the thumbnail function
require_once dirname(__FILE__) . '/paGdThumbnail.php';

// get the path to the current directory
$path = dirname(__FILE__);

// get the url for the images
$path_info = pathinfo($_SERVER['SCRIPT_NAME']);
$url = $path_info['dirname'];

// create an array to store image names in.
$images = array();

//$dir = new DirectoryIterator($path);

$file_name=$_REQUEST['big_img'];//$_FILES['big_img']['name'];
$file_path=$path.'\\'.$file_name;

/*
 Loop through the directory, finding all images that aren't thumbnails.
 For each image:
- if it doesn't already have a thumbnail, or the thumbnail is older than the image,
                create a thumbnail.
- get info about the image
- add the image to our images array
*/


if( preg_match('#^(.+?)(_t)?\.(jpg|gif|png)#i', $file_name, $matches)){

list( ,$name, $is_a_thumb, $extension) = $matches;

// if its not a thumbnail
if( !$is_a_thumb ){
    // does a valid thumbnail exist?
    $has_thumb = false;
    $thumb_file = $path . '/' . $matches[1] . '_t.jpg';
    if( file_exists($thumb_file) && filemtime($thumb_file) > filemtime($file_path) ){
                    $has_thumb = true;
    }
    else{
        // no thumbnail, so we shall create one!

        // create a gd image. reading the contents of the image file into a string, then
        // using imagecreatefromstring saves having to check the filetype and which
        // imagecreatefrom(jpeg/gif/png) function to use
        $image = imagecreatefromstring(file_get_contents($file_path));

        if( $image ){
            // create the thumbnail
            $thumb = paGdThumbnail($image, $thumb_width, $thumb_height, $thumb_method, $thumb_bgColour);
            // free the image resource
            imagedestroy($image);
            if( $thumb ){
                // save the thumbnail
                if( imagejpeg($thumb, $thumb_file, $thumb_quality) ){
                                $has_thumb = true;
                }
                // free the memory used by the thumbnail image
                imagedestroy($thumb);
            }
        }
    }
    if( $has_thumb ){
                    $images[$file_name] = $name;
    }
}
}


// sort the images array so always in the same order
ksort($images);

// end or processing, now display the gallery
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Thumbnails :</title>
<style type="text/css">
<!--
#thumbs{
    position: relative;
}

#thumbs div{
    float: left;
    width: <?php echo $thumb_width + 30?>px;
    height: <?php echo $thumb_height + 30?>px;
    text-align: center;
}

#thumbs a:link img, #thumbs a:visited img{
    border: 1px solid #acacac;
    padding: 5px;
}

#thumbs a:hover img{
    border: 1px solid black;
}

-->
</style>
</head>
<body>
<div id="thumbs">
<?php foreach( $images as $imagename => $name ){ ?> 
<div>
    <a href="<?php echo $url . '/' . $imagename?>" title="Full Size"><img src="<?php echo $url . '/' . $name . '_t.jpg'?>"  /></a>
</div> 
<?php }?>
</div>
</body>
</html>