Well what i want is very simple, how to get it might proove very hard.
I am getting image from external sites and i want to have a backup image which is displayed in its place in case that site doesn't have the image i'm requesting.
I want to do this in a basic way without too much code, is it possible?
thanks,
The Creator
Alternate Images
Started by The Creator, Jul 18 2007 01:29 PM
6 replies to this topic
#1
Posted 18 July 2007 - 01:29 PM
#2
Posted 18 July 2007 - 09:27 PM
You can do this to save the files if you aren't doing it manually.
If it is .png or .gif, you will have to use imagecreatefrompng/imagepng or imagecreatefromgif/imagegif, respectively. Or you can make a version that checks what the extension is to tell which functions to use.
More information.
http://www.php.net/image
Then, you can do something like this.
$img = imagecreatefromjpeg('http://site.com/images/image.jpg');
imagejpeg($img, 'images//image.jpg');
If it is .png or .gif, you will have to use imagecreatefrompng/imagepng or imagecreatefromgif/imagegif, respectively. Or you can make a version that checks what the extension is to tell which functions to use.
More information.
http://www.php.net/image
Then, you can do something like this.
function show_image($image){
if(file_exists('http://www.site.com/images/'.$image))
return 'http://www.site.com/images/'.$image;
else
return '/images/'.$image;
}
echo '<img src="'.show_image('image.jpg').'" alt="" />';
Edited by Demonslay, 18 July 2007 - 09:31 PM.
#3
Posted 19 July 2007 - 04:33 AM
thanks for the help but im afraid to say ive already tried that...
i should have mentioned before that file_exists(); only works if the file is on your site...
i should have mentioned before that file_exists(); only works if the file is on your site...
#4
Posted 19 July 2007 - 06:34 AM
Not sure if it's the best way, but you can replace file_exists with file_get_contents which supports external files...
#5
Posted 19 July 2007 - 06:58 AM
ill try that, thanks bud
#6
Posted 19 July 2007 - 03:45 PM
My bad, I keep forgetting about that, lol.
Still, if you look on the manual page, you can still find plenty of ways to see if a URL exists, with and without the use of cURL.
Still, if you look on the manual page, you can still find plenty of ways to see if a URL exists, with and without the use of cURL.
#7
Posted 22 July 2007 - 05:59 PM
As Matthew directs, use file_get_contents, but make sure this function is availabe in your php distribution, to do this add following code on top of your code:
Secondly, calling specific function for each formats may result in large code, to avoid this, use imagecreatefromstring and pass it the image data you read through file_get_contents function. This way you script will accomodate all GD supported image formats.
Some error checking:
I hope it helps.
regards
if(!function_exists('file_get_contents')) {
function file_get_contents($filename) {
$dataArr = @file($filename);
if($dataArr === FALSE) return false;
return implode("",$dataArr);
}
}
Secondly, calling specific function for each formats may result in large code, to avoid this, use imagecreatefromstring and pass it the image data you read through file_get_contents function. This way you script will accomodate all GD supported image formats.
Some error checking:
$data = file_get_contents("your image file link");
if(!$data)
print "Error getting file contents!";
else {
$res = @imagecreatefromstring($data);
if($res === FALSE)
print "input data is not an image";
else {
//yes it is image, do something
}
}
I hope it helps.
regards
Edited by Ali Imran, 22 July 2007 - 06:00 PM.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users
