在PHP上传图片而无需手动提交表单

I have a image that is merged using php (this is in localhost). I want to automatically upload this file to a server php file that will accept it or upload it to a server folder.

The normal way is to use a form with multipart then submit form to upload it. I need to pass the image itself to the php or submit form automatically without having to do it manually.

I tried to create a directory pictures set permission to 777 and try to save the image to server

$outfile = "http://XXXXXX.com/pictures/testing.png";
$quality = 100;
imagejpeg($output,$outfile,$quality);

didn't work

Update: Tried the canvas todataurl in localhost, it works fine but getting error when trying it on server giving

XMLHttpRequest cannot load http://xxx.com/upload_img. Origin http://localhost is not allowed by Access-Control-Allow-Origin.

So I tried to check out how to allow access origin this but don't understand how to do it. Can someone point me to a good simple tutorial on how to do this, thanks.

I suggest you to use javascript to submit the form automatically and put that image upload input type within the form tag you are submitting automatically. javascript code will be some thing like this <script language="JavaScript">document.formname.submit();</script>

I believe you have to use a Path eg( 'C:\path\filename' ) and NOT a url ( http://example.com ) for the imagejpeg function

So found the solution using jquery $.post with Access-Control-Allow-Origin

Javascript

var canvas = document.getElementById('myCanvas');
  var context = canvas.getContext('2d');
  var imageObj = new Image();
  imageObj.src = 'sample.jpg';
  imageObj.onload = function() {
    canvas.width = imageObj.width;
    canvas.height = imageObj.height;
    context.drawImage(imageObj, 0, 0);

    var strDataURI = canvas.toDataURL();
    strDataURI = strDataURI.substr(22, strDataURI.length);

    $.post("http://xxxx.com/upload_img",
    { 
        str: strDataURI
    },
    function(data){
     //to check if any error occurs
        alert(data);
    });

Php file that will accept the image got the php allow-control-allow-access solution here

// * can be set to something else such as http://example.com/
header('Access-Control-Allow-Origin: *');

$data = base64_decode($_POST["str"]);

$urlUploadImages = "./uploads/";
$nameImage = "test.jpg";

$img = imagecreatefromstring($data);

imagejpeg($img, $urlUploadImages.$nameImage, 100);
imagedestroy($img);