1

我正在使用一个函数来存储存储在目录中的原始图像的缩略图,现在我需要将该缩略图的文件路径保存在 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(); ?>

以上是我为此目的使用的代码。

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

4

3 回答 3

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
}
于 2013-06-06T08:11:20.500 回答
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 保存在数据库中。

于 2013-06-06T08:30:51.177 回答
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";
    }
    ?>

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

于 2013-06-06T13:31:20.787 回答