PHP: if image exists show image else show different image - php

i am making a login system with registration and a profile page in php and i am trying to make a profile picture work.
if the user has not uploaded a profile picture yet then make it show a "no profile picture" image if the user has uploaded a profile picture make make it show the image that he has uploaded.
Right now it only show the default picture, noprofile.png.
< img src="uploads/< ? echo "$username" ? >/noprofile.png">
i want it to show icon.png if icon.png has been uploaded and if it hasnt been uploaded make it show, noprofile.png.

Just run it through the logic, using file_exists:
$image="/path/on/local/server/to/image/icon.png";
$http_image="http://whatever.com/url/to/image";
if(file_exists($image))
{
echo "<img src=\"$http_image\"/>\n";
}
else
{
echo "<img src=\"uploads/$username/noprofile.png\"/>\n";
}

Check to see if the file has been uploaded by using file exists. If the file exists, use that url else use the default noprofile.png.

you could make a column in the DB to store a value if it has been uploaded or not.
OR
you could see if the file exists.
<?php
if (file_exists('uploads/' . $username . '/icon.png')) {
echo '<img src="uploads/' . $username . '/icon.png">';
}
else {
echo '<img src="uploads/' . $username . '/noprofile.png">';
}
?>

<?php
$img = file_exists(sprintf('/path/to/uploads/%s/icon.png', $username))
? 'icon.png' : 'noprofile.png';
?>
<img src="uploads/<?php printf('%s/%s', htmlspecialchars($username), $img) ?>">

You could use http://us3.php.net/file_exists to check if the image file is there.
Another alternative is - assuming you keep your user info in a database - have a column with the image name. Since you have to retrieve info from your user table anyway, check to see if that column is NULL or blank. If it is, the user has not uploaded an image yet.
Then, in the page you display the user photo, you might have code something like this:
$userPhoto = ($photoName)? $photoName : 'placeholder';
echo '<img src="uploads/'.$userPhoto.'.png" />

Assuming the filepaths are correct, here's what you do...
<?php $filename = "uploads/".$username;
$imgSrc = file_exists($filename) ? $filename : "uploads/noprofile.png"; ?>
<img src=<?php echo $imgSrc?>

Use onerror attribute in img tag
<img onerror="this.src= 'img/No_image_available.png';" src="<?php echo $row['column_name ']; ?>" />

Related

How do I get PHP to display a user picture if you have one within a blog post?

I have a script that can disply user pictures but I want to have it now so if you don't have a picture it displays a default one.
CODE
if (file_exists($displayuserimage)) {
$displayuserimage = '<img src="upic/'. $posts['userid'].'.jpg" width="50px" height="50px" border="2" />';
} else {
$displayuserimage = '<img src="upic/default.jpg" width="50px" height="50px" border="2"/>';
}
That's what im guessing, but this is the working script, that shows images if you have one:
$displayuserimage = '<img src="upic/'. $posts['userid'].'.jpg" width="50px" height="50px" border="2"/>';
Your if is checking a variable,$displayuserimage, which has not been defined before. To check if your image exists, that if may look like
if (file_exists("upic/". $posts['userid'].".jpg")) {
Using PHP's file_exists() function
http://php.net/manual/en/function.file-exists.php
$image="/path/on/local/server/to/image/icon.png";
$http_image="http://whatever.com/url/to/image";
if(file_exists($image))
{
echo "<img src=\"$http_image\"/>\n";
}
else
{
echo "<img src=\"uploads/$username/noprofile.png\"/>\n";
}
Use the same scenario if you are calling user images from Database too. Replace the
echo "<img src=\"$http_image\"/>\n";
with your row from the Database which contains your user's profile image. Example:
echo $rows['user_image'];

Secure images failing - Codeigniter

My script pretty much works, however when I run a for loop it fails, in the view:
<?php foreach($pics as $index=>$pic){?>
<td class='Pictures'>
<center>
<?php
$piece = explode('/',$pic["ThumbImg"]);
$string = $piece[5];
?>
<img src='<?php echo base_url(); ?>index.php/Controller/getImage/<?php echo $string; ?>/1' width="100px">
</a>
Controller class - Get image function:
//function to protect images from being accessed directly by obfuscating the URL
function getImage($img_id,$type){
if($this->members->logged_in()){
//code to authenticate user goes here then...
//code to decide which type of file it is/thumb/full size image
if($type==1)
{
$url = $this->data['base_url'].'system/application/images/pic/thumbs/';
}else if($type==0){
$url = $this->data['base_url'].'system/application/images/pic/';
}
$filepath = $url.$img_id;
//send image to web browser.
header("Content-type: image/jpeg");
//get path
$img_handle = imagecreatefromjpeg($filepath) or die("");
//create and send
ImageJpeg($img_handle);
}
I suspect that this could be due to the headers being resent, if so how can I resolve this.
Cheers
Fixed the problem - used "Hotlinking"
You will basically only need to set the header information header("Content-type: image/jpeg") if you're loading the image from the database. But since you're loading it from a folder you might want to skip the php file and immediately fetch the image from the folder:
<img src='<?php echo base_url(); ?>img_folder/<?php echo $string; ?>/1'
width="100px">

Get Twitter profile picture actual link

I'm trying to get the twitter profile picture actual link.
I know I can get the profile picture through the following link:
$test = "http://api.twitter.com/1/users/profile_image?screen_name=".$nickname."&size=original"
but when I want to get the file contents of this url, it doesn't work, cause above mentioned link is redirected to the actual link of the profile picture. So this doesn't work:
file_get_contents($test);
How can I get the actual link of the profile picture and then with the size original?
Try this it might help you.
<?php
function getTwitterProfileImage($username) {
$size = '_bigger';
$api_call = 'http://twitter.com/users/show/'.$username.'.json';
$results = json_decode(file_get_contents($api_call));
return str_replace('_normal', $size, $results->profile_image_url);
}
$img = getTwitterProfileImage('thetutlage');
echo '<img src="'.$img.'"/>';
?>
Try this is up+direct,
<img class="img-rounded" src="<?php
$size = '';
echo str_replace('_normal', $size,$tweet->user->profile_image_url)
?>"
height="250px" width="400px" />

echo file if it exist. if not echo "default.png" php

I have a script that echo's usernames an inserts that into img src. This works great as long as the image is in the directory. How can I create an if statement that only echos the below command if the file exist? If it doesn't exist show default.png
I tried using mod_rewrite and have had zero luck with it..
<div class="contactphoto"><img src="contactphoto/<? echo ($note['user_name'] == "Support")? $note['first_name'].''.$note['last_name'] : $note['user_name'];?>.png"/></div>
The name says it all: file_exists()
I think this is what you want.
<?php
$file = ($note['user_name'] == "Support") ? $note['first_name'].''.$note['last_name'] : $note['user_name'];
$file .= '.png';
if(!file_exists($_SERVER{'DOCUMENT_ROOT'} .'/'.$file)){
$file = 'placeholder.png';
}
?>
<div class="contactphoto">
<img src="contactphoto/<?php echo $file; ?>"/>
</div>
If that fails, try a test ( this should match the path of the image ) also watch out for case sensitivity:
echo $_SERVER{'DOCUMENT_ROOT'} .'/'.$file;

php - Decide whether an image exist and if it does show it

I have a code that shows a different image depending where on the page I am, but some places don't have an image so it displays a "no image" icon. I want to add a condition that checks if there really is an image in the given path and if returns false don't do anything. I have no idea how to do it.
This is the original code:
<?php
$search=get_search_query();
$first=$search[0];
if ($first=="#"){
echo "<html>";
echo "<img src='http://chusmix.com/Imagenes/grupos/".substr(get_search_query(), 1). ".jpg'>";
}
?>
What I need to know is which function do I use to get a true/false of that image path. Thanks
Use file_exists
$image_path = 'Imagenes/grupos/' . substr(get_search_query(), 1) . '.jpg';
if (file_exists($image_path)) {
echo "<img src='http://chusmix.com/Imagenes/grupos/".substr(get_search_query(), 1). ".jpg'>";
} else {
echo "No image";
}
http://php.net/manual/en/function.file-exists.php
You can use file_exists

Categories