OK,讓我們說我們有一個表格,並在其中一個簡單的文件上傳輸入保存上傳圖片的縮略圖,並以Base64編碼字符串
<form action="index.php" method="post" enctype="multipart/form-data">
<input name="image_file" type="file" />
<input type="submit" name="submit-btn" value="Upload" />
</form>
然後是index.php文件
// check $_FILES['ImageFile'] not empty
if(!isset($_FILES['image_file']) || !is_uploaded_file($_FILES['image_file']['tmp_name'])){
die('Image file is Missing!'); // output error when above checks fail.
}
//uploaded file info we need to proceed
$image_name = $_FILES['image_file']['name']; //file name
$image_size = $_FILES['image_file']['size']; //file size
$image_temp = $_FILES['image_file']['tmp_name']; //file temp
$image_size_info = getimagesize($image_temp); //get image size
if($image_size_info){
$image_width = $image_size_info[0]; //image width
$image_height = $image_size_info[1]; //image height
$image_type = $image_size_info['mime']; //image type
}else{
die("Make sure image file is valid!");
}
然後我嘗試重新大小的文件
switch($image_type){
case 'image/png':
$image_res = imagecreatefrompng($image_temp);break;
case 'image/gif':
$image_res = imagecreatefromgif($image_temp); break;
case 'image/jpeg': case 'image/pjpeg':
$image_res = imagecreatefromjpeg($image_temp); break;
default:
$image_res = false;
}
現在我要輸出的圖像用戶瀏覽器不存儲文件。 這爲什麼我試圖做到這一點
$data = base64_encode(file_get_contents($_FILES['image_file']['tmp_name']));
但我覺得在這裏我有我的邏輯失效,becaue它不輸出的畫面,我想反正東西到底是呼應圖片如下所示。
switch($image_type){
case 'image/png':
echo '<img src="data:image/png;base64,"'.$data.' alt="" />'; break;
case 'image/gif':
echo '<img src="data:image/gif;base64,"'.$data.' alt="" />';break;
case 'image/jpeg': case 'image/pjpeg':
echo '<img src="data:image/jpeg;base64,"'.$data.' alt="" />';break;
}
有人可以看到失敗?
您的解決方案是不工作我得到'警告:imagepng()預計參數1是資源,串given' –
啊,對不起。我的錯。我會更新答案。 – budwiser