justin7410 Posted June 1, 2013 Share Posted June 1, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/278651-best-way-to-approach-missing-image-in-database/ Share on other sites More sharing options...
requinix Posted June 1, 2013 Share Posted June 1, 2013 Yeah, an if statement, that's about it. You can make code look prettier to some degree but it all comes down to an "if it's empty then do this instead". Quote Link to comment https://forums.phpfreaks.com/topic/278651-best-way-to-approach-missing-image-in-database/#findComment-1433494 Share on other sites More sharing options...
justin7410 Posted June 1, 2013 Author Share Posted June 1, 2013 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/278651-best-way-to-approach-missing-image-in-database/#findComment-1433497 Share on other sites More sharing options...
Jessica Posted June 1, 2013 Share Posted June 1, 2013 It depends on what your data is. Are you saying you stored the filename but the files are missing? Then use php's file functions. Is it simply a case of no filename? Then use a string function. Quote Link to comment https://forums.phpfreaks.com/topic/278651-best-way-to-approach-missing-image-in-database/#findComment-1433499 Share on other sites More sharing options...
justin7410 Posted June 1, 2013 Author Share Posted June 1, 2013 (edited) 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 June 1, 2013 by justin7410 Quote Link to comment https://forums.phpfreaks.com/topic/278651-best-way-to-approach-missing-image-in-database/#findComment-1433501 Share on other sites More sharing options...
requinix Posted June 1, 2013 Share Posted June 1, 2013 now i wan't the logic of. if (file is mising) { display a different imag src } else continue the normal process. You got it right there. Quote Link to comment https://forums.phpfreaks.com/topic/278651-best-way-to-approach-missing-image-in-database/#findComment-1433510 Share on other sites More sharing options...
justin7410 Posted June 3, 2013 Author Share Posted June 3, 2013 (edited) 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 June 4, 2013 by justin7410 Quote Link to comment https://forums.phpfreaks.com/topic/278651-best-way-to-approach-missing-image-in-database/#findComment-1433884 Share on other sites More sharing options...
DavidAM Posted June 4, 2013 Share Posted June 4, 2013 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; } Quote Link to comment https://forums.phpfreaks.com/topic/278651-best-way-to-approach-missing-image-in-database/#findComment-1433910 Share on other sites More sharing options...
justin7410 Posted June 4, 2013 Author Share Posted June 4, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/278651-best-way-to-approach-missing-image-in-database/#findComment-1433932 Share on other sites More sharing options...
justin7410 Posted June 4, 2013 Author Share Posted June 4, 2013 (edited) 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 June 4, 2013 by justin7410 Quote Link to comment https://forums.phpfreaks.com/topic/278651-best-way-to-approach-missing-image-in-database/#findComment-1433933 Share on other sites More sharing options...
justin7410 Posted June 7, 2013 Author Share Posted June 7, 2013 (edited) 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 June 7, 2013 by justin7410 Quote Link to comment https://forums.phpfreaks.com/topic/278651-best-way-to-approach-missing-image-in-database/#findComment-1434756 Share on other sites More sharing options...
DavidAM Posted June 8, 2013 Share Posted June 8, 2013 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). Quote Link to comment https://forums.phpfreaks.com/topic/278651-best-way-to-approach-missing-image-in-database/#findComment-1434795 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.