Page 1 of 1

checking if an image exists

Posted: Wed Dec 27, 2006 11:10 am
by wyrmmage
Hello everyone :)
I need a function/code/a hint in the right direction as to finding out if an image resides of the server that I'm running my php code from. Could I just use fopen() to check if it was there, or is there a better way?
Thanks in advance :)
-wyrmmage

Posted: Wed Dec 27, 2006 11:15 am
by RobertGonzalez

Posted: Thu Dec 28, 2006 2:22 pm
by wyrmmage
oh, lol, ok...that should work quite well :)
Thanks for the help guys,
-wyrmmage

Posted: Thu Dec 28, 2006 3:02 pm
by brendandonhue
You can getimagesize() if you want to make sure it's an image.

Posted: Thu Dec 28, 2006 5:23 pm
by Ollie Saunders
is_readable is good too

Posted: Fri Dec 29, 2006 3:18 am
by Jaxolotl
file_exists() check if is it really a file, but make not distintion between file types.
For example if you "feed" the funtion with only a directory path it will return TRUE if the directory exists.

So you may create a funtion that returns true if it find the file type you want as on the example below
the file_exists() function + Pearl style pattern match

http://it2.php.net/manual/en/function.preg-match.php

Code: Select all

function isImageAndExists($string){
	if((file_exists("$string"))&&(preg_match('#(\.jpg|\.jpeg|\.gif|\.png|\.bmp)$#i',$string))){
		return true;
	}
	else{
		return false;
	}
}
if it not enough self explained just ask

anyway the getimagesize() suggested by brendandonhue is a good option

Posted: Sat Dec 30, 2006 2:14 pm
by brendandonhue
If you want to match the file extension, you could just use pathinfo(). But that doesn't really tell you if it's an image, since you could rename a .exe to .jpg. getimagesize() actually pulls the first few bytes of the file and checks it against known file formats.

Posted: Sun Dec 31, 2006 12:53 pm
by wyrmmage
Ok, thanks for the help :)
Right now I've got the function:

Code: Select all

function displayImage($page_name,$image_number)
{

//Define the Search query, to fetch information
$SearchImages = mysql_query("
SELECT *
FROM `images`
WHERE
`page` = '$page_name'
");

if(!$SearchImages) die(mysql_error());

$images = mysql_fetch_array($SearchImages);

$imageLinks = explode(":", $images['links']);

$imageLink = $imageLinks[$image_number];

$imgSource = 'images/' . $imageLink;

if(fopen($imgSource, 'r'))
{
echo('
<IMG SRC="' . $imgSource . '" border="0">
');
}
else
{
$imageParts = explode(".", $imageLink);

if($imageParts[1] == 'JPG')
{
$imageParts[1] = 'jpg';
}
else if($imageParts[1] == 'jpg')
{
$imageParts[1] = 'JPG';
}
else if($imageParts[1] == 'BMP')
{
$imageParts[1] = 'bmp';
}
else if($imageParts[1] == 'bmp')
{
$imageParts[1] = 'BMP';
}
else if($imageParts[1] == 'GIF')
{
$imageParts[1] = 'gif';
}
else if($imageParts[1] == 'gif')
{
$imageParts[1] = 'GIF';
}

$imgSource = 'images/' . $imageParts[0] . '.' . $imageParts[1] . '';
if(fopen($imgSource, 'r'))
{

echo('
<IMG SRC="' . $imgSource . '" border="0">
');

}

}

}
This seems to work pretty well, although I might change fopen() to one of the functions that you listed :)
Thanks again guys ^_^
-wyrmmage

Posted: Mon Jan 01, 2007 6:24 am
by Ollie Saunders
Make a separate function for displaying like this:

Code: Select all

function imageDisplay($path, $alt = '', $border = 0)
{
    return '<img src="' . $path . '" alt="' . $alt . '" border="' .$border . '" />';
}
and change your displayImage to imageGet or something like that.