GOTO

Showing posts with label Php. Show all posts
Showing posts with label Php. Show all posts

Wednesday, September 1, 2010

Stretching...with Php!!

Hi everybody,

are you sports? Well, so you know for sure the stretching...a programmer too, but he think immediately to pictures' stretching... What is this? Pictures' stretching is a set of operations of elongation, shrinking and many other applied to pictures...Usually you need to fit a picture in a smaller image using mathematical proportions...It works, obvious, but If you try this, you'll note that this image is very distorted! There's nothing to do, the only case where the final pictures looks not distorted is when the big image's dimensions ( width and height ) are multiples of the small picture's ones, but this is not always possible..If you have to create thumbnails with php this is not the right way..Keeping in mind the story of multiple dimensions, let's try to proceed in this other way: first we take the big picture and select an area centered in the image as big as the destination image ( yellow rectangle )..


This area is very small...let's try to multiply by 2 both width and height..We get this:


Quite as big as the image...multiplying now by 3 the area will exceed the image, so we consider the area * 2, an area proportioned to the final pictures...What remains to do? Stretch this area and fit it into our target picture, getting:


Here is the code:


function makeThumbNotStretched($src_file,$dest_file,$t,$win_w,$win_h){

@list($width, $height, $type, $attr) = @getimagesize($src_file);

$cw=$width/2;

$ch=$height/2;

$i=1;

$w=$cw-(($win_w*$i)/2);
$h=$ch-(($win_h*$i)/2);

while($w>=0 && $h>=0){
$i++;
$w=$cw-(($win_w*$i)/2);
$h=$ch-(($win_h*$i)/2);
}

$i--;
$w=$cw-(($win_w*$i)/2);
$h=$ch-(($win_h*$i)/2);

$dest = imagecreatetruecolor($win_w*$i, $win_h*$i);

if ($t == 'image/jpeg')
$src = @imagecreatefromjpeg($src_file);

imagecopy($dest,$src,0,0,$w,$h,$win_w*$i,$win_h*$i);

$thumb = imagecreatetruecolor($win_w, $win_h);
imagecopyresampled($thumb,$dest,0,0,0,0,$win_w,$win_h,$win_w*$i,$win_h*$i);

if ($t == 'image/jpeg')
@imagejpeg($thumb,$dest_file, 75);
}


::See you next post::

Thursday, May 20, 2010

How are stored passwords in Joomla 1.5?

Hi everybody,

my community "toworktogether" created with Joomla is coming at the end: now I'm working on an application (expandable) to offer some utilities to administrators. This application requires a login, so we have to implement that, but there's a problem...Looking at jos_users table in my database, I discovered that passwords are not stored simply using md5, but by 1.5 version when the password is created, it's hashed with a 32 character salt that is appended to the end of the password string. Finally, the password is stored as {TOTAL HASH}:{ORIGINAL SALT}. So I report my Php script for verifying if the login is right (I use a library for executing queries and password and username are passed using post).

$mydb=new Database("db_host","db_user","db_psw","db_name");
$cred=$mydb->doQuery("SELECT username,password FROM jos_users where username='".$_POST['user']."'");

$row=mysql_fetch_array($cred);

if($row['username']==""){
     //Username not found. Exit script..
}

$psw=$_POST['psw'];
$field=split(":",$row['password']);

$md5=$field[0];
$salt=$field[1];

if(md5($psw.$salt)==$md5)
{
     //Password verified. Access to service..
}

else
{
     //Password is wrong. Exit script..
}
This is a simple guideline, obviously if you wanna implement something that uses login with Joomla users, you have to use sessions.

::See you next post::

Wednesday, May 12, 2010

Updating your Twitter status from your application/site

Hi everybody,

I'm developing a website using Joomla for my university about the world of work. A website like that is frequently updated and must also have a Facebook page, a Gtalk account and a Twitter profile for extending its communicability . How can we use these two social networks? Well, we can post events, news and communications taken from our website, but it's a very long and boring work to do manually. So we must automatize this process with an application (in this case a web application written in PHP). Let's start talking about how to update Twitter status..Twitter offers many APIs to developers ( for further information visit http://apiwiki.twitter.com/ ) for authenticating,reading updates, posting messages, responding...So you can make all what you want but without using Twitter. Ok, we are only interested in posting message, so the API we are interested in is http://twitter.com/statuses/update.json. See an example:
$out = "POST http://twitter.com/statuses/update.json HTTP/1.1\r\n"
."Host: twitter.com\r\n"
."Authorization: Basic ".base64_encode ('user_name:user_password')."\r\n"
."Content-type: application/x-www-form-urlencoded\r\n"
."Content-length: ".strlen ("status=$msg")."\r\n"
."Connection: Close\r\n\r\n"
."status=$msg";

$fp = fsockopen ('twitter.com', 80);
fwrite ($fp, $out);
fclose ($fp);
Remember to change user_name:user_password with its real value (':' character is required) and set $msg with data taken from your data source (files, databases...).
You can also call this API using curl by your command line, so you can integrate Twitter functionalities in your applications:
curl -u 'user_name:user_password' -d status="Testing twitter API by console" http://twitter.com/statuses/update.xml
Once updated twitter status, you can update Facebook and Buzz statuses too, installing an application for Facebook for reading your status on Twitter (I suggest you use smart twitter) and adding a reference in Buzz to your Twitter profile.

Data source ---> API elaboration ---> Twitter ---> Facebook and Buzz.

::See you next post::

Thursday, May 6, 2010

An alternative to audio tag

Hi everybody,

last week I had a problem with the new tag audio included in html5: I wasn't able to reproduce mp3 files in Firefox browser. I've reported this in bugzilla and Mozilla's staff replied that Mozilla haven't the right licence...
I've discover another tag named "embed". I would report an alternative method for playing mp3 using this new tag and dynamic html:

HTML code

<span id=player></span>
<form>
<input type="button" value="Play my song" onClick="Play('Panama.mp3')">
</form>

Javascript code

function Play(url) {
document.getElementById("player").innerHTML=
"<embed src='"+url+"' hidden=true autostart=true height=0 width=0
loop=false>";
}

Setting height and width parameters to 0 to hide the player. How I said some posts ago, mp3 files are simply to concatenate like wav, but have small sizes. This method is also comaptible with Chrome, Opera, Explorer and, I think, Safari ( I can't try on it :) ).

::See you next post::

Tuesday, April 20, 2010

Concatenating audio files in php

Hi everybody,

yesterday I was working on a spelling function in php. The base idea was taking each letter of the word to spell and playing the relative sound (a voice that pronounces a letter). Letter's index and word's name was passed by GET method.

session_start();
$in=$_GET['index'];
$var=$_SESSION[$_GET['name']];
$filename = $var[$in].".mp3";

header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-disposition: attachment; filename=sound');
header("Content-Type: audio/mp3");
header("Content-Transfer-Encoding: binary");
header('Content-Length: '. filesize($filename));
readfile($filename);


So, in that case, I needed to create an audio tag ( I'm updated, I'm using html5 :) ) for each audio file and play them using javascript. But there's another more simple way to do what I wanted: concatenate all files! How can we do that? It's very simple, we have only to call readfile function for each file and set the content length as the sum of all file sizes. Here is an example (2 files to spell 'hi!'):

session_start();
$filename1 = "h.mp3";
$filename2 = "i.mp3";

$size=filesize($filename1)+filesize($filename2);

header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-disposition: attachment; filename=sound');
header("Content-Type: audio/mp3");
header("Content-Transfer-Encoding: binary");
header('Content-Length: '.$size);
readfile($filename1);
readfile($filename2);


If you want to use audio tags, remember that mp3 are only supported by Chrome and Safari4. Firefox and Opera supports wave files. Don't try to use ogg files: concatenating operation isn't so simple..There are also some problems with the use of audio tags, but...I'll deal with it in another post...

::See you next post::