Image capture for broken image link?

Currently, users can upload images to my site, and the location of the image is stored in a MySQL table.

When I call images using the SELECT function, they are loaded via the CSS style background-image: url ('image.jpg')

Currently, I have used the IF statement, which places a placeholder image in place of any rows for which the "Image_URL" column is set to NULL.

if ($row["Image"] != "") {                  
                    echo "<td width='200' rowspan='3'><center><a href='/uploads/".$row["Image"]."' target='_blank'><div class='image-cropper'><div class='rounded' style='background-image:url(\"/uploads/".$row["Image"]."\")'>";
                } else {
                    echo "<td width='200' rowspan='3'><center><a><div class='image-cropper'><div class='rounded' style='background-image:url(\"/images/no-image.jpg\")'";
                }

My method, however, does not work for erroneous links (where the original image was deleted or the link is incorrect for some reason.) It just shows a space, and I understand why, because $ row ["Image"]! = NULL , there is a link, it is simply not connected with the image.

Is there a way to upload an image with a no-image.jpg sample when the image is not found on the provided link?

My DIVs are just making my images circular and centered, so I will post them here, if that helps, you can help me adapt these classes to add placeholder functionality.

.image-cropper {
    max-width: 150px;
    height: auto;
    position: relative;
    overflow: hidden;
}
.rounded {
    display: block;
    margin: 0 auto;
    height: 150px;
    width: 150px;
    -webkit-border-radius: 50%;
    -moz-border-radius: 50%;
    -ms-border-radius: 50%;
    -o-border-radius: 50%;
    border-radius: 50%;
    background:center no-repeat;
    background-size:cover;
}

The cropping image and the rounded DIV are actually from this site, so thanks Hiral !

+4
2

file_exists , glob.

Stewartside , , if.

:

$image = $row["Image"];
if ($row["Image"] != "") {  
    if (file_exists('uploads/'.$image)) {               
        echo "<td width='200' rowspan='3'><center><a href='/uploads/".$row["Image"]."' target='_blank'><div class='image-cropper'><div class='rounded' style='background-image:url(\"/uploads/".$row["Image"];
    } else {
        echo "<td width='200' rowspan='3'><center><a><div class='image-cropper'><div class='rounded' style='background-image:url(\"/images/no-image.jpg";
    }
    } else {
        echo "<td width='200' rowspan='3'><center><a><div class='image-cropper'><div class='rounded' style='background-image:url(\"/images/no-image.jpg";
}

, :

    if (file_exists('uploads/'.$row["Image"])) {    

!:)

+1

glob(), , .

- :

if ($row["Image"] != "") {
  var $file = glob("/uploads/".$row["Image"]); 
  if (count($file) > 0) {
    echo "<td width='200' rowspan='3'><center><a href='/uploads/".$row["Image"]."' target='_blank'><div class='image-cropper'><div class='rounded' style='background-image:url(\"/uploads/".$row["Image"]."\")'>";
  } else {
    echo "<td width='200' rowspan='3'><center><a><div class='image-cropper'><div class='rounded' style='background-image:url(\"/images/no-image.jpg\")'";
  }
} else {
  echo "<td width='200' rowspan='3'><center><a><div class='image-cropper'><div class='rounded' style='background-image:url(\"/images/no-image.jpg\")'";
}
0

Source: https://habr.com/ru/post/1612366/


All Articles