There are a couple ways to use the image class. Images can be grabbed from the web, album, locally.
Options for source image:
- camera: The camera app is opened. Once the user takes a snapshot and saves, the new image is obtained.
- library: The image picker view is pulled up, allowing the user to select a photo from the photo library.
- web: An image is grabbed externally from the web.
Options to save image:
- local: Image will be saved locally.
- album: Images will be saved to the Saved Photos album.
- url: A path to the PHP upload script. Images will be sent from the iPhone and uploaded to the server.
Example of Library to Local
Open the photo library. When a photo is selected, it will be saved locally as the filename selected.
function openLibraryToLocal(){
lg.bind('lg_image_library', getImage);
lg.image.library({'url':'local', 'filename':'myPhoto'+g+'.jpg'});
}
//An example of setting an image retrieved as a background to a div.
function getImage(e){
$('#boxPhoto').css('backgroundImage', 'url(' + lg.image.file.url +')');
}
Example of Web to Album
Get an image from the web. When downloaded, save to the user album.
function openWebToAlbum(){
lg.bind('lg_image_web', getImage);
lg.image.web({'url':'album', 'filename':'http://www.liquidgear.net/img/logo.png', 'quality':'1'});
}
//An example of setting an image retrieved as a background to a div, using jQuery.
function getImage(e){
$('#boxPhoto').css('backgroundImage', 'url(' + lg.image.file.url +')');
}
Example of PHP Upload Script
Sample script that will upload an image from the iPhone to a server.
<?
$uploadDir = 'upload/';
if ($_FILES["file"]["error"] > 0) {
echo "{'error':{'desc':".$_FILES['file']['error']."'}";
} else {
move_uploaded_file($_FILES['file']['tmp_name'], 'upload/' . $_FILES['file']['name']);
echo "{'file':{";
echo "'url':'http://localhost/tmp/".$uploadDir . $_FILES['file']['name']."',";
echo "'name':'".$_FILES['file']['name']."',";
echo "'type':'".$_FILES['file']['type']."',";
echo "'size':'".round($_FILES['file']['size']/1024, 0)." kb'";
echo "}, 'type':'".$_REQUEST['type']."'}";
}
?>
PHP Test Page
If you are having trouble uploading an image, try usin the HTML page below to test to ensure the PHP script is working correctly. Be sure to check the permissions as well.
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>File Upload Form</title>
</head>
<body>
This form allows you to upload a file to the server.<br/>
<form action="upload.php" method="post" enctype="multipart/form-data">
Type (or select) filename:
<input type="hidden" name="type" value="library"/>
<input type="file" name="file"/>
<input type="submit" value="Upload File"/>
</form>
</body>
</html>
