如何获取目录中orignal image创建的缩略图的文件路径

时间:2013-06-06 08:07:52

标签: php mysql

我正在使用一个函数来存储存储在目录中的原始图像的缩略图,现在我需要在db中保存该缩略图的文件路径。

我怎样才能获得缩略图的路径?

    <?php ob_start(); ?>

    <?php
    function make_thumb($src, $dest, $desired_width) {

    /* read the source image */
    $source_image = imagecreatefromjpeg($src);
    $width = imagesx($source_image);
    $height = imagesy($source_image);

    /* find the "desired height" of this thumbnail, relative to the desired width  */
    $desired_height = floor($height * ($desired_width / $width));

    /* create a new, "virtual" image */
    $virtual_image = imagecreatetruecolor($desired_width, $desired_height);

    /* copy source image at a resized size */
    imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);

    /* create the physical thumbnail image to its destination */
    imagejpeg($virtual_image, $dest);
    echo $thumb ."this is result";
    $sql="INSERT INTO images thumb VALUES('$thumb')";       
     $result = mysql_query($sql) or die(mysql_error());
    }
    ?>
    <?php  
     //This is the directory where images will be saved 
     $target = "uploads/images/"; 
     $target = $target . basename( $_FILES['photo']['name']); 
     //This gets all the other information from the form 
     $pic=($_FILES['photo']['name']); 
     // Connects to your Database 
     include('connect.php');
     //Writes the information to the database 
     $sql="INSERT INTO images (name, alt)
                    VALUES('$pic','$_POST[altText]')";      
     $result = mysql_query($sql) or die(mysql_error());
     //Writes the photo to the server 
     if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
     { 
     //Tells you if its all ok 
     echo "The file ". basename( $_FILES['photo']['name']). " has been uploaded, and your information has been added to the directory"; 
     } 
     else { 
     //Gives and error if its not 
     echo "Sorry, there was a problem uploading your file."; 
     } 
     ?>
    <?php
    $src=$target;
    $dest="uploads/thumbs/". basename( $_FILES[photo][name]);
    $desired_width=160; 
    $quality=80;
    make_thumb($src, $dest, $desired_width,$quality);
    // here i need the code to save the file path of thumbnail
    ?>

     <?php
     //unset($_POST);
     // 5. Close connection
     mysql_close($connection);
     header("Location: images.php");
    ?>

    <?php ob_flush(); ?>

以上是我用于此目的的代码。

在这方面的任何帮助将不胜感激

3 个答案:

答案 0 :(得分:1)

$dest是您的缩略图图片路径。您已经转到make_thumb()函数。

    $dest="uploads/thumbs/". basename( $_FILES[photo][name]); 

成功创建缩略图后,您应该从make_thumb()函数

返回true
$thumbFlag =  make_thumb($src, $dest, $desired_width,$quality);

if($thumbFlag){
       // write your insert query here to save thumb path (e.g, $dest)
}  else {
       // handle error here
}

答案 1 :(得分:0)

使用此功能。

  function imgOptCpy($inFile, $outFile="", $width=200, $height=200, $quality=100,      $resize=false)
{
//----------------------------------------
//echo "tanmoy-".$inFile;
//exit;
list($width_original, $height_original) = @getimagesize($inFile);
if($resize)
{
    if($width)
    {
        $height2 = ($width / $width_original) * $height_original;
        if($height && $height2 > $height)
            $width = ($height / $height_original) * $width_original;
        else
            $height = $height2;     

        //echo 'width = '.$width;
        //echo '- height = '.$height;
    }
    elseif($height)
    {
        $width = ($height / $height_original) * $width_original;

        //echo 'width = '.$width;
        //echo '- height = '.$height;
    }
    else
    {
        list($width, $height) = @getimagesize($inFile);

        //echo 'width = '.$width;
        //echo '- height = '.$height;
    }
}       
//echo 'width = '.$width;
//echo '- height = '.$height;

//----------------------------------------
$inType = @strtolower(@strrev(@substr(@strrev($inFile), 0, 4)));

if(!$outFile)
    $outFile = $inFile;
$outType = @strtolower(@strrev(@substr(@strrev($outFile), 0, 4)));
$image_xy = @imagecreatetruecolor($width, $height);

//----------------------------------------
if($inType==".jpg" || $inType=="jpeg")
{
    //echo $inFile ; //exit ;  
    $image = @imagecreatefromjpeg($inFile);

    @imagecopyresampled($image_xy, $image, 0, 0, 0, 0,
        $width, $height, $width_original, $height_original);
}
elseif($inType==".gif")
{
    $image = @imagecreatefromgif($inFile);
    @imagecopyresampled($image_xy, $image, 0, 0, 0, 0,
        $width, $height, $width_original, $height_original);
}
elseif($inType==".png")
{
    //echo $inFile ; //exit ;  
    $image = @imagecreatefrompng($inFile) ; 
    @imagecopyresampled($image_xy, $image, 0, 0, 0, 0,
        $width, $height, $width_original, $height_original);
}
//----------------------------------------
if($outType==".jpg" || $outType=="jpeg")
{
    //@header("Content-Type: image/jpeg");
    $ok = @imagejpeg($image_xy, $outFile, $quality);
}
elseif($outType==".gif")
{
    //@header("Content-Type: image/gif");
    //$ok = @imagegif($image_xy, $outFile, $quality);
    $ok = @imagegif($image_xy, $outFile);
}
elseif($outType==".png")
{
    //@header("Content-Type: image/png");
    if ($quality == 100) $quality = 9;
    $ok = @imagepng($image_xy, $outFile, $quality);
}
@chmod($outFile, 0777);
//echo "gfg-".$inFile;
   // exit;
//----------------------------------------
}

并将其调用到您需要执行缩略图的位置。

imgOptCpy($upload_target_original, $upload_path_for_thumbnail, 270, 0, 215, true);

并将$ upload_path_for_thumbnail保存在数据库中。

答案 2 :(得分:0)

这是我在Roopendra所提到的变化。

    <?php
    $src=$target;
    $dest="uploads/thumbs/". basename( $_FILES['photo']['name']);
    $desired_width=160; 
    $quality=80;
   // make_thumb($src, $dest, $desired_width,$quality);

     $thumbFlag =  make_thumb($src, $dest, $desired_width,$quality);
     echo $dest;
    if(!$thumbFlag){
    // write your insert query here to save thumb path (e.g, $dest)
    $sql = "UPDATE images SET thumb='$dest' WHERE name='$pic'";
    mysql_query($sql) or die(mysql_error());
    }  
    else {
    // handle error here
    echo "Sorry unable to store the image";
    }
    ?>

最后在数据库中获取缩略图的路径。 非常感谢!