468,103 Members | 1,226 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,103 developers. It's quick & easy.

Help needed in displaying an image

GAR
I recently set up ampache so I can have an mp3 server. I dont like how
they display album covers so I decided to try and change it a little.
Instead of taking the album cover from the mp3s themselves I want it to just
have a file called 'cover.jpg' in the album's direcory where when someone
looks at the album the web browser will display that pic.

Here is what I have so far...

In the template:
<img src="albumart.php?id=<?=$album?>" alt="Album Art" align=right>

In albumart.php:

....
$album_name = get_album_name($_REQUEST['id']);
$songs = get_songs_from_album($_REQUEST['id']);

$full_filename = $songs[1]->file;
preg_match("/^.*\/(.*?)$/",$songs[1]->file, $filename);

// Get directory name.
$searchvalue = "/" . $filename[1] . "/";
$directory = preg_replace($searchvalue, "", $full_filename);

// Check if cover.jpg is in directory with album...
// Assumes all files from same album are in the same directory!!
$album_cover_art = $directory . "cover.jpg";
if( file_exists($album_cover_art) ){
// check image type...might want to be able to use other image types
header('content-type: ' . exif_imagetype($album_cover_art) );
echo $album_cover_art;
} else { //check in mp3s
....

What I'm trying to do here is I get the file location from the song and from
there I replace the song name with 'cover.jpg'. I then send it through the
if statement where if it finds the file in the albums directory I use that
file for the cover art otherwise it checks to see if the cover art is
included in the id3 tag (which is what it did before). Im pretty sure my
problem is in the if statement. Im not really sure how to send the pic back
to the browser.

Im pretty new to php so I'd appreciate it if anyone could help me out...

Thanks,
Brian
Jul 17 '05 #1
1 2084
Hi Brian,

[SNIP]

if( file_exists($album_cover_art) )
{
// check image type...might want to be able to use other image
types
header('content-type: ' . exif_imagetype($album_cover_art) );
echo $album_cover_art;
}

[/SNIP]

In your code you are trying to output a file contents with an echo
statement.
But echo statement will only ouput the filename in this case. You file
handling function to read and output the contents of a file.
Few such functions are fread, fpassthru, readfile, file_get_contents
etc.

Another thing exif_imagetype function returns an INTEGER constant you
need to convert this value to appropriate mime type. Function
image_type_to_mime_type will do that.

Modify your code accordingly: -

[SNIP]

if( file_exists($album_cover_art) )
{
// check image type...might want to be able to use other image
types
header('content-type: ' .
image_type_to_mime_type(exif_imagetype($album_cove r_art)) );
readfile($album_cover_art);
}

[/SNIP]

Hope this will help..

-- Rahul

"GAR" <bb@spamsucks.com> wrote in message news:<BP*********************@newssrv26.news.prodi gy.com>...
I recently set up ampache so I can have an mp3 server. I dont like how
they display album covers so I decided to try and change it a little.
Instead of taking the album cover from the mp3s themselves I want it to just
have a file called 'cover.jpg' in the album's direcory where when someone
looks at the album the web browser will display that pic.

Here is what I have so far...

In the template:
<img src="albumart.php?id=<?=$album?>" alt="Album Art" align=right>

In albumart.php:

...
$album_name = get_album_name($_REQUEST['id']);
$songs = get_songs_from_album($_REQUEST['id']);

$full_filename = $songs[1]->file;
preg_match("/^.*\/(.*?)$/",$songs[1]->file, $filename);

// Get directory name.
$searchvalue = "/" . $filename[1] . "/";
$directory = preg_replace($searchvalue, "", $full_filename);

// Check if cover.jpg is in directory with album...
// Assumes all files from same album are in the same directory!!
$album_cover_art = $directory . "cover.jpg";
if( file_exists($album_cover_art) ){
// check image type...might want to be able to use other image types
header('content-type: ' . exif_imagetype($album_cover_art) );
echo $album_cover_art;
} else { //check in mp3s
...

What I'm trying to do here is I get the file location from the song and from
there I replace the song name with 'cover.jpg'. I then send it through the
if statement where if it finds the file in the albums directory I use that
file for the cover art otherwise it checks to see if the cover art is
included in the id3 tag (which is what it did before). Im pretty sure my
problem is in the if statement. Im not really sure how to send the pic back
to the browser.

Im pretty new to php so I'd appreciate it if anyone could help me out...

Thanks,
Brian

Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by arun | last post: by
5 posts views Thread by Buchwald | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.