Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've been working with the upload feature since the second iOS6 beta release. The following code works for me:</p> <p>Put this in the head of your HTML page -</p> <pre><code>&lt;script&gt;window.onload = function() { var canvas = document.createElement('canvas'); var ctx = canvas.getContext("2d"); var fileSelect = document.getElementById("fileSelect"), input = document.getElementById("input"); input.addEventListener("change", handleFiles); //hides ugly default file input button fileSelect.addEventListener("click", function (e) { if (input) { input.click(); } e.preventDefault(); }, false); function handleFiles(e) { var reader = new FileReader; reader.onload = function (event) { var img = new Image(); img.src = reader.result; img.onload = function () { var maxWidth = 320, maxHeight = 350, imageWidth = img.width, imageHeight = img.height; if (imageWidth &gt; imageHeight) { if (imageWidth &gt; maxWidth) { imageHeight *= maxWidth / imageWidth; imageWidth = maxWidth; } } else { if (imageHeight &gt; maxHeight) { imageWidth *= maxHeight / imageHeight; imageHeight = maxHeight; } } canvas.width = imageWidth; canvas.height = imageHeight; ctx.drawImage(this, 0, 0, imageWidth, imageHeight); // The resized file ready for upload var finalFile = canvas.toDataURL("image/png"); var postData = 'canvasData=' + finalFile; var ajax = new XMLHttpRequest(); ajax.open('POST', 'save.php', true); ajax.setRequestHeader('Content-Type', 'canvas/upload'); ajax.onreadystatechange = function () { if (ajax.readyState == 4) { //just to visually confirm it worked... window.open(canvas.toDataURL("image/png"), "mywindow"); } } ajax.send(postData); } } reader.readAsDataURL(e.target.files[0]); } } &lt;/script&gt; </code></pre> <p>Here's the HTML -</p> <pre><code> &lt;div style="width:320px;position:absolute;z-index:9;top:387px;"&gt; &lt;button style="width:60px;" id="fileSelect"&gt;upload&lt;/button&gt; &lt;input type="file" id="input" name="input" accept="image/*" style="display:none;"&gt;&lt;/div&gt; </code></pre> <p>Here's the PHP -</p> <pre><code>&lt;?php if (isset($GLOBALS["HTTP_RAW_POST_DATA"])) { // Get the data $imageData=$GLOBALS['HTTP_RAW_POST_DATA']; // Remove the headers (data:,) part. // A real application should use them according to needs such as to check image type $filteredData=substr($imageData, strpos($imageData, ",")+1); // Need to decode before saving since the data we received is already base64 encoded $unencodedData=base64_decode($filteredData); // Save file. This example uses a hard coded filename for testing, // but a real application can specify filename in POST variable $fp = fopen( 'users/user_photo.png', 'wb' ); fwrite( $fp, $unencodedData); fclose( $fp ); } ?&gt; </code></pre> <p>The only issue I've battled with is getting images to load from the camera without being rotated 90 degrees.</p> <p>Hope this helps, let me know if you've any issues with the code (it's my first post).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload