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

Asked

Viewed 311 times

8

I would like to know if there is a solution to the following problem,

if there is a logo_enterprise image to consider it and apply it to css, if there is no logo_default, that way I could control versions and my users could customise the application without fear of replacing the files and go back to default, there is some solution in css ?

3 answers

6

It is possible to do with Multiple backgrounds.

The estate background accepts multiple background planes stacked as if they were layers, staying on top of each other.

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

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

.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 an image in Base64, in case the second image has also been deleted, perhaps by accident. But there is some factors to be considered, especially in older browsers. Still, follow 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/v9Xo7uWhV6HKdlkXixVuTvluJPno0EWzy2S6WCj4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qsfJE/niUtDSXWImy8ZVhIr6mvQ0RKyknYuSvQvt2+7oiK79v1TQZSGFpU8VaV7/F4XNwnJpJOI2jVYuVNA0h2ZVPVjeV5hIrdZiz6nEQHZnn6vvaxErUUlErbKuRkQhKSmq3t3NWnYdbP6k+ZRp9eYKCFWtd/cZ3rtaXbjrhNJxWLVYg6iAUognRjUGqUam5xAosyLBow4O8EJt9xEhtYtVVZF0NBrZlnszCEW0haLs982uOidVs/vK5rV8X2vCpkK1YobZ+I01mW/bL/Jxj4DM4x0arFysQBbj6x783JRIwcC1EPZtiAttnihrUjz+MLRNELI7DigJoH9Fngn3E/doW25LG2kgHbIOfeswn4xcD9Wu7HFM8jc3Ub0AfcVGo7bidhuFidYIV8PgzoZzsw8XaCmG+ygIRC0Ws/JJ+x6O5k324WFshpKz8h2/mycw946m+k524WB0nIbhYHSchuFgdJyG4WB0nIbhYHSchuFgdJyG4WB0nIbhYHSchuFgdJyG4WB0nIbhYHSchuFgdJyG4WB0nIbhYHSchuFgdJyG4WB0nIbhYHSchuFgdJyG4WB0nIbhYHScRiPw/Oe5MNKcoAIQAAAAASUVORK5CYII=');
}
<div class='logo'></div>

  • 1

    +1, but this makes two HTTP requests (and downloads the two logos) even if the custom logo is available and be it valid.

  • @re22 the fact of the overlap would affect images with the transparent background that is a pattern that we work there would be a way to exist the first image the second disappear ?

  • http://jsfiddle.net/filadown/oavz3dhw/

  • @Gabrielrodrigues I didn’t find anything about it, I don’t think so. With Javascript is possible, but as your doubt was specific in CSS, this is one way to do.

  • @re22 test the method posted by Craig Stuntz and check if it solves my problem, give you a feedback

6

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

You can use PHP to check the image logo_empresa.png exists in the directory. If yes, it shows. If not, it shows logo_default.png.

Use the function file_exists for that reason. http://php.net/manual/en/function.file-exists.php

But then you have to do it in HTML and do it via Style in 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 research and found this article: http://blog.thiagobelem.net/css-dinamico-inserindo-php-no-css/

  • 1

    I wanted to give you two Ikes, but I can only one, unfortunately, one for the server-side solution and the other for the article by Thiago Belém. He is very good! Another thing I would believe that the customization could be done using PHP COOKIES, if it is not necessary to login the user or PHP SESSION if he has to login.

1


The @Renan response on Multiple backgrounds could solve my problem, but in uploads of images with background transparent images end up blending.

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

found a javascript solution that perfectly solves the problem, would be to check if the image exists, and in onerror() her place 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.