The14thGOD Posted May 9, 2012 Share Posted May 9, 2012 I apologize if I am using the wrong terminology here... I am trying to take an image that is dragged and dropped and then uploading the image to a server to do other things with it. I've found very generic example(s) that can just move the file, but I need to be able to manipulate it on the server. AJAX cut-out code: var xhr = new XMLHttpRequest(); if(xhr.upload && file.type == 'image/jpeg' || file.type == 'image/png'){ xhr.open('POST', 'path/to/script.php', true); xhr.setRequestHeader('X_FILENAME', file.name); xhr.send(file); } The above part works but when I try to manipulate it with PHP it doesn't see it as a typical $_FILES variable. Ideally I would like to get it to be like that, if not I understand I will have to change stuff. Here is my PHP that handles the above call: <?php $slide = new Slide(); //echo gettype($_SERVER['HTTP_X_FILENAME']); //str $file = file_get_contents('php://input'); $image = base64_decode($file); if($slide->add_image_to_slide($_GET['sid'],$image)){ echo 'yay'; }else{ echo $slide->error; } Object Code: <?php public function add_image_to_slide($id,$image){ //Verify image before proceeding forward $accepted_file_types = array('jpg','png'); $ext = explode('.',$image['name']); if(array_search($ext[1],$accepted_file_types) !== false) { //variables $this->id = $id; $this->select_slide($this->id); //make sure that the folders for the images to go actually exist, if not, create them $this->create_folders(); $this->generate_thumbnail($image); $this->thumbnail = $this->upload_directory.$this->rand_id.'_thumb_'.$this->filename; $this->full_img = $this->upload_directory.$this->rand_id.'_'.$this->filename; // thumbnail is moved automatically by function above, just move full image move_uploaded_file($image['tmp_name'], BASE_ROOT.'/'.$this->full_img); if($stmt = $this->_db->prepare('UPDATE slides SET full_img = ? , thumbnail = ? WHERE id = ?')){ $stmt->bind_param('ssi',$this->full_img,$this->thumbnail,$id); $stmt->execute(); $stmt->close(); return true; } return false; } $this->error = 'File Extension Error: .jpg, .png allowed only'; return false; } Right now it's skipping straight to $this->error since it's not an image object. I'm not really sure what to pursue from here. I did try this line once but it also skipped to the error line: <?php $image = createimagefromstr(file_get_contents('php://input')); Any help would be greatly appreciated! Thanks, Justin Quote Link to comment https://forums.phpfreaks.com/topic/262316-need-help-converting-ajax-img-upload-to-img-object/ Share on other sites More sharing options...
The14thGOD Posted May 9, 2012 Author Share Posted May 9, 2012 nvm, found a different way to approach it, in case anyone runs into a similar issue, here is the solution: http://stackoverflow.com/a/5976031 Quote Link to comment https://forums.phpfreaks.com/topic/262316-need-help-converting-ajax-img-upload-to-img-object/#findComment-1344346 Share on other sites More sharing options...
waynew Posted May 9, 2012 Share Posted May 9, 2012 Thank you for posting the solution. Months from now, somebody will mentally thank you for it. (Unlike those guys that say "It's OK, I found a solution" and never elaborate. Quote Link to comment https://forums.phpfreaks.com/topic/262316-need-help-converting-ajax-img-upload-to-img-object/#findComment-1344350 Share on other sites More sharing options...
The14thGOD Posted May 9, 2012 Author Share Posted May 9, 2012 yeah, i hate when that happens, hopefully it'll be of use to someone in the future (came back to mark topic solved as I forgot to) Quote Link to comment https://forums.phpfreaks.com/topic/262316-need-help-converting-ajax-img-upload-to-img-object/#findComment-1344351 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.