Jump to content

[SOLVED] Is there a way to say, if an image is broken, don't show it?


Recommended Posts

Is there a way to say, if an image is broken, don't show it?

 

For example, basically, it looks much like this:

echo "<img src='snow".$i.".jpg'><br />\r\n";

 

If for some reason the image is broken, I don't want it to echo the link.

 

 

This works, but it is really slow!

if (fopen("snow".$i.".jpg", 'r'))

 

{

 

echo "<img src='snow".$i.".jpg'><br />\r\n";

 

}

[move]First of all Please Dont use a BIG Signature of Such size[/move]

--------------------------------------------------------------------------

Ya it can be done

Use getimagesize()

If the Image is not broken then and only then it will return the Mime type and the Size of that Image else It would return just false.

Please don't post with marquee and moan about big sigs.. I can scroll past them and they bon't bother me...

 

Lets keep replies to to the question - if anyones sig is 'too big' then I am sure admin will say something to them....

I wouldn't Stop Untill He doesn't STOPIf you really Love PHPBB and Fire fox Use Smaller Images..

------------------------------------[move]First of all Please Dont use a BIG Signature of Such size[/move]

-----------------------------------------------------------------------

try

 

if (isfile($i))

{

...

}

It doesn't say that the IMAGE (Not File) Is Broken or not.

It says wheather that is File or not.

ToonMariner,

When I use that, I get this error:

Fatal error: Call to undefined function isfile()

 

------------------------------

neel_basu, Uh, for the record, I hate PHPBB...

What profit you will get using that much big Signature

Here isfile isn't needed.

You need

if(getimagesize('file.png'))
{
echo "File Is OK";
}
else
{
echo "File is Broken";
}

neel_basu,

    Thank you for the reply.

Both "getimagesize()" and "fopen()" seem to work the same.

 

Is there a different way, or should I look into Javascript?

 

---------------------------------

Also, neel_basu, if you are to criticize someones sig, you might consider reading it...

No where in my sig does it say anything about PHPBB... :/

i think if the file doesnt exist then its broken so its the same

No A broken image is still a Binary file

How can it be File doesn't Exists.

Look this is a Broken Image

File Attached

 

[attachment deleted by admin]

neel_basu: If you think a user has a too big signature, then report him to an administrator or moderator. I think you are far more annoying with your marquees and big red text than his signature is. Besides, you can turn viewing of users' signatures off here: http://www.phpfreaks.com/forums/index.php?action=profile;sa=theme

file_exists() doesn't seem to work at all....

 

If you list images sometimes you get a little tiny square that looks like it's been ripped in half. That could be because the file doesn't exist or because the file is broken. Either way, I want to keep that from being displayed...

~chigley

Ya I was just trying to make him understand this

-------------

~bobleny

Have you ever used getimagesize() ??

Both "getimagesize()" and "fopen()" seem to work the same.

Not not at all.

PLease reaqd it from here http://in.php.net/getimagesize

It returns Image Hight Weidth , MIME Type etc etc....

So if the Image is broken it cannot get its Size and So it returns False.

I tried it again, but this time I use a larger number.

getimagesize() worked

fopen() didn't work so well...

 

I will use getimagesize().

 

Is there a way to say, if getimagesize() fails 4 times consecutively, stop?

 

For example, here is what I actually have:

for($r = 1; $r <= $c; $r++)

{

  	$i = str_pad($r, $digits, '0', STR_PAD_LEFT);

if (getimagesize($before.$i.$after))

{

	echo "<img src='".$before.$i.$after."'><br />\r\n";

}

}

 

 

Say there are actually only 10 images.

Say for some reason $c = 300.

The 10 images will work, but the rest will fail because there are only 10 images, right?

How do I tell the for loop to stop looping after getimagesize() fails 4 times in a row?

if(!isset($cnt)){$cnt = 0}

for($r = 1; $r <= $c; $r++)

{

  $i = str_pad($r, $digits, '0', STR_PAD_LEFT);

  if (getimagesize($before.$i.$after))

    {

      echo "<img src='".$before.$i.$after."'><br />\r\n";

    }

  else

    {

      $cnt++;//Incrementing

      if($cnt >= 4){break;}//Break the Loop Is more than 4

    }

}

Something like

for($r = 1, $fails = 0; $r <= $c; $r++)
{
    $i = str_pad($r, $digits, '0', STR_PAD_LEFT);

    if (getimagesize($before.$i.$after))
    {
        echo "<img src='".$before.$i.$after."'><br />\r\n";
        $fails = 0;                                                        // reset on success if you want 4 in a row
    }
    else ++$fails;
    
    if ($fails > 3) break;
}

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.