How to use a different logo if the default does not exist?

8

I wonder if there is a solution to the following problem,

If there is a company logo, consider it and apply it to css, if there is no use of logo_default, then I could control versions and my users could customize the application without fear of replacing the files and returning to default, is there any solution in css?

    
asked by anonymous 14.05.2015 / 15:08

3 answers

1
  

@renan's response to multiple backgrounds could solve   My problem, however, is when loading background images   transparent images blending.

In the OS response: Fallback image and timeout - External Javascript Content

I found a solution in javascript that solves the problem perfectly, it would check if the image exists, and in onerror() it puts the other message.

Example:

Javascript - jQuery:

function checkImage(imagemDefault, imagemEmpresa) {
  var img = new Image();
  img.src = imagemDefault;
  img.onload = function() {
    $('#logo').attr('src', imagemDefault);
  };
  img.onerror = function() {
    $('#logo').attr('src', imagemEmpresa);
  };

}

var imagemDefault = 'imagemDefault.png';
var imagemEmpresa = 'imagemEmpresa .png';

checkImage(imagemDefault, imagemEmpresa);

HTML:

<img id="logo" alt="" />

Example working on jsfiddle

    
23.07.2015 / 22:23
6

The only way I know it is by server-side . PHP , for example.

You can use PHP to check if the logo_empresa.png image exists in the directory. If so, show. If not, show logo_default.png .

Use the file_exists function to do this. link

But then you have to do this in HTML and do via Style in the HTML tag.

if(file_exists('img/imagem.png'))
   echo '<div style="background-image:url(<?php echo $var; ?>)"> Teste </div>';
else
   echo '<div style="background-image:url('imagem_padrao.png')"> Teste ';</div>

Out of curiosity I went to search and found this article: link

    
14.05.2015 / 15:38
6

You can do with multiple backgrounds .

The background property accepts multiple stacked layers as layers, layered over each other.

You can take advantage of this by defining the image that has the possibility of failing (not existing) as the first layer of background. If there are other images, you can put them in sequence but leave the last layer with the default image to be used if all the previous ones will fail.

In case the first is not found, the second will be used. If you have multiple images, the first one that does not crash will be searched in sequential order.

.logo {
  width: 235px;
  height: 90px;
  background: url('imagemQueVaiFalhar.png'), 
              url('http://i.stack.imgur.com/pqk9d.png'); /* default */
}
<div class='logo'></div>

It is possible to have a base64 image, in case the second image has also been deleted, perhaps by accident. But there are a few factors to consider, especially in older browsers. Still, here is an example:

.logo {
  width: 235px;
  height: 90px;
  
  background: url('imagemQueNuncaVaiExistir.jpg'),
              url('imagemQueSubstituiAPrimeira.jpg'),
              url('imagemColoqueiPorDefaultEOUsuarioApagou.png'),
              url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOsAAABdCAYAAABEmZG9AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAABLeSURBVHhe7Z15kBVVlofdNdQ/DEPD3dEZw7FbmTBCB40Z2y3cwqFjdMaVdgQJ/9AIpwMZpDW0tQ1xbEFABVllU0BlkUXZFGQrpNm3kR1kUdlKqoCidupMfVl5ss7Leq82iuJl1fkiTtR799y8me/m/eU59758WSeJ4ziJwMXqOAnBxeo4CcHF6jgJwcXqOAnBxeo4CcHF6jgJwcXqOAnBxeo4CcHF6jgJwcXqOAnBxeo4CSHxYi1bO1WKhjxR+aI4LHGclkmixVo6410peOWqwEqmvB6WOk7LJNFiLVs5UY682aZKsK//RsqWjws9jtPySHwaXDKzRxRdC/v9Xo7uWhV6HKdlkXixVuTvluJPno0EWzy2S6WCj4Rex2k5JEasFcUHw1c1Kd80TwrfuyMSbMncAaHHcVoOiRBrRcE+Kf74bimd9j8ZV33LFgyJxHrk3X+R8vWzQ4/jtAyyX6wlBZVp7u+lsMffBVY86qFAvDWoFHHx+JciwRYN7yAVv+4InY6TfLJerBWFv0rpvL9KYc+/rxbs6P+sTH1nhDWqKf9ptRR+9O+RYEumdg89jpN8EjNnLVv6sRT2bxsJtmjQ76RsxSeht5qy5eOl4I3fVgn2z/8opUs/Dz2Ok2wSI1YoXzdZikf+WyTYwt6VYlzwXo3Fp5Lp/xtF18K+7eTozhWhx3GSS6LECuU7FkrxuA7Vgq20kund5Gju5rBGZep8YJcUjXgmEmzxF51Fig+HXsdJJlkp1pK8HbInp6+UFx8KS1KpyN0kpdO7pgi2eHxHKd++MKxRKeoNs6Xw3X+NBFs656PQ4zjJJCvFun3SC7Km52/khw/byt6/DZaKNF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