Confirm Aspect Ratio of an image to trigger function

Asked

Viewed 67 times

0

I have an image upload system in Php and when 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 cannot choose by size, only by proportion) and if yes, open another function, different from the current one that opens any size.

I’d be very grateful if someone could shed some light on that.

  • 1

    include the code you have already developed in your question.

  • the code that generates the thumbnail does not have the clue? because that’s where the image is manipulated, right

  • I tried to paste the code but it exceeds the characters in the message 600 digits, there is some way to post code without this limitation here?

  • Just put the related code, don’t need to put everything.

  • if ($setup->getConfig('thumbnails') == true) : ? > <script type="text/javascript"> Function loadImg(thislink, thislinkencoded, thisname, thisID){ $(".vfm-zoom"). html("<i class="fa fa-refresh fa-spin"></i><img class="preimg" src="vfm-Thumb.php?Thumb="+ thislink +"&y=1" />"); $("#zoomview"). data('id', thisID); $("#zoomview .thumbtitle"). val(thisname); var firstImg = $('.preimg'); firstImg.css('display','None'); $("#zoomview"). modal();

  • I guess I don’t know how to post code here yet

Show 1 more comment

2 answers

0

You find the ratio by dividing the width / height (width/height). 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>
Clique nas imagens:
<br>
630x354
<br>
<img src="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">

  • Thank you very much dvd! I will test your solution!

  • If the image is 2:1 you don’t want to open the modal?

  • Actually open another different modal (with another js) of the pattern if you have that image ratio

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));
}
  • Thank you Temistocles! I will test!

  • For nothing, if I can say if it worked I’ll be grateful

Browser other questions tagged

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