因此,我将其设置为通过 Phonegap 本机 api 从设备图像/相机上传图像,并在图像名称上附加随机数。我试图弄清楚如何访问服务器上图像位置的最终 url,以便我可以在制作 Facebook 帖子时利用该图像的 url,因为它们需要外部上传的图像。
代码:.php
<?php
$dir = "upload";
$randomImg = rand(1, 9999)."image".rand(1, 999999).".jpg";
if ($_FILES) {
print_r($_FILES);
mkdir ($dir, 0777, true);
move_uploaded_file($_FILES["file"]["tmp_name"],$dir."/".$randomImg);
}
else if (isset($_GET['image'])) {
$image = $dir."/".$_GET['image'];
header('Content-type: image/jpeg');
list($width, $height) = getimagesize($image);
$newWidth = 120.0;
$size = $newWidth / $width;
$newHeight = $height * $size;
$resizedImage = imagecreatetruecolor($newWidth, $newHeight);
$image = imagecreatefromjpeg($image);
imagecopyresampled($resizedImage, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
imagejpeg($resizedImage, null, 80);
}
else {
$images = scandir($dir);
$ignore = Array(".", "..");
$baseURI = "http://".$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$_SERVER['REQUEST_URI'];
if ($images) {
foreach($images as $curimg){
if (!in_array($curimg, $ignore)) {
echo "Image: ".$curimg."<br>";
echo "<img src='".$baseURI."?image=".$curimg."&rnd=".uniqid()."'><br>";
}
}
}
else {
echo "We apologize, but there are no images found on our server at this time.";
}
}
?>
.html
<section>
<input value="Upload Photo" id="uploadBtn" type="button" onclick="uploadImg();" />
</section>
.js
function uploadImg() {
var img = document.getElementById('camImg');
var imageURI = img.src;
if (!imageURI || (img.style.display === "none")) {
$('#status').html("Snap a photo or select one from your gallery by clicking the 'Attach Photo...' button").css('color', 'red');
return;
}
paramount = "http://www.param0unt.com/app/upload.php";
if (paramount) {
var opts = new FileUploadOptions();
opts.fileKey = "file";
opts.fileName = imageURI.substr(imageURI.lastIndexOf('/') + 1);
opts.mimeType = "image/jpeg";
opts.chunkedMode = false;
var media = new FileTransfer();
media.upload(imageURI, paramount, function(r) {
$('#status').html("Photo successfully stored on our server! (<font color='red'>NOTE:</font> This is alpha version, you will be able to utilize these photos via facebook group ad posts in an upcoming beta release!)").css('color', 'green');
}, function(error) {
$('#status').html("Photo Upload Failed, please try again later.").css('color', 'red');
//console.log(error.code);
}, opts);
}
}