Jump to content

Recommended Posts

Hey guys,

 

so i have a website that has info outputted from my database.

 

the data is users that are signed up

 

right now lets say i have 10000 users each linked with their own image to their profile from amazon cloud based system. Unfortunately, 20 percent of my users don't have an image, and their box shows a missing image box. What i want to do until the user submits or a image is available, is to use my own image stating that the "image is not available". 

 

i just want to know what is the best approach to get this to work. 

 

is the done with an if statement ? 

 

i just am drawing a blank.

 

any suggestions ? 

 

much appreciated.

yea i figured as much, but i cant see how an if statement would be written to see if a file is returning as null or not null

 

i understand what your trying to tell the pc to do 

 

but as how to word it for the server to see if the image has loaded or not loaded

 

then for that if statement to then switch the image to the other image output.

 

i am struggling more so with how to write the logic for this , more so than as to what the logic actually is.

 

any suggestions ?

function load_content_images($id) {
$id = md5($id);


return 'http://s3.amazonaws.com/fast-network/photos/' .$id .'.jpg';
}
function list_profile ($is_name, $id, $year,$is_male) {
if ($is_male == true) {
echo  '<li>';
echo '<div class="pic_profile"><a href="profile.php?profile=' .$is_name . '&id=' .$id .'"><img src="' .load_content_images($id).'" alt=' .$is_m_title .'('.$year.')="" width="90" height="130"></a></div>';
echo '<div class="text_banner_top"><a href="profile.php?profie=' .$is_name . '&id=' .$id .'">' .$is_name. '(2013)</a></div>';


echo  '</li>';


}if ($is_female == true) {
echo '<li>';
echo '<div class="pic_profile"><a href="profile.php?profile=' .$is_name . '&id=' .$id .'"><img src="' .load_content_images($id).'" alt=' .$is_m_title .'('.$year.')="" width="90" height="130"></a></div>';
echo '<div class="text_banner_top"><a href="profile.php?profie=' .$is_name . '&id=' .$id .'">' .$is_name. '(2013)</a></div>';/div>';


echo  '</li>';


}
}

as you can see the image src = the function that takes the matching ID of the user and the hash password of the user , and grabs the appropriate image for user.

 

problem is as i stated, some users don't upload an image, and the image appears blank with a red x. 

 

so to answer your question, yes the file is missing and now i wan't the logic of.

 

if (file is mising) { display a different imag src }

 

else continue the normal process.

Edited by justin7410

You got it right there.

 

Well i understand what the logic is to do what i want.

 

again, i am more so confused with the function to be able to see if an image is being loaded.

 

so if the image is returning false from the cloud server then it would replace the image with the conditional.

 

any suggestions on how to approach the actual if part of the statement 

if (image is not returning) {
      replace image with new image
} else continue

if this was a file in my own server uploaded through FTP i think string functions would be the answer.

 

i am just confused since the img src in a dynamic link. i cant figure out how to check if the source is null

 

just wanted to add, that when an image is not found through amazon cloud you get an access denies message on the url. is there any way to set the conditional to see if access if denied then give the default image

 

im guessing there is an easier way to check to see if the image is being returned though :/.

 

thanks guys

Edited by justin7410

As I understand it, you want to know, in a PHP script, if an image file on a remote server exists or not. If the file were on your local server, you could use file_exists. If allow_url_fopen is on, you can use it (file_exists()) on the remote file (I've never tried this).

 

You can try something like this -- again, this would require that allow_url_fopen be on.

function load_content_images($id) {
$id = md5($id);

$url = 'http://s3.amazonaws.com/fast-network/photos/' .$id .'.jpg';
if (! file_exists($url)) $url = 'http://mydomain.com/missing_image_filename.jpg';

return $url;
}

As I understand it, you want to know, in a PHP script, if an image file on a remote server exists or not. If the file were on your local server, you could use file_exists. If allow_url_fopen is on, you can use it (file_exists()) on the remote file (I've never tried this).

 

You can try something like this -- again, this would require that allow_url_fopen be on.

function load_content_images($id) {
$id = md5($id);

$url = 'http://s3.amazonaws.com/fast-network/photos/' .$id .'.jpg';
if (! file_exists($url)) $url = 'http://mydomain.com/missing_image_filename.jpg';

return $url;
}

hey david,

 

thanks for the great feedback, unfortunately i tried exactly this also but all the images return as false and are replaced with the new image.

 

when i used file_exists with my own conditional i was just getting it to return all true, and it wouldnt replace any.

As I understand it, you want to know, in a PHP script, if an image file on a remote server exists or not. If the file were on your local server, you could use file_exists. If allow_url_fopen is on, you can use it (file_exists()) on the remote file (I've never tried this).

 

You can try something like this -- again, this would require that allow_url_fopen be on.

function load_content_images($id) {
$id = md5($id);

$url = 'http://s3.amazonaws.com/fast-network/photos/' .$id .'.jpg';
if (! file_exists($url)) $url = 'http://mydomain.com/missing_image_filename.jpg';

return $url;
}

how would i be able to check if my server has allow_url_fopen ? can i access this in my phpinfo ?

 

edit: so i checked this info under my phpinfo and it is on, if that makes any difference to the issue

Edited by justin7410

Ok so after a few days of playing around.

 

i figured that file_exists() is a horrible way to get this done. the more images per page you have the longer it will take the page to process.

 

i realized that in the database there is an image_key field that has it own set value. the ones missing a value are the ones missing an image + the database has not been synced so any member past $id < $num has no image. 

 

so i then created a conditional with an if statement

function load_content_images($id,$url,$image_key) {
$id = md5($id);


$url = '';
$num = 230467

if(strlen($image_key) < 2 || $id > $num) {
$url = 'img/image_not_found.jpg';
} else {
$url = 'http://s3.amazonaws.com/fast-network/photos/' .$id .'.jpg';
}

return $url; 

and this actually worked, except for some reason, the wrong images are being replaced.

 

images with missing data are still showing and the id of some profiles lower than the set $num are missing

 

i am thinking i am on the right track, just having some issues with the arguments.

 

any suggestions would be great

Edited by justin7410

 

if(strlen($image_key) < 2 || $id > $num) {

 

You have already modified $id with md5() so this comparison is invalid. However, hard-coding a test like this ($id > 230467) is not generally a good idea. You will soon forget this check is buried deep in the code, and some change to the data will make it obsolete or not 100% accurate. If the image_key test is correct, use it. Better yet, add a column to the database either indicating there is or is NOT an image, or store the complete URL of the image (and NULL for none).

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.