[英]how to create an rss feed for a website with php mysql from scratch
[英]How do I create a PHP/MySQL powered image gallery from scratch?
在提到lib或包含提供功能庫的include之前已經問過這個問題,但是我想從頭開始創建一個。 所以以下任何想法
要求
有任何想法嗎? 請讓我知道是否也無法完成: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之類的東西。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.