[英]Uploading images in database and Resize the image
I spend six hours on trying to figure out how to upload images in my database and to re-size images. 我花了六个小时试图弄清楚如何在数据库中上传图像并调整图像大小。 I new in php so anyone can help me.
我是php新手,所以任何人都可以帮助我。
I research this post: How to upload images into MySQL database using PHP code 我研究了这篇文章: 如何使用PHP代码将图像上传到MySQL数据库
but I dont know what BLOBS is nor I do not think this post answer my question. 但是我不知道BLOBS是什么,我也不认为这篇文章能回答我的问题。 I may be wrong so please explain if I am.
我可能错了,所以请解释我是否正确。
Also, I research this Post: How to add an image to php mysql database? 此外,我研究了这篇文章: 如何将图像添加到php mysql数据库?
::::CHANGES:::: ::::变化::::
I made changes to my php. 我对我的PHP进行了更改。 I add my variables into my php code and now it not sending images to my server side folder "photo" how can I get that happen?
我将变量添加到我的php代码中,现在它没有将图像发送到服务器端文件夹“照片”,我该怎么办?
PHP code: 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);
}
}
?>
Here my html: 这是我的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>
PHP CODE: PHP代码:
Photo upload, validation at server
照片上传,在服务器上验证
<?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.");
}
}
Create thumbnail
创建缩略图
$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.
Store Thumbnail in database
将缩略图存储在数据库中
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);
}
?>
As you have image data in $image_p (after resizing), you need to put this variable in your database. 由于$ image_p中有图像数据(调整大小后),因此需要将此变量放入数据库中。
PHP does not provide a way to convert the image object to it's data, so we need to output in buffer and save in variable: PHP没有提供将图像对象转换为其数据的方法,因此我们需要在缓冲区中输出并保存在变量中:
ob_start();
$imagecreate($image_p);
$contents = ob_get_contents(); //this is your image contents
ob_end_clean();
Now insert this contents in your database. 现在,将这些内容插入数据库中。 Make sure the contents column is of BLOB Type.
确保内容列为BLOB类型。 For example:
例如:
$sql = "INSERT INTO Uploadimg (img_id,img_name,img_path,img_type,contents) VALUES ('0','$filename','$filepath','$filetype', '$contents')";
When you want to show the image in HTML src tag use this: 当您想在HTML src标签中显示图像时,请使用以下命令:
<img src="data:image/png;base64,<?php echo base64_encode($image_contents_from_db); ?>">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.