如何使用PHP在数据库中保存缩略图

时间:2015-04-16 11:15:18

标签: web-development-server

我正在创建图像的缩略图,然后如何将其保存到数据库中,但我不知道该怎么做。这是我的代码,并以拇指图像显示图库图像。

<?php function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth ){

Uplaoding图片类型:

include("conn.php");

function GetImageExtension($imagetype)
     {
       if(empty($imagetype)) return false;
       switch($imagetype)
       {
           case 'image/bmp': return '.bmp';
           case 'image/gif': return '.gif';
           case 'image/jpeg': return '.jpg';
           case 'image/png': return '.png';
           default: return false;
       }
     }


 if (!empty($_FILES["uploadedimage"]["name"])) {

$file_name=$_FILES["uploadedimage"]["name"];
$temp_name=$_FILES["uploadedimage"]["tmp_name"];
$imgtype=$_FILES["uploadedimage"]["type"];
$ext= GetImageExtension($imgtype);
$imagename=date("d-m-Y")."-".time().$ext;
$target_path = "images/".$imagename;


if(move_uploaded_file($temp_name, $target_path)) {

    //$sql="INSERT into 'images_tbl1' ('images_path','submission_date') VALUES ('".$target_path."','".date("Y-m-d")."')";
    $sql = "INSERT INTO images_tbl1 ". "(image_path) "."VALUES('$target_path')";

    mysql_select_db('gallery1');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not enter data: ' . mysql_error());
}
echo "Uploaded successfully\n";
}
}



        $pathToImages = "images/";
        // open the directory
        $dir = opendir( $pathToImages );

        // loop through it, looking for any/all JPG files
        while (false !== ($fname = readdir( $dir ))) {
          // parse path for the extension

          $info = pathinfo($pathToImages . $fname);
          // continue only if this is a JPEG image


          if ( strtolower($info['extension']) == 'jpg' ) 
          {

            $pathToThumbs = "images/thumbs/";
        echo "Creating thumbnail for {$fname}";
        echo "<br>";


        $img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
        $width = imagesx( $img );
        $height = imagesy( $img );

        // calculate thumbnail size
        $new_width = $thumbWidth;
        $new_height = floor( $height * ( $thumbWidth / $width ) );

        // create a new temporary image
        $tmp_img = imagecreatetruecolor( $new_width, $new_height );

        // copy and resize old image into new image 
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width,$new_height, $width, $height );

        // save thumbnail into a file
        imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
         $thumbimage =  $fname;
          }
        }
        // close the directory
closedir( $dir );

 }
 createThumbs("/images","/images/thumbs/",200);


/*?> if(move_uploaded_file($fname, $uploadedimage)) {

    //$sql="INSERT into 'images_tbl1' ('images_path','submission_date') VALUES ('".$target_path."','".date("Y-m-d")."')";
    $sql = "INSERT INTO images_tbl1 ". "(image_path) "."VALUES('$uploadedimage')";

    mysql_select_db('gallery1');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not enter data: ' . mysql_error());
}
echo "Uploaded successfully\n";
}
<?php */





?>

显示图库图片:

<?php
include("conn.php");

$select_query = "SELECT  *FROM images_tbl1 ORDER by 'images_id' DESC";
mysql_select_db('gallery1');
$sql = mysql_query($select_query, $conn) or die(mysql_error()); 


 $count = 0;
while($row = mysql_fetch_array($sql))
{


if($count == 6) //three images per row
            {
               print "</tr>";
               $count = 0;
            }
            if($count==0)
               print "<tr>";
            print "<td>";

?>



<a class="fancybox" href="<?php echo $row["image_path"]; ?>" data-fancybox-group="images" ><img src="thumbnail.php?images_id=' . $row['image_path'] . '"  width="1000"/></a>

<?php
            $count++;
            print "</td>";
        }
        if($count>0)
           print "</tr>";
        ?>

0 个答案:

没有答案