Upload image from stack to database using php webservice

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
razvan
Posts: 19
Joined: Tue Feb 24, 2015 10:16 am

Upload image from stack to database using php webservice

Post by razvan » Thu Sep 10, 2015 8:07 am

Hello!
I'am beginner in programing and I want to learn how can I put a image from my app in livecode, into database (blob type), using php webservice?
For take picture from database it's working with this function in php which call in livecode :

Code: Select all

<?php
/**
* @mainpage
* This documentation provides information about the API.
*
* More Information about Doxygen can be found here:


/**
 * @file   index.php
 * @brief  Main part of the web service
 * @date   Jul 16, 2015
 * @author Nicola Liebi <nicola.liebi@patrickliebi.ch>
 */
require 'db.php';
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();

// Initialize App
$app = new \Slim\Slim();

// URL Routing
$app->get('/image/:id','getImage');

// Run App
$app->run();

 
/**
 * GET link/api_razvan/image/:ID
 *
 * Get image  by id
 *
 * @return          ID
 */
function getImage($id) {
	try {
		$link = mysqli_connect("localhost", "XXXXXX", "XXXXXXX","XXXXXX");
		$sql = "SELECT picture FROM testObject WHERE oID=$id";
		$res = mysqli_query($link,$sql);
		if($res)
		   {
			   $row = mysqli_fetch_assoc($res);
			   echo $row['picture'];
		   }
	} catch(PDOException $e) {
		echo '{"error":{"text":'. $e->getMessage() .'}}';
	}
}

code in livecode :

Code: Select all

on takeimageweb
   global tIDimage
   if tIDimage is a integer or tIDimage is not empty
   then 
      put "link/api_razvan/image/"& tidImage into temp
      put url temp into tImage
      set the text of image "imagecontainer" to tImage
   else 
      answer "No row selected!"
   end if
   if tImage is empty
   then 
      answer "Picture not found!"
   end if
end takeimageweb
This methode don't working when I want to upload a picture to database.
Thank you!
Regards!
Razvan.

zaxos
Posts: 222
Joined: Thu May 23, 2013 11:15 pm
Location: Greece

Re: Upload image from stack to database using php webservice

Post by zaxos » Fri Sep 11, 2015 4:13 pm

I'm not sure if i understand correctly, first of all, why dont you upload the picture from within livecode? if for any reason thats not what you want then you would have to look for the POST command in the dictionary if you want to send data to a web-service and of course you will have to make the appropriate script in php to accept and send the data to MySQL.
EX:

Code: Select all

POST theImage to URL "http://www.myPage.com/uploadImage.php"
of course you should add some user verification for security reasons, but for now just make your php script and upload the picture, if you need any help with it let us know.
Knowledge is meant to be shared.

razvan
Posts: 19
Joined: Tue Feb 24, 2015 10:16 am

Re: Upload image from stack to database using php webservice

Post by razvan » Fri Nov 13, 2015 7:23 am

Hello Zaxos.
I tried post method ,but still not working.
Can you give me a simple example ?
If is not too much,please atach the stack and php file.
This is my try:

1) create function/url routing in PHP:

$app->post('/blop', 'insertBlop');

function insertBlop($id, $blop) {
$sql = "INSERT INTO images (id, blop) VALUES (?,?)";
try {
$db = getDB();
$stmt = $db->prepare($sql);
$stmt->execute(array(
$id,
$blop
));
$db = null;
echo true;
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}

2) call the site from livecode

on mouseUp
put image "61_148.jpg" into tBlop
put 22 into tId
put "id=" & tId & "&blop=" & tBlop into tArgList
post tArgList to URL "http://my.web.site/blop"
put it into tFormResults
answer tFormResults
end mouseUp

Thank you.
Razvan

zaxos
Posts: 222
Joined: Thu May 23, 2013 11:15 pm
Location: Greece

Re: Upload image from stack to database using php webservice

Post by zaxos » Tue Nov 17, 2015 1:33 pm

Hey there razvan, believe i missleaded you, try something like that
PHP Code:

Code: Select all

<?php
$image = $_GET['image'];
if($image) {
   Your php upload code.
}
else {
    echo '<p>No Image.</p>';
}
?>
LC Code

Code: Select all

   put base64Encode(img 1) into tImageData
   put URL "http://localhost/test/test.php?image="&tImageData into tResult
   answer tResult
Knowledge is meant to be shared.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”