我如何创建一个PHP/MySQL供的图像的画廊从头开始?
题
这个问题已经要求前提lib或包括,提供了一个功能画廊,但是我想创建一个从零开始。因此,任何想法,下面的
- 画廊的需要上传使用的形式和浏览(这个我能找到的没问题,只是需要它在那里概述的步骤)
- 需要有一个缩影像创造了当的文件上传。
- 应该如何构建的数据库,例如存在DB作为图像或文件
要求
- 只PHP和MySql
任何想法?请让我知道,如果它不能完成,以及:D
感谢
解决方案
我要尝试回答你的问题:
问题1
这部分实际上是简单的。创建一个文件上传的形式,你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";
}
}
问题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
在这里,你有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如果你要用它来创建的略图。
不隶属于 StackOverflow