在数据库中上传图像并调整图像大小

时间:2016-05-04 09:26:59

标签: php image mysqli

我花了六个小时试图弄清楚如何在我的数据库中上传图像并重新调整图像大小。我是php的新手,所以任何人都可以帮助我。

我研究这篇文章:How to upload images into MySQL database using PHP code

但我不知道BLOBS是什么,也不认为这篇文章回答了我的问题。我可能错了所以请解释一下我是不是。

另外,我研究这篇文章:How to add an image to php mysql database?

::::变化::::

我对我的php进行了更改。我将我的变量添加到我的PHP代码中,现在它不将图像发送到我的服务器端文件夹“photo”我怎么能这样做?

PHP代码:

<?php

$filetmp = $_FILES["file_img"]["tmp_name"];
$filename = $_FILES["file_img"]["name"];
$filetype = $_FILES["file_img"]["type"];
$filesize = $_FILES["file_img"]["size"];
$fileinfo = getimagesize($_FILES["file_img"]["tmp_name"]);
$filewidth = $fileinfo[0];
$fileheight = $fileinfo[1];
$filepath = "~warren/mysql2/photo/".$filename;
$filepath_thumb = "~warren/mysql2/photo/thumb/".$filename;
chmod($filepath,0777);
chmod($filepath_thumb,0777);


if(isset($_POST['btn_upload']))
{
    $sPhotoFileName = $filename;
    $nPhotoSize = $filesize;
    $sTempFileName = $filetmp;

if ($sPhotoFileName) // file uploaded
{   $aFileNameParts = explode(".", $sPhotoFileName);
    $sFileExtension = end($aFileNameParts); // part behind last dot
    if ($sFileExtension != "jpg"
        && $sFileExtension != "png"
        && $sFileExtension != "gif")
    {   die ("Choose a JPG for the photo");
    }

    if ($nPhotoSize == 0)
    {   die ("Sorry. The upload of $sPhotoFileName has failed.
Search a photo smaller than 100K, using the button.");
    }
    if ($nPhotoSize > 10240000000)
    {   die ("Sorry.
The file $sPhotoFileName is larger than 100K.
Advice: reduce the photo using a drawing tool.");
    }
    // read photo

    $oTempFile = fopen($sTempFileName, "r");
    $sBinaryPhoto = fread($oTempFile, fileSize($sTempFileName));
    // Try to read image
    $nOldErrorReporting = error_reporting(E_ALL & ~(E_WARNING)); // ingore warnings
    $oSourceImage = imagecreatefromstring($sBinaryPhoto); // try to create image
    error_reporting($nOldErrorReporting);
    if (!$oSourceImage) // error, image is not a valid jpg
    { die ("Sorry.
It was not possible to read photo $sPhotoFileName.
Choose another photo in JPG format.");
    }
}
 $nWidth = imagesx($oSourceImage); // get original source image width
        $nHeight = imagesy($oSourceImage); // and height
        // create small thumbnail
        $nDestinationWidth = 80;
        $nDestinationHeight = 60;
    //$oDestinationImage = imagecreatetruecolor($nDestinationWidth, $nDestinationHeight);
        $oDestinationImage = imagecreate($nDestinationWidth, $nDestinationHeight);
    /*$oResult = imagecopyresampled(
        $oDestinationImage, $oSourceImage,
        0, 0, 0, 0,
        $nDestinationWidth, $nDestinationHeight,
        $nWidth, $nHeight); // resize the image
    */
        imagecopyresized($oDestinationImage, $oSourceImage,0, 0, 0, 0,$nDestinationWidth, $nDestinationHeight,$nWidth, $nHeight); // resize the image
        ob_start(); // Start capturing stdout.
        imageJPEG($oDestinationImage); // As though output to browser.
        $sBinaryThumbnail = ob_get_contents(); // the raw jpeg image data.
        ob_end_clean(); // Dump the stdout so it does not screw other output.

    if($_POST['btn_upload'])
    {

    $sBinaryThumbnail = addslashes($sBinaryThumbnail);
    $oDatabase = $link;
    mysql_select_db("upload", $oDatabase);
    $sQuery = "insert into image (thumbnail) values ('$sBinaryThumbnail')";
    echo $sQuery;
    mysql_query($sQuery, $oDatabase);
    }
}

    ?>

这是我的html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>

<form action="test4.php" method="post" enctype="multipart/form-data">
<input type="file" name="file_img" />
<input type="submit" name="btn_upload" value="Upload">  
</form>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

PHP代码:

  
    

照片上传,服务器验证

  
<?php
$sPhotoFileName = $_FILES['photo']['name']; // get client side file name
if ($sPhotoFileName) // file uploaded
{   $aFileNameParts = explode(".", $sPhotoFileName);
    $sFileExtension = end($aFileNameParts); // part behind last dot
    if ($sFileExtension != "jpg"
        && $sFileExtension != "JPEG"
        && $sFileExtension != "JPG")
    {   die ("Choose a JPG for the photo");
    }
    $nPhotoSize = $_FILES['photo']['size']; // size of uploaded file
    if ($nPhotoSize == 0)
    {   die ("Sorry. The upload of $sPhotoFileName has failed.
Search a photo smaller than 100K, using the button.");
    }
    if ($nPhotoSize > 10240000000)
    {   die ("Sorry.
The file $sPhotoFileName is larger than 100K.
Advice: reduce the photo using a drawing tool.");
    }
    // read photo
    $sTempFileName = $_FILES['photo']['tmp_name']; // temporary file at server side
    $oTempFile = fopen($sTempFileName, "r");
    $sBinaryPhoto = fread($oTempFile, fileSize($sTempFileName));
    // Try to read image
    $nOldErrorReporting = error_reporting(E_ALL & ~(E_WARNING)); // ingore warnings
    $oSourceImage = imagecreatefromstring($sBinaryPhoto); // try to create image
    error_reporting($nOldErrorReporting);
    if (!$oSourceImage) // error, image is not a valid jpg
    { die ("Sorry.
It was not possible to read photo $sPhotoFileName.
Choose another photo in JPG format.");
    }
}
  
    

创建缩略图

  
    $nWidth = imagesx($oSourceImage); // get original source image width
        $nHeight = imagesy($oSourceImage); // and height
        // create small thumbnail
        $nDestinationWidth = 80;
        $nDestinationHeight = 60;
    //$oDestinationImage = imagecreatetruecolor($nDestinationWidth, $nDestinationHeight);
        $oDestinationImage = imagecreate($nDestinationWidth, $nDestinationHeight);
    /*$oResult = imagecopyresampled(
        $oDestinationImage, $oSourceImage,
        0, 0, 0, 0,
        $nDestinationWidth, $nDestinationHeight,
        $nWidth, $nHeight); // resize the image
    */
        imagecopyresized($oDestinationImage, $oSourceImage,0, 0, 0, 0,$nDestinationWidth, $nDestinationHeight,$nWidth, $nHeight); // resize the image
        ob_start(); // Start capturing stdout.
        imageJPEG($oDestinationImage); // As though output to browser.
        $sBinaryThumbnail = ob_get_contents(); // the raw jpeg image data.
        ob_end_clean(); // Dump the stdout so it does not screw other output.
  
    

将缩略图存储在数据库中

  
 if($_POST['send'])
    {
    $sBinaryThumbnail = addslashes($sBinaryThumbnail);
    $oDatabase = mysql_connect("localhost", "root", "karma");
    mysql_select_db("upload", $oDatabase);
    $sQuery = "insert into image (thumbnail) values ('$sBinaryThumbnail')";
    echo $sQuery;
    mysql_query($sQuery, $oDatabase);
    }
    ?>

答案 1 :(得分:0)

由于您在$ image_p中有图像数据(调整大小后),您需要将此变量放在数据库中。

PHP没有提供将图像对象转换为数据的方法,因此我们需要在缓冲区中输出并保存在变量中:

ob_start();
$imagecreate($image_p);
$contents =  ob_get_contents(); //this is your image contents
ob_end_clean();

现在将此内容插入数据库中。确保内容列是BLOB类型。 例如:

$sql = "INSERT INTO Uploadimg (img_id,img_name,img_path,img_type,contents) VALUES ('0','$filename','$filepath','$filetype', '$contents')";

如果要在HTML src标记中显示图像,请使用以下命令:

<img src="data:image/png;base64,<?php echo base64_encode($image_contents_from_db); ?>">