Using PHP to play sound - php

my php code to fetch all sound files, random way, in a folder is this:
$files = glob("audio/*.mp3");
$random = $files[array_rand($files)];
and to play them i use:
<audio src="<?php echo $random; ?>" width="400" height="200" controls></audio>
What i want is to play the sound files pressing a HTML button tag and not seeing the default HTML5 audio player. Just like Spotify or similar.
I tried making it with button onclick="function()" but with javascript wich is a client language i don't know a way to loop all files in my folder, just like i did with the php code above.
Any ideas? Many thanks...

Few minutes after asking i solved the question by myself. Been dealing with this four a couple of hours, so here it is:
<a onclick="this.firstChild.play()"><audio src="<?php echo $random; ?>"></audio>►</a>
Used the browser DOM and a html special caracter & #9658;. It does the trick, in spite of not allowing the use of an icon image.
Thanks to all :)

Related

How to embed an audio file into a website with php?

I am attempting to embed a sound file of Bohemian Rhapsody into my website and have found this code snippet:
echo "<embed src=\"SONGURL.mp3\" autostart=\"true\" loop=\"true\" hidden=\"true\"> </embed>\n" ."<noembed><bgsound src=\"SONGURL.mp3\" loop=\"infinite\"></noembed>";
My question is how to make this apply to the song I want? Also, what part of this would I modify?
Well it seems that the HTML snippet being produced by the PHP snippet in question relies on some sort of plug-in (my guess would be Flash, but I'm not familiar with Flash at all) so it would be much more complicated than a simple edit of the HTML string. If you're willing to break backwards compatibility with old browsers, you could use the HTML5 audio tag (see for example https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Using_HTML5_audio_and_video).
In your case, given some filename $song_url, you could accomplish what it seems you want via string-building as
$html_snippet = "<audio src=\"$song_url\" autoplay loop></audio>";
echo $html_snippet;
To support multiple different formats (for example .ogg files for Firefox users) you can simply write
$html_snippet = <<<EOD
<audio autoplay loop>
Upgrade to a newer browser!
<source src="$song_url_mp3" type="audio/mpeg">
<source src="$song_url_ogg" type="audio/ogg">
</audio>
EOD;
This HTML tag will ensure that your audio plays automatically and loops forever. To give users standard music controls, simply add a controls attribute.

Calling a gif with createimagefromgif in PHP

I've been trying to display a gif image using PHP and have not found a solution to my problem with the research I've done. I know I can display image in HTML/CSS, but I need to use php in this case.
<html>
<?php
$img = imagecreatefromgif("http://www.mysite.com/images/timer.gif");
?>
<img src="<?= $img ?>" alt="timer" />
</html>
That code resides in a php doc on my server. I can tell the code is working because an icon of a torn image displays on my site, and when I attempt to save the torn icon image to desktop, the automatic file name appears as "Resource id.html"?
I read somewhere that creating gifs with Photoshop CS5 (as I did) uses a different frame separator sequence, \x00\x21, instead of the official standard \x00\x2C. The guy then said he uses pattern "#\x00\x21\xF9\x04.{4}\x00(\x2C|\x21)#s" to bypass that issue but I have no idea how/where to implement that or if that is even my issue (tried a different gif from internet and had same display problem). Thoughts? Thank you.
Imagecreatefromgif() returns an image resource, which you can't use in this way.
Have your PHP script output the image data, with the correct headers (see example here http://php.net/manual/en/function.imagecreatefromgif.php).
Then you can call your script just like you would any other image:
<img src="http://www.mysite.com/images/generate_image.php" alt="timer" />

Shadowbox PHP Facebook Graph Api Error

i'm using shadowbox for my website to open the big images with clicking thumbnails as you know. My Problem is, i'm fetching the users' facebook profile photo like :
$large = "https://graph.facebook.com/{$id2}/picture?type=large";
$small = "https://graph.facebook.com/{$id2}/picture?type=square";
And it's working perfect, but in shadowbox i have problem with large image..
I'm calling this in shadowbox like :
<a href="<?php echo $large; ?>" rel="shadowbox">
<img style="max-width:50px; max-height:50p;" src="<?php echo $small; ?>" />
</a>
As you can imagine, small image is showing perfect, but when i click on the small image which has href, it fails to show the large image.
I've tried to change large image variable to this :
$large = "https://graph.facebook.com/{$id2}/picture?type=large&redirect=false";
but also it has failed to show the large image..
Hope you can help, thank you
I haven't worked with Shadowbox before, so I don't know how it works behind the scenes. It may be that it can't deal with a url that returns another url for the large image.
Try making the API call with php and then passing this result to shadowbox.
$large = file_get_contents("https://graph.facebook.com/{$id2}/picture?type=large&redirect=false");
Of course, using file_get_contents() like this is a quick and dirty method. If it works, before you roll this out to a production site, you'll want to use cURL or better yet the Facebook PHP SDK to do this.
Assuming you're using shadowbox to show multiple users' photos on a page, you'll probably end up bumping into the API limits at some point. To prevent this, redo your API calls to grab multiple photos in one shot:
$large_photos = $fb->api('/picture?type=large&redirect=false&ids=' .
implode($ids_array, ','));
https://graph.facebook.com/{$id2}/picture?type=large&redirect=false basically returns you a text content the URL of the image. That's why it's not showing.
Eg.
https://graph.facebook.com/yungsenriady.budiman.3/picture?type=large&redirect=false
returns
"https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash3/157348_100001167523294_1569184886_n.jpg"
Try just use
https://graph.facebook.com/{$id2}/picture?type=large

Preloading pictures for refresh, cache

I have these two php variables: $img1src and $img2src, (them being PHP is irrelevant as you can echo a php variable anywhere) they both hold a URL of an image. I was wondering if I was able to preload these images, or cache them.
My goal is: when someone refreshes the page I want these pictures to instantly appear in a <img src >.
I'm not going to provide specific code, because I don't want to give you a fish, but rather show how google is a fishing pole.
I googled "php cache images tutorial" just to see what would come up. Below is a great resource.
http://dtbaker.com.au/random-bits/how-to-cache-images-generated-by-php.html
Can't get much better than that.
Caching an image isn't really a job for PHP. PHP should be used to decide whether or not to display it. (There are caching things you can do with PHP, but not in the same sense.) Essentially, what you want to do is make the clients browser request the second image. Once the browser gets the image, it should automatically send an "if-modified-by" parameter in the header. Next time you load the page, the response code should be 304 and your image should load instantly. You can choose from a variety of ways to do this. Load the image with javascript after the page has loaded (to prevent additional load time) or you can just include an image tag that is hidden on the page some where.
I also haven't tested it, but you might be able to send an ajax request to the image directly. I'm not sure if that way would cache it or not.
EDIT:
This isn't the most elegant solution, but it should get the idea across.
JS Example:
<?php
session_start();
if (!isset($_SESSION['graphic'])) $_SESSION['graphic'] = "http://www.tomsfreelance.com/laptop/DSC_0011.JPG";
else $_SESSION['graphic'] = "http://www.tomsfreelance.com/laptop/DSC_0012.JPG";
?>
<html>
<head>
<script>
function loadImage() {
document.getElementById('preload').style.backgroundImage = "url(http://www.tomsfreelance.com/laptop/DSC_0012.JPG)";
}
</script>
</head>
<body onload="loadImage();">
<div id="preload" style="display: none;"></div>
<img src="<?php echo $_SESSION['graphic'];?>">
</body>
</html>
Sure you can, try this javascript:
var image1 = new Image(), image2 = new Image();
image1.src = <?php echo $img1src; ?>;
image2.src = ?<php echo $img2src; ?>;
That should preload the image so when you append an img tag to the DOM the image should appear right away.
If your aim is to make less http requests overall: you can try CSS Sprites and/or Data Url methods of displaying these images. These methods will be the most effective when the images are smaller.

Long (5 second) DOMContentLoaded wait when opening PHP file

Thanks in advance for your help with this. I've got a very simple PHP file that returns HTML code for an image based on some passed parameters. The code works fine, and the image shows up quickly. However, the page itself doesn't finish loading for a good 5 seconds, which is interfering with some AJAX calls I'm trying to do. Firebug says the time breakdown is like this:
0ms: DNS lookup
0ms: Connecting
0ms: Queuing
211ms: Waiting for response
14ms: Receiving data
+5.32s: 'DOMContentLoaded' (event)
+5.33s: 'load' (event)
Here's my PHP code:
<?php
$getimage = $_GET['p'];
$getcity = $_GET['c'];
?>
<img src="/images/photos/big/<?php echo $getcity; ?>_<?php echo $getimage; ?>.jpg" alt="" class="gallery" />
Pretty simple, no? Any idea what's going on?
This page is using "/images/photos/big/" which I assume means it's using a "big" image. images take time to render, this is likely the slowdown.
you might be able to speed this up by setting the image height and width because the DOM will not know where everything is placed until the image size is known. I am not positive if this will fix the problem, sorry.

Categories