Archive for the 'PHP4' Category

Simple ajax tutorial

First time I heard about ajax it seems to be something very complicate, but
it depends on what do you want to achieve.

So there are several ajax libraries like script.aculo.us. But there are some more simple, I use XHConn library.

Ajax concept is based on XMLHttpRequest() javascript object which let you to load contents on a page without need to reload the entery page.

To develop simple pages using this library is needed XHConn.js and include_to.js files. Then imagine you have an index.php page with a textbox and a button. We want to load content in the textbox in a div.

index.php







What's you name?



In the head of the page we have needed libraries. Then in the boby an input text whose id is “name” and a button with an onclick property that contains the include_to function:
First parameter are the variables we send to the server, the second one is the page we send it to, the third is the destination where we load that page and the last is the image loader by default spinner.gif if empty.

Function getValue() in the first parameter returns the value of the element with passed id. In that case it returns the name we set in the input text box.

feedback.php

echo "My name is ".$_POST["name"].".";
?>

This page just echo’s the name.

Download example files here.

See it online.

El dinamisme web portat a l’extrem

Actualment estic treballant en una web on s’han de publicar fotos. Però això implica posar una foto petita de previsualització i una de mida més gran per ampliar.

Com us podeu imaginar això alenteix molt l’inserció de continguts. Doncs bé he trobat unes funcions que permeten manipular imatges. Aquestes funcions formen part de les llibrerires GD i permeten redimensionar, superposar imatges, rotar-les, etc.

Posaré un petit exemple de la potència d’aquestes funcions amb una funció que he trobat i permet inserir una imatge sobre una altre. La imatge de sobre queda transparent i li pots marcar la intensitat de manera que sigui com una marca d’aiga.

//Funció que sobreposa $src en la posició $dstx, $dsty amb una intensitat $pct de $dst
function imagecopyburn($dst,$src,$dstx,$dsty,$pct)
{

$w = imagesx($src);
$h = imagesy($src);
$i = 0; $j = 0; $k = 0; $rgb = 0;
$d = array(); $s = array();

for ($i=0; $i<$h; $i++) {

for ($j=0; $j<$w; $j++) {
$rgb=imagecolorat($dst,$dstx+$j,$dsty+$i);
$d[0] = ($rgb >> 16) & 0xFF;
$d[1] = ($rgb >> 8 ) & 0xFF;
$d[2] = $rgb & 0xFF;

$rgb = imagecolorat($src,$j,$i);
$s[0] = ($rgb >> 16) & 0xFF;
$s[1] = ($rgb >> 8 ) & 0xFF;
$s[2] = $rgb & 0xFF;

$d[0] -= max($d[0]-$s[0],0)*$pct/100;
$d[1] -= max($d[1]-$s[1],0)*$pct/100;
$d[2] -= max($d[2]-$s[2],0)*$pct/100;

imagesetpixel(
$dst, $dstx+$j, $dsty+$i,
imagecolorallocate($dst,$d[0],$d[1],$d[2])
);

}

}
return $dst;

}

//Funció que retorna una imatge que conté un text d’error
function error(){

/* See if it failed */
$im = imagecreatetruecolor(150, 40); /* Create a black image */
$bgc = imagecolorallocate($im, 255, 255, 255);
$tc = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 150, 40, $bgc);
/* Output an errmsg */
imagestring($im, 2, 5, 5, “El fitxer no existeix”, $tc);
return $im;

}
//Funció que carrega un fitxer jpg
function LoadJpeg($imgname)
{

$im = @imagecreatefromjpeg($imgname); /* Attempt to open */
if (!$im) { /* See if it failed */
$im=error();
}
return $im;

}
//Enviem la capçalera de la imatge
header(‘Content-type: image/jpeg’);
//Carreguem les imatges destí($dst) i orgiren($src)
$dst=LoadJpeg($imgname1);
$src=LoadJpeg($imgname2);
//Posem $src sobre $dst en l’extrem superior esquerra amb una intensitat del 75%
$im=imagecopyburn($dst,$src,0,0,75);
//Finalment enviem la imatge al navegador
imagejpeg($im, null, 100);
?>

Últims Twitts

Posting tweet...