<?php
ini_set("max_execution_time", "30000");
/**
* GENERAL FUNCTIONS
*/
function nadjiVrijednosti($byte1, $byte2){
$byte1 = hexdec(bin2hex($byte1));
$byte2 = hexdec(bin2hex($byte2));
return ($byte1 + ($byte2*256));
}
/**
* Great function slightly modified as posted by Minux at
*
http://forums.clantemplates.com/showthread.php?t=133805 */
function html2rgb($input) {
$input=($input[0]=="#")?substr($input, 1,6):substr($input, 0,6);
return array(
hexdec( substr($input, 0, 2) ),
hexdec( substr($input, 2, 2) ),
hexdec( substr($input, 4, 2) )
);
}
if (isset($_FILES["mp3"])) {
$filename = $_FILES["mp3"]["name"];
//$filename = "http://localhost/firstsight.wav";
$handle = fopen ($filename, "r");
//dohvacanje zaglavlja wav datoteke
$zaglavlje[] = fread ($handle, 4);
$zaglavlje[] = bin2hex(fread ($handle, 4));
$zaglavlje[] = fread ($handle, 4);
$zaglavlje[] = fread ($handle, 4);
$zaglavlje[] = bin2hex(fread ($handle, 4));
$zaglavlje[] = bin2hex(fread ($handle, 2));
$zaglavlje[] = bin2hex(fread ($handle, 2));
$zaglavlje[] = bin2hex(fread ($handle, 4));
$zaglavlje[] = bin2hex(fread ($handle, 4));
$zaglavlje[] = bin2hex(fread ($handle, 2));
$zaglavlje[] = bin2hex(fread ($handle, 2));
$zaglavlje[] = fread ($handle, 4);
$zaglavlje[] = bin2hex(fread ($handle, 4));
//bitrate wav datoteke
$peek = hexdec(substr($zaglavlje[10], 0, 2));
$bajta = $peek / 8;
//provjera da li se radi o mono ili stereo wavu
$kanala = hexdec(substr($zaglavlje[6], 0, 2));
if($kanala == 2){
$omjer = 40;
}
else{
$omjer = 80;
}
while(!feof($handle)){
$bytes = array();
//get number of bytes depending on bitrate
for ($i = 0; $i < $bajta; $i++){
$bytes[$i] = fgetc($handle);
}
switch($bajta){
//get value for 8-bit wav
case 1:
$data[] = nadjiVrijednosti($bytes[0], $bytes[1]);
break;
//get value for 16-bit wav
case 2:
if(ord($bytes[1]) & 128){
$temp = 0;
}
else{
$temp = 128;
}
$temp = chr((ord($bytes[1]) & 127) + $temp);
$data[]= floor(nadjiVrijednosti($bytes[0], $temp) / 256);
break;
}
//skip bytes for memory optimization
fread ($handle, $omjer);
}
// close and cleanup
fclose ($handle);
//unlink("$filename");
/**
* Image generation
*/
header("Content-Type: image/png");
// how much detail we want. Larger number means less detail
// (basically, how many bytes/frames to skip processing)
// the lower the number means longer processing time
define("DETAIL", 0.01);
// get user vars from form
$width = isset($_POST["width"]) ? (int) $_POST["width"] : 500;
$height = isset($_POST["height"]) ? (int) $_POST["height"] : 50;
$foreground = isset($_POST["foreground"]) ? $_POST["foreground"]: "brown";
$background = isset($_POST["background"]) ? $_POST["background"]: "blue";
// creat@e original image width based on amount of detail
$img = @imagecreatetruecolor(sizeof($data) / DETAIL, $height) or die('Cannot Initialize new GD image stream');
// fill background of image
list($r, $g, $b) = html2rgb($background);
imagefilledrectangle($img, 0, 0, sizeof($data) / DETAIL, $height,imagecolorallocate($img, 255, 255, 255));
// generate background color
list($r, $g, $b) = html2rgb($foreground);
// loop through frames/bytes of wav data as genearted above
for($d = 0; $d < sizeof($data); $d += DETAIL) {
// relative value based on height of image being generated
// data values can range between 0 and 255
$v = (int) ($data[$d] / 225 * 80);
// draw the line on the image using the $v value and centering it vertically on the canvas
imageline($img, $d / DETAIL, 0 + ($height - $v), $d / DETAIL,$height - ($height - $v), imagecolorallocate($img, 163, 168, 171));
}
// want it resized?
if ($width) {
// resample the image to the proportions defined in the form
$rimg = imagecreatetruecolor($width, $height);
imagecopyresampled($rimg, $img, 0, 0, 0, 0, $width, $height,sizeof($data) / DETAIL, $height);
imagepng($rimg);
imagedestroy($rimg);
} else {
// print out at it's raw width (size of $data / detail level)
imagepng($img);
imagedestroy($img);
}
} else {
?>
<form method="post" action="" enctype="multipart/form-data">
<p>MP3 File:<br />
<input type="file" name="mp3" /></p>
<p>Image Width:<br />
<input type="text" name="width" value="500" /></p>
<p>Image Height:<br />
<input type="text" name="height" value="50" /></p>
<p>Foreground Color: <small>(HEX/HTML color code)</small><br />
<input type="text" name="foreground" value="#FF0000" /></p>
<p>Background Color: <small>(HEX/HTML color code)</small><br />
<input type="text" name="background" value="#FFFFFF" /></p>
<p><input type="submit" value="Generate Waveform" /></p>
</form>
<?php
}
?>
By Using the above code I have generated waveform image<embed src="firstsight.wav" height="100" widt="500"></embed>
By using above html i am running .wav file.Now i want to place waveform image as background while i am running this firstsight.wav