Confirm Aspect Ratio of an image to trigger function

0

I have an image upload system in Php and when the images are posted, they create thumbnails. Today, these thumbnails when clicked call a jquery function to display it in a modal.

I need to confirm if this image has the ratio of 2 to 1 (for example 2000 x 1000px. But I can not choose by size, only by ratio) and if yes, open another function, different from the current one that opens any size.

I will be very grateful if anyone can give me a light on it.

    
asked by anonymous 02.06.2018 / 01:43

2 answers

0

You find the ratio by dividing the width / height (% with%). If the result is 2, then it has the ratio of 2: 1. Example:

$("img").click(function(){
   var wd = $(this).width(); // pega largura da imagem
   var hg = $(this).height(); // pega altura da imagem
   var prop = wd/hg; // pega a proporção
   
   console.clear(); // limpa o console (só para exemplo, não inclua isso no seu código)
   
   if(prop == 2){
      outraFuncao(); // chama outra função se for 2:1
   }else{
      console.log("Imagem NÃO tem proporção de 2:1");
   }
});

function outraFuncao(){
   console.log("Imagem tem proporção de 2:1");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>Cliquenasimagens:<br>630x354<br><imgsrc="https://www.cleverfiles.com/howto/wp-content/uploads/2016/08/mini.jpg" width="200">
<br><br>
200x100
<br>
<img src="https://vignette.wikia.nocookie.net/simpsons/images/1/1c/Marge_procurando_homer_luneta_farol.jpg/revision/latest/window-crop/width/200/x-offset/0/y-offset/48/window-width/650/window-height/325?cb=20170128200714&path-prefix=pt"width="200">
    
02.06.2018 / 01:59
0

You can use a function like this:

$url= "img/imagem.gif"; //url da imagem 

function proporcao($url){

   $tam= getimagesize($url);

   //width = tam[0] / height = tam[1]

   //Retorna se largura = dobro da altura
   return ($tam[0] == ($tam[1]*2));
}
    
02.06.2018 / 02:06