Jump to content

[SOLVED] call a php function and return the value?


Dragen

Recommended Posts

Hi,

I've got a php script which takes an x and y coordinate and gets the pixel colour from an image with those coordinates:

<?php
class getcolor{
function getcolor($x, $y){
	$image  = @imagecreatefrompng('palette.png');
	$x  = (integer) $x;
	$y  = (integer) $y;

	if($image){
		$color  = ImageColorAt($image, $x, $y);
		$r  = ($color >> 16) & 0xFF;
		$g  = ($color >>  & 0xFF;
		$b  = $color & 0xFF;

		$this->color = $this->gethex($r, $g, $b);
	}else{
		return false;
	}
}

function gethex($r, $g, $b){
	return strtolower(sprintf('#%02X%02X%02X', $r, $g, $b));
}
}

$getcolor = new getcolor(50, 86);
// the colour is stored as hex in the variable: $getcolor->color;
?>

 

Now what I want to do is use ajax to create an onclick event which calls up the getcolor function with the specified x and y, then retrieves the given value.

 

How can I send and recieve from a php file with ajax? I've seen something in javascript about the call metho, but am unsure if this is the right direction.

 

Thanks

Link to comment
Share on other sites

nevermind. I've figured it out.. javascript syntax is really driving me nuts though ;)

 

here's my solution:

 

getcolor.php:

<?php
class getcolor{
function getcolor($x, $y){
	$this->color = 'n/a';
	$image  = @imagecreatefrompng('palette.png');
	$x  = (integer) $x;
	$y  = (integer) $y;

	if($image){
		$color  = ImageColorAt($image, $x, $y);
		$r  = ($color >> 16) & 0xFF;
		$g  = ($color >>  & 0xFF;
		$b  = $color & 0xFF;

		$this->color = $this->gethex($r, $g, $b);
	}
}

function gethex($r, $g, $b){
	return strtolower(sprintf('#%02X%02X%02X', $r, $g, $b));
}
}

if((isset($_GET['x']) && isset($_GET['y'])) && (ctype_digit($_GET['x']) && ctype_digit($_GET['y']))){
$getcolor = new getcolor($_GET['x'], $_GET['y']);
}else{
$getcolor->color = 'n/a';
}

echo $getcolor->color;
?>

as you can see, at the end I echo out the variable.

 

Then I have an onclick event which calls the javascript getcolor function:

<a href="#" onclick="javascrip:javascript:getcolor(X, Y);">click here</a> //x and y are my x and y coordinates

The javascript looks like this:

var xmlhttp

function getcolor(x, y){
xmlhttp=null
// code for Mozilla, etc.
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest()
  }
// code for IE
else if (window.ActiveXObject)
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
  }
  
if (xmlhttp!=null)
  {
  xmlhttp.onreadystatechange=state_Change
  xmlhttp.open("GET","/admin/includes/getcolor.php?x="+x+"&y="+y,true)
  xmlhttp.send(null)
  }
else
  {
  alert("Your browser does not support XMLHTTP.")
  }
}

function state_Change()
{
// if xmlhttp shows "loaded"
if (xmlhttp.readyState==4)
  {
  if (xmlhttp.status==200)
    {
	var response = xmlhttp.responseText;
		alert('response: '+response);
    }
  else
    {
    alert("Problem retrieving XML data")
    }
  }
}

 

Which correctly gives me anything that is output from my php file. All I need to do now is get it to put the value into a text input, which should be fairly easy.

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.