这个问题在提及lib或包含提供功能库之前已经被询问过了,但是我想从头创建一个。所以以下如何从头开始创建一个基于PHP/MySQL的图像库?
- 画廊任何想法需要使用表和浏览上传(这个我找不到任何问题,只需要它在那里勾勒步骤)
- 需要有一个缩略图创建当一个文件上传。
- 究竟应该如何在数据库中的结构,例如存储在数据库作为图像或文件名
质量要求
- 只有PHP和MySQL
任何想法?请让我知道,如果它不能做,以及:d
感谢
这个问题在提及lib或包含提供功能库之前已经被询问过了,但是我想从头创建一个。所以以下如何从头开始创建一个基于PHP/MySQL的图像库?
质量要求
任何想法?请让我知道,如果它不能做,以及:d
感谢
我要去尝试回答您的问题:
那部分其实很简单。要创建文件上传表单,您的HTML需要看起来像:
<form enctype='multipart/form-data' action='CodeTool.php' method='POST'>
File: <input name='picture' type='file'/>
<input type='submit' value='Upload'/>
</form>
你的形式需要有enctype='multipart/form-data'
和method
必须POST
。然后,要阅读上传文件,您可以简单地使用以下内容。我还添加了一些基本的验证,以确保该文件是一个图像。
if(isset($_FILES['picture'])) {
echo "File has been uploaded under temp file " . $_FILES['picture']['tmp_name'];
// Let's check if the file is an image:
$fileData = file_get_contents($_FILES['picture']['tmp_name']);
// Using imagecreatefromstring, that way you don't need to
// guess the image format.
if(($img = @imagecreatefromstring($fileData)) !== FALSE) {
echo " and is a valid image";
} else {
echo " and is not a valid image";
}
}
要创建一个缩略图,你可以使用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
}
这里有2个选项。您可以将图像存储在文件系统或数据库中。储存您的图片在文件系统中,你可以做到以下几点:
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
。
你几乎肯定会要存储在文件系统中的图像,然后刚才提到在数据库条目中的文件名\路径 - 它保持你的查询结果的大小,特别是如果你想拉多个图像的信息。如果您想使用它来创建缩略图,它还可以更容易地调用imagemagick之类的东西。
是否ImageMagick的支持PHP? – 2009-08-05 01:35:07
http://us.php.net/imagick – Amber 2009-08-05 02:35:40
我说得对,插入查询数据类型是TEXT? – 2009-08-05 01:37:21