你可以尝试将它转换成 Laravel
//The file post to the server
$image = $_FILES['imgfile'];
//array for type if octet
$MimeType = array(
'useNew' => false,
'type' => ''
);
if($image['type'] == "application/octet-stream"){
$imageMimeSize = getimagesize($img['tmp_name']); // get temporary file REAL info
$MimeType['type'] = $imageMimeSize['mime']; //set in our array the correct mime
$MimeType['useNew'] = true; //set to true for next if
}
//now check if we will use the MimeType or the one sent by browser
//$mimeCheck is the things you want to check. In my case i just wanted PNG or JPG
if($MimeType['useNew'] == true){ //if true, use MimeType to check file/image file
$mimeCheckType = ($MimeType['type'] != "image/png" && $MimeType['type'] != "image/jpeg" && $MimeType['type'] != "image/jpg") && $MimeType['type'] != "image/gif");
}else{ //if not using real mime, go with the one device sent to us or browser sent us
$mimeCheckType = ($img['type'] != "image/png" && $img['type'] != "image/jpeg" && $img['type'] != "image/jpg");
}
//returns error if image not png/jpg/jpeg/gif
if($mimeCheck){
//return some error
}
了解更多相关文档,请访问此链接:源文件
如果你想要极致体验,可以尝试将其格式化为 Laravel
<?php
function getjpegsize($img_loc) {
$handle = fopen($img_loc, "rb") or die("Invalid file stream.");
$new_block = NULL;
if(!feof($handle)) {
$new_block = fread($handle, 32);
$i = 0;
if($new_block[$i]=="\xFF" && $new_block[$i+1]=="\xD8" && $new_block[$i+2]=="\xFF" && $new_block[$i+3]=="\xE0") {
$i += 4;
if($new_block[$i+2]=="\x4A" && $new_block[$i+3]=="\x46" && $new_block[$i+4]=="\x49" && $new_block[$i+5]=="\x46" && $new_block[$i+6]=="\x00") {
$block_size = unpack("H*", $new_block[$i] . $new_block[$i+1]);
$block_size = hexdec($block_size[1]);
while(!feof($handle)) {
$i += $block_size;
$new_block .= fread($handle, $block_size);
if($new_block[$i]=="\xFF") {
$sof_marker = array("\xC0", "\xC1", "\xC2", "\xC3", "\xC5", "\xC6", "\xC7", "\xC8", "\xC9", "\xCA", "\xCB", "\xCD", "\xCE", "\xCF");
if(in_array($new_block[$i+1], $sof_marker)) {
$size_data = $new_block[$i+2] . $new_block[$i+3] . $new_block[$i+4] . $new_block[$i+5] . $new_block[$i+6] . $new_block[$i+7] . $new_block[$i+8];
$unpacked = unpack("H*", $size_data);
$unpacked = $unpacked[1];
$height = hexdec($unpacked[6] . $unpacked[7] . $unpacked[8] . $unpacked[9]);
$width = hexdec($unpacked[10] . $unpacked[11] . $unpacked[12] . $unpacked[13]);
return array($width, $height);
} else {
$i += 2;
$block_size = unpack("H*", $new_block[$i] . $new_block[$i+1]);
$block_size = hexdec($block_size[1]);
}
} else {
return FALSE;
}
}
}
}
}
return FALSE;
}
?>
将图像转换为JPEG,请尝试以下方法:
<?php
$fileName = $_FILES["uploaded_file"]["name"];
$fileTmpLoc = $_FILES["uploaded_file"]["tmp_name"];
$fileType = $_FILES["uploaded_file"]["type"];
$fileSize = $_FILES["uploaded_file"]["size"];
$fileErrorMsg = $_FILES["uploaded_file"]["error"];
$fileName = preg_replace('#[^a-z.0-9]#i', '', $fileName);
$newExt = explode(".", $fileName);
$fileExt = end($newExt);
if (!$fileTmpLoc) {
echo "ERROR: Please browse for a file before clicking the upload button.";
exit();
} else if($fileSize > 5242880) {
echo "ERROR: Your file was larger than 5 Megabytes in size.";
unlink($fileTmpLoc);
exit();
} else if ($fileErrorMsg == 1) {
echo "ERROR: An error occured while processing the file. Try again.";
exit();
}
$moveResult = move_uploaded_file($fileTmpLoc, "uploads/$fileName");
if ($moveResult != true) {
echo "ERROR: File not uploaded. Try again.";
exit();
}
$target_file = "uploads/$fileName";
$resized_file = "uploads/resized_$fileName";
$wmax = 500;
$hmax = 500;
ak_img_resize($target_file, $resized_file, $wmax, $hmax, $fileExt);
if (strtolower($fileExt) != "jpg") {
$target_file = "uploads/resized_$fileName";
$new_jpg = "uploads/resized_".$newExt[0].".jpg";
ak_img_convert_to_jpg($target_file, $new_jpg, $fileExt);
}
echo "The file named <strong>$fileName</strong> uploaded successfuly.<br /><br />";
echo "It is <strong>$fileSize</strong> bytes in size.<br /><br />";
echo "It is an <strong>$fileType</strong> type of file.<br /><br />";
echo "The file extension is <strong>$fileExt</strong><br /><br />";
echo "The Error Message output for this upload is: $fileErrorMsg";
?>
为了更清晰地记录上述代码。
HTML
<div id="profilephoto" class="profilephoto" data-coupling="#content2"><img src="<?php echo $avatar ?>"></div>
<div id="content2" style="display: none;" class="uploadbutton" data-coupling="#content2">
<form id="imguploadin" method="POST" action="image_upload_script.php">
<label class="cabinet"> <i class="icon-upload-alt"></i> Choose Photo
<input type="file" name="uploaded_file" class="file" onchange="handleFiles(this.files)"/>
</label>
</form>
</div>
jQuery/JavaScript
function handleFiles(files) {
for (var i = 0; i < files.length; i++) {
var file = files[i];
var imageType = /image.*/;
if (!file.type.match(imageType)) {
continue;
}
var img = document.createElement("img");
img.classList.add("newphoto");
img.file = file;
profilephoto.appendChild(img);
var reader = new FileReader();
reader.onload = (function(aImg) { return function(e) { aImg.src = e.target.result; }; })(img);
reader.readAsDataURL(file);
var eventt = jQuery.Event("submit");
$("#imguploadin").trigger(eventt);
}
}
PHP(image_upload_script.php)
<?php
session_start();
$username = $_SESSION["username"];
$fileName = $_FILES["uploaded_file"]["name"];
$fileTmpLoc = $_FILES["uploaded_file"]["tmp_name"];
$fileType = $_FILES["uploaded_file"]["type"];
$fileSize = $_FILES["uploaded_file"]["size"];
$fileErrorMsg = $_FILES["uploaded_file"]["error"];
$kaboom = explode(".", $fileName);
$fileExt = end($kaboom);
if (!$fileTmpLoc) {
echo "ERROR: Please browse for a file before clicking the upload button.";
exit();
} else if ($fileSize > 1242880) {
echo "ERROR: Your file was larger than 5 Megabytes in size.";
unlink($fileTmpLoc);
exit();
} else if (!preg_match("/.(gif|jpg|png)$/i", $fileName)) {
echo "ERROR: Your image was not .gif, .jpg, or .png.";
unlink($fileTmpLoc);
exit();
} else if ($fileErrorMsg == 1) {
echo "ERROR: An error occured while processing the file. Try again.";
exit();
}
$moveResult = move_uploaded_file($fileTmpLoc, "images/users/$username.$fileExt");
if (!$moveResult) {
echo "ERROR: File not uploaded. Try again.";
unlink($fileTmpLoc);
exit();
}
include_once("ak_php_img_lib_1.0.php");
$target_file = "images/users/$username.$fileExt";
$resized_file = "images/users/resized_$username.$fileExt";
$wmax = 200;
$hmax = 150;
ak_img_resize($target_file, $resized_file, $wmax, $hmax, $fileExt);
$thumbnail = "images/users/thumb_$username.$fileExt";
$wthumb = 150;
$hthumb = 150;
ak_img_thumb($target_file, $thumbnail, $wthumb, $hthumb, $fileExt);
$sql_user = "myuser";
$sql_pass = "mypassword";
$sql_ip = "localhost";
$sql_DB = "mydb";
if ($moveResult != true)
echo("couldnt write file path to database due to unsuccesfull upload");
else {
$mysqli = new mysqli($sql_ip, $sql_user, $sql_pass, $sql_DB);
if ($stmt = $mysqli->prepare("UPDATE users SET avatar=? WHERE username=?")) {
$user = $_SESSION['username'];
$avatar = $resized_file;
$stmt->bind_param('ss', $avatar, $user);
$stmt->execute();
$stmt->close();
return true;
}
$mysqli->close();
}
echo "The file named <strong>$username.$fileExt</strong> uploaded successfuly.<br /><br />";
echo "It is <strong>$fileSize</strong> bytes in size.<br /><br />";
echo "It is an <strong>$fileType</strong> type of file.<br /><br />";
echo "The file extension is <strong>$fileExt</strong><br /><br />";
echo "The Error Message output for this upload is: $fileErrorMsg";
?>
您可以在此处找到编辑后的文档
source。
imagecreatefromstring(//上传文件内容//)
时,我会收到imagecreatefromstring():gd警告:内存分配乘法的一个参数为负数或零,优雅地失败操作的错误提示。 - Mladen Janjetovic