Jump to content

need help converting ajax img upload to img object


The14thGOD

Recommended Posts

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.