QJ.NET | Videos | Forums | iPhone | MMORPG | Nintendo DS | Wii | PlayStation 3 | PSP | Xbox 360 | PC | Downloads | Contact Us
Forums | Gaming News | Videos | Downloads | Today's Posts | Mark Forums Read | Chat | FAQ | Members List | Contact

QJ.net Game Discussion - PSP, Xbox, Wii, PS3, PSP Homebrew, and PSP Guides

Go Back   QJ.net Game Discussion - PSP, Xbox, Wii, PS3, PSP Homebrew, and PSP Guides > Consumer > General PC Forums > Everything Windows
The above video goes away if you are a member and logged in, so log in now!

Problem with PHP random sig with mp3 embedded image

This is a discussion on Problem with PHP random sig with mp3 embedded image within the Everything Windows forums, part of the General PC Forums category; Ok I'm having a problem with the image generated using a randomsig code. The original image had an mp3 embedded ...

Reply
 
LinkBack Thread Tools
Old 01-20-2007, 05:56 PM   #1
Enter Custom Title
 
Chathurga's Avatar
 
Join Date: Mar 2006
Location: Ireland
Posts: 2,089
Trader Feedback: 0
Default Problem with PHP random sig with mp3 embedded image

Ok I'm having a problem with the image generated using a randomsig code. The original image had an mp3 embedded in it:



As you can see, dragging that into Winamp, no problem it plays.

Now this is the code I'm using:
Code:
<?php
header("Content-Type: image/png");
$sigs = array();
$sigs[] = "http://img267.imageshack.us/img267/7640/sig1box4hk.png";
mt_srand((double)microtime()*1000000);
$sig = $sigs[mt_rand(0, count($sigs)-1)];
$im = ImageCreatefrompng("$sig");
imagealphablending($im, true);
imagesavealpha($im, true); 
Imagepng($im);
?>
Output:



The randomsig code works but the problem is that the generated php only uses the image data and it loses the mp3 embedded inside of it.

Can anyone show me a way of making the output retain the mp3?

Oh yeah and thanks Pope for that guide you wrote a while back on embedding an mp3
Chathurga is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 01-21-2007, 06:25 AM   #2
 

 
Join Date: Jun 2005
Location: London UK
Posts: 3,739
Trader Feedback: 0
Default

No problem

I tested this out:
PHP Code:
<?php
$im
= imagecreatefrompng("hello.png");
?>
<?php
header
("Content-type: image/png");
imagepng($im);
?>
Where hello.png was 4.5Mb and had an mp3 in it.
On displaying the page, it was 20kb which means the mp3 was stripped out.

I'm guessing php reads up to the png header and stops. There might be ways to get around this and I am testing them now, I just started learning php a few days ago so I don't know too much about it. Does fwrite work with non text files?
-= Double Post =-
PHP Code:
<html>
<body>
<?php
    
//define the backround image and the text
    
$backround="canvas.png";
    
$text="hello world";
?>
<?php
    
//create the image with the text and save it as canvaswithtext.png
    
$Base = imagecreatefrompng($backround);
    
$textcolor = imagecolorallocate($Base, 0, 0, 255);
    
imagestring($Base, 5, 0, 0, $text, $textcolor);
    
imagepng($Base,"canvaswithtext.png");
?>
<?php
    
//open the image and mp3 you want to merge, read their contents
    
$PNG = "canvaswithtext.png";
    
$MP3 = "WoW.mp3";
    
$OpenPNG = fopen($PNG, "a+");
    
$OpenMP3 = fopen($MP3, "a+");
    
$ReadPNG = fread($OpenPNG,filesize($PNG));
    
$ReadMP3 = fread($OpenMP3,filesize($MP3));
?>
<?php
    
//Create a file called result.png, write the png and mp3 files into it
    
$result = fopen("result.png","w+");
    
fwrite($result,$ReadPNG.$ReadMP3);
?>
<img src="result.png">
</body>
</html>
I think this works, but I am not sure about the random sig thing. Is that image static or do you write text on top of it?
You can use your random image code to define $background. After it has created the image, the image is saved as canvaswithtext.png. This image is read along with your MP3 and they are used to create the file result.png which is displayed at the end.
As I said, I just started learning PHP a few days ago so there was probably an easier way to do all this.
-= Double Post =-
**UPDATE**

I rewrote it with your random code and removed the text overlay, since I think you don't need it and I misunderstood the first time. I also closed the files, which apparently is good but I don't understand it.

PHP Code:
<?php
    $sigs
= array();
    
$sigs[] = "http://img267.imageshack.us/img267/7640/sig1box4hk.png";
    
mt_srand((double)microtime()*1000000);
    
$sig = $sigs[mt_rand(0, count($sigs)-1)];
    
$im = ImageCreatefrompng("$sig");
    
imagealphablending($im, true);
    
imagesavealpha($im, true);
    
Imagepng($im,"canvaswithtext.png");
?>
<?php
    
//open the image and mp3 you want to merge, read their contents
    
$PNG = "canvaswithtext.png";
    
$MP3 = "WoW.mp3";
    
$OpenPNG = fopen($PNG, "a+");
    
$OpenMP3 = fopen($MP3, "a+");
    
$ReadPNG = fread($OpenPNG,filesize($PNG));
    
$ReadMP3 = fread($OpenMP3,filesize($MP3));
?>
<?php
    
//Create a file called result.png, write the png and mp3 files into it
    
$result = fopen("result.png","w+");
    
fwrite($result,$ReadPNG.$ReadMP3);
?>
<?php
    
//Close open files
    
fclose($OpenPNG);
    
fclose($OpenMP3);
    
fclose($result);
?>
<?php
    header
("Content-Type: image/png");
    
$finalimage = Imagecreatefrompng("result.png");
    
imagealphablending($finalimage, true);
    
imagesavealpha($finalimage, true);
    
Imagepng($finalimage);
?>
The only problem I can see is that when you actually get more images, they will all have the same mp3 file attached to them. If you have a set of random MP3s, then that won't do much good either since the image and mp3 will be mismatched.
I know nothing about arrays so I guess I can't help with that one.

Last edited by PopeOfDope; 01-21-2007 at 06:47 AM.. Reason: Automerged Doublepost
PopeOfDope is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 01-21-2007, 06:48 AM   #3
Enter Custom Title
 
Chathurga's Avatar
 
Join Date: Mar 2006
Location: Ireland
Posts: 2,089
Trader Feedback: 0
Default

Thanks, I had thought of trying to do that but I couldn't. Now I'm having problem with making the random sig output be usuable but that's alright, I'll figure it out. I onlt relised something though, the mp3 is fixed. A random pic will be written to a definate song, now even if I made the song random the chances of them matching all the time is tiny.

EDIT: Oh I just saw your second edit, one sec. Thanks again

EDIT2: You solved the random image problem, now all I need is to find some way of matching the song up.

EDIT3: Damn Imagecreatefrompng strips the mp3, I'm almost sure.
Chathurga is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 01-21-2007, 07:41 AM   #4
 

 
Join Date: Jun 2005
Location: London UK
Posts: 3,739
Trader Feedback: 0
Default

I was thinking about creating an image array and an MP3 array in matching order e.g. 1.png goes with 1.mp3 etc.
Then you define
PHP Code:
$randomnumber = mt_rand(0, count($sigs)-1)
then

PHP Code:
$sig = $sigs[$randomnumber];
$MP3= $MP3s[$randomnumber];
In both cases, the random number generated should be the same, so the arrays are matched. I don't know if this is possible though,
-= Double Post =-
Quote:
Originally Posted by Chathurga
EDIT3: Damn Imagecreatefrompng strips the mp3, I'm almost sure.
Oh yeah, I changed that in my last edit, umm, just use <img src="result.png">.
-= Double Post =-
Damn

I just tried myself, but if you use <img src=""> you can't send the png header anymore which means you can't hotlink the image.
The only other way I know to display and image is the Imagepng function which only works on created pngs, which is a big problem

Last edited by PopeOfDope; 01-21-2007 at 07:41 AM.. Reason: Automerged Doublepost
PopeOfDope is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 01-21-2007, 08:36 AM   #5
Enter Custom Title
 
Chathurga's Avatar
 
Join Date: Mar 2006
Location: Ireland
Posts: 2,089
Trader Feedback: 0
Default

Haha Pope I almost have it.
Here's the code:

PHP Code:
<?php
$sigs
= array();
$sigs[] = "Sig1_box.png";
mt_srand((double)microtime()*1000000);
$sig = $sigs[mt_rand(0, count($sigs)-1)];
$filename = "sig_output2.png";
$dummy = "dummy.png";
?>

<?php
    $Openrandom
= fopen($sig, "a+");
    
$Openbase = fopen($filename, "a+");
    
$Readrandom = fread($Openrandom,filesize($sig));
    
$Readbase = fread($Openbase,filesize($dummy));
?>

<?php
    $result
= fopen("Music.png","w+");
    
fwrite($result,$Readrandom.$Readbase);
?>
That outputs Music.png which works in Winamp! Now all that's left is the have the image display code in the PHP. Couldn't have gotten this far with your help Pope.

"Sig1_box.png" is the png with and mp3 in it.
"dummy.png" is a 5x5 png file just so "$Readbase" doesn't give an error to to the fact that "sig_output2.png" doesn't have a size.
-= Double Post =-
Looks like the last part is proving a problem too.
If we can't solve this I can just let the user refresh the song by clicking my sig then refreshing the page.

Last edited by Chathurga; 01-21-2007 at 08:36 AM.. Reason: Automerged Doublepost
Chathurga is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 01-21-2007, 09:36 AM   #6
 

 
Join Date: Jun 2005
Location: London UK
Posts: 3,739
Trader Feedback: 0
Default

Quote:
Originally Posted by Chathurga
Looks like the last part is proving a problem too.
If we can't solve this I can just let the user refresh the song by clicking my sig then refreshing the page.
Well since you are 300k hot, you can have 2 images in your sig. Make one image with your PHP code, modifying the 'result.png' file. Just make an image at the end of your code which is just a 1 pixel transparent image. That way when it loads in your sig, the PHP code will be executed and the image modified.

Your second image is just a direct link to result.png.

EDIT: just to clarify, add this at the end of the code you posted above.
PHP Code:
<?php
$im
= imagecreatefrompng("hello.png");
?>
<?php
header
("Content-type: image/png");
imagepng($im);
?>
...and link to it. Hello.png is just a transparent pixel you can create.
PopeOfDope is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Tags
embedded , image , mp3 , php , problem , random , sig

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



All times are GMT -8. The time now is 05:16 AM.



Use of this Web site constitutes acceptance of the TERMS & CONDITIONS and PRIVACY POLICY
Copyright © 2009, QJ.NET. All Rights Reserved.
Contact Us