php check if img src is image -
i have code, should display image if gets 1 generated, or write "no image" if not.
<body> <?php $src = "somescript.php"; // if (getimagesize($src)) { if (file_exists($src)) { echo '<img src="'.$src.'" />'; } else { echo "no image"; } ?> </body>
somescript.php
local file generates image, or die();
the problems are:
file_exists
condition doesn't return anything.
getimagesize
condition return "failed open stream".
anyone have idea how work?
since seems appropriate generate image once each request, handle @ client side.
you can image using jquery on document load , decide whether show image or show "no image".
or can keep img
tag, use javascript detect errors in image:
css
#mynoimg { display: none; }
html
<img id="myimg" src="somescript.php" /> <div id="mynoimg">no image</div>
javascript
document.getelementbyid("myimg").onerror = function() { this.style.display = "none"; document.getelementbyid("mynoimg").style.display = "block"; }
or using jquery:
$("#myimg").error(function () { // when image doesn't load correctly $(this).hide(); // hide $("#mynoimg").show(); // , show "no image" message instead });
Comments
Post a Comment