如何从头开始创建支持PHP/MySQL的图像库?
在提到提供功能库的lib或includes之前,有人问过这个问题,但我想从头开始创建一个功能库。那么你对以下几点有什么想法如何从头开始创建支持PHP/MySQL的图像库?,php,mysql,gallery,Php,Mysql,Gallery,在提到提供功能库的lib或includes之前,有人问过这个问题,但我想从头开始创建一个功能库。那么你对以下几点有什么想法 画廊需要上传使用的形式和浏览(这我可以发现没有问题,只是需要它在那里概述的步骤) 上传文件时需要创建缩略图 它应该如何在数据库中结构化,例如作为图像或文件名存储在数据库中 要求 只有PHP和MySql 有什么想法吗?如果做不到,请告诉我 谢谢您几乎肯定会希望将图像存储在文件系统中,然后只需在DB条目中引用filename\path即可-这会降低查询结果的大小,特别是如果
- 只有PHP和MySql
谢谢您几乎肯定会希望将图像存储在文件系统中,然后只需在DB条目中引用filename\path即可-这会降低查询结果的大小,特别是如果您想获取多个图像的信息。如果您想使用imagemagick创建缩略图,它还可以更轻松地调用它。您几乎肯定会希望将图像存储在文件系统中,然后只在DB条目中引用文件名\路径-它可以减小查询结果的大小,特别是如果你想获取多张图片的信息。如果您想使用imagemagick创建缩略图,它还可以让您更轻松地调用imagemagick等工具。我将尝试回答您的问题:
问题1 这一部分其实很简单。要创建文件上载表单,您的HTML需要如下所示:
<form enctype='multipart/form-data' action='CodeTool.php' method='POST'>
File: <input name='picture' type='file'/>
<input type='submit' value='Upload'/>
</form>
问题2 要创建缩略图,您可以使用GD(或ImageMagick,但它不包括在默认配置中)这样。。。让我们从
imagecreatefromstring
if
语句继续:
if(($img = @imagecreatefromstring($fileData)) !== FALSE) {
// Let's create a 100x100 thumbnail
$width = imagesx($img);
$height = imagesy($img);
$boxSize = min($width,$height);
$boxX = ($width / 2) - ($boxSize / 2);
$boxY = ($height / 2) - ($boxSize / 2);
$thumb = imagecreatetruecolor(100, 100);
imagecopyresampled($thumb, $img, 0, 0, $boxX, $boxY, 100, 100, $boxSize, $boxSize);
//$thumb is now a 100x100 thumbnail
}
问题3 这里有两个选项。您可以将图像存储在文件系统或数据库中。要在文件系统中存储图像,可以执行以下操作:
if(($img = @imagecreatefromstring($fileData)) !== FALSE) {
move_uploaded_file($_FILES['picture']['tmp_file'], 'somefile.jpg');
// the code from the previous example
imagejpeg($thumb, 'somefile_thumb.jpg');
}
我个人更喜欢使用数据库来存储图像,因为这样更容易保持引用完整性并使备份更简单(备份数据库,您就完成了)。速度稍微慢一点,但差别并不是那么大:
if(($img = @imagecreatefromstring($fileData)) !== FALSE) {
// the code from the previous example
$tmp_thumb = tempnam(sys_get_temp_dir(), 'thumb');
imagejpeg($thumb, $tmp_thumb);
$thumbData = file_get_contents($tmp_thumb);
mysql_query("INSERT INTO images (original, thumb) VALUES ('" . mysql_real_escape_string($fileData) . "', '" . mysql_real_escape_string($thumbData) . "');");
}
字段必须是
BLOB
我将尝试回答您的问题:
问题1 这一部分其实很简单。要创建文件上载表单,您的HTML需要如下所示:
<form enctype='multipart/form-data' action='CodeTool.php' method='POST'>
File: <input name='picture' type='file'/>
<input type='submit' value='Upload'/>
</form>
问题2 要创建缩略图,您可以使用GD(或ImageMagick,但它不包括在默认配置中)这样。。。让我们从
imagecreatefromstring
if
语句继续:
if(($img = @imagecreatefromstring($fileData)) !== FALSE) {
// Let's create a 100x100 thumbnail
$width = imagesx($img);
$height = imagesy($img);
$boxSize = min($width,$height);
$boxX = ($width / 2) - ($boxSize / 2);
$boxY = ($height / 2) - ($boxSize / 2);
$thumb = imagecreatetruecolor(100, 100);
imagecopyresampled($thumb, $img, 0, 0, $boxX, $boxY, 100, 100, $boxSize, $boxSize);
//$thumb is now a 100x100 thumbnail
}
问题3 这里有两个选项。您可以将图像存储在文件系统或数据库中。要在文件系统中存储图像,可以执行以下操作:
if(($img = @imagecreatefromstring($fileData)) !== FALSE) {
move_uploaded_file($_FILES['picture']['tmp_file'], 'somefile.jpg');
// the code from the previous example
imagejpeg($thumb, 'somefile_thumb.jpg');
}
我个人更喜欢使用数据库来存储图像,因为这样更容易保持引用完整性并使备份更简单(备份数据库,您就完成了)。速度稍微慢一点,但差别并不是那么大:
if(($img = @imagecreatefromstring($fileData)) !== FALSE) {
// the code from the previous example
$tmp_thumb = tempnam(sys_get_temp_dir(), 'thumb');
imagejpeg($thumb, $tmp_thumb);
$thumbData = file_get_contents($tmp_thumb);
mysql_query("INSERT INTO images (original, thumb) VALUES ('" . mysql_real_escape_string($fileData) . "', '" . mysql_real_escape_string($thumbData) . "');");
}
字段必须是
BLOB
@Shahmir Javiad:不,它必须是我在回答中指定的BLOB
TEXT
是位置感知的,虽然它对大文本非常有用,但它会导致二进制对象的数据损坏问题。10/4 Com,仔细阅读答案后,我可以看到它在工作,因此我将在原始注释中接受itI意味着区域感知。我想我需要一些睡眠。@Shahmir Javiad:不,它需要是我在回答中指定的BLOB
TEXT
是位置感知的,虽然它对大文本非常有用,但它会导致二进制对象的数据损坏问题。10/4 Com,仔细阅读答案后,我可以看到它在工作,因此我将在原始注释中接受itI意味着区域感知。我想我需要一些睡眠。