Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => Images

problem with wave form image

images php coding issues or problems here.

Moderators: macek, egami, gesf

problem with wave form image

Postby roopanimmagadda » Wed Mar 16, 2011 4:50 am

I have successfully executed generating waveform image using MP3/Wav files.
But my code is occupying more memory when i run the file.
Code:
<?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"])) {

$name=$_FILES["mp3"]["name"];
$fname = explode(".",$fname);
$tmpname = $fname[0];
exec(lame $tmpname.mp3 $tmpname.wav);
$filename = "$tmpname.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", 5);

// get user vars from form
$width = isset($_POST["width"]) ? (int) $_POST["width"] : 500;
$height = isset($_POST["height"]) ? (int) $_POST["height"] : 100;
$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] / 130 * $height);
// 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="100" /></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

}
?>
After execution when i stop httpd service and start automatically it is executing the above file.And memory size is increasing.
Please anyone can solve this
roopanimmagadda
New php-forum User
New php-forum User
 
Posts: 12
Joined: Wed Mar 16, 2011 4:42 am

Re: problem with wave form image

Postby gandahar » Wed Mar 16, 2011 7:04 am

hi

yeah !, great & interesting code 8) , but what is the problem ?, i don't understand

After execution when i stop httpd service and start automatically it is executing the above file.And memory size is increasing.
Please anyone can solve this


you run this script locally ?

memory size increasing ? how do you check this ? what kind of memory ?


please tell us more about what & where is the problem.
User avatar
gandahar
New php-forum User
New php-forum User
 
Posts: 112
Joined: Tue Mar 15, 2011 4:05 am

Re: problem with wave form image

Postby roopanimmagadda » Thu Mar 17, 2011 10:21 pm

This is solved ,this is because of worng files....

Everything is working fine .wave files..

But now another one is...The above code will generate waveform based on .wav file,this wave form i have to place while playing that particular wav file...

If u know can u pls help me
roopanimmagadda
New php-forum User
New php-forum User
 
Posts: 12
Joined: Wed Mar 16, 2011 4:42 am


Return to PHP coding => Images

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.