Visualizzazione dei risultati da 1 a 6 su 6
  1. #1
    Utente di HTML.it
    Registrato dal
    Feb 2002
    Messaggi
    1,202

    width e height immagine = 0?

    Ciao, ho questa funzione:

    Codice PHP:
    function scaleImage() {
      var 
    image document.getElementById('eg_photo');
      
      
    o_width image.width;
      
    o_height image.height;

      if(
    o_width > {MAX_WIDTH} || o_height > {MAX_HEIGHT}){
        
    //do resize
        
    perc_w = {MAX_WIDTH} / o_width;
        
    perc_h = {MAX_HEIGHT} / o_height;
        
        if(
    perc_h perc_w){
          
    width = {MAX_WIDTH};
          
    height Math.round(o_height perc_w);
        } else {
          
    height = {MAX_HEIGHT};
          
    width Math.round(o_width perc_h);
        }
        
        
    image.height height;
        
    image.width width;
        
        var 
    warning document.createElement('div');
        
    warning.className 'small';
        
    warning.style.width width 'px';
        
    warning.innerHTML '{EG_PHOTO_THIS} ' o_width ' * ' o_height ' {EG_PHOTO_PIXELS}' + (image.fileSize ' {EG_PHOTO_AND} ' Math.floor(image.fileSize/1024) + 'KB' '') + ' {EG_PHOTO_LARGE}. {EG_PHOTO_CLICK} [url="javascript:void(0);"]{EG_PHOTO_HERE}[/url] {EG_PHOTO_TO_VIEW}';
        
        
    image.parentNode.appendChild(warning);
      }

    applicata a

    codice:
    <div class="view-photo">
    [img]{IMAGE_SRC}[/img]
    
    </div>
    E non funziona perchè image.width e image.height sono sempre uguali a 0... :rollo: perchè? Da cosa può dipendere?

    Grazie :quote:
    Debian GNU/Linux sid
    Publishing a theory should not be the end of one's conversation with the universe, but the beginning. (Eric S. Raymond)
    Kernel 2.6.14-ck1

  2. #2
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    dal fatto che nel tag img a cui vai ad applicare lo script le due proprietà width e height non siano definite?
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  3. #3
    Utente di HTML.it
    Registrato dal
    Feb 2002
    Messaggi
    1,202
    io le vorrei definire "dinamicamente" a seconda delle dimensioni reali dell'immagine
    Debian GNU/Linux sid
    Publishing a theory should not be the end of one's conversation with the universe, but the beginning. (Eric S. Raymond)
    Kernel 2.6.14-ck1

  4. #4
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    ecco, allora devi scriverle prima nel tag, tipo via server-side oppure se non sono nel tag img da cui carichi l'immagine devi andare a creare un oggetto Image (javascript) passando il percorso del file

    Tanto per esplicitare la cosa un pochino

    codice:
    function scaleImage() { 
      var image = new Image();
      image.src = document.getElementById('eg_photo').src; 
       
      o_width = image.width; 
      o_height = image.height; 
    
      if(o_width > {MAX_WIDTH} || o_height > {MAX_HEIGHT}){ 
        //do resize 
        perc_w = {MAX_WIDTH} / o_width; 
        perc_h = {MAX_HEIGHT} / o_height; 
         
        if(perc_h > perc_w){ 
          width = {MAX_WIDTH}; 
          height = Math.round(o_height * perc_w); 
        } else { 
          height = {MAX_HEIGHT}; 
          width = Math.round(o_width * perc_h); 
        } 
         
        image.height = height; 
        image.width = width; 
         
        var warning = document.createElement('div'); 
        warning.className = 'small'; 
        warning.style.width = width + 'px'; 
        warning.innerHTML = '{EG_PHOTO_THIS} ' + o_width + ' * ' + o_height + ' {EG_PHOTO_PIXELS}' + (image.fileSize ? ' {EG_PHOTO_AND} ' + Math.floor(image.fileSize/1024) + 'KB' : '') + ' {EG_PHOTO_LARGE}. {EG_PHOTO_CLICK} {EG_PHOTO_HERE} {EG_PHOTO_TO_VIEW}'; 
         
        document.getElementById('eg_photo').parentNode.appendChild(warning); 
      } 
    }
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  5. #5
    Utente di HTML.it
    Registrato dal
    Feb 2002
    Messaggi
    1,202
    Grazie!

    Posto il codice completo e funzionante:

    Codice PHP:
    function scaleImage() { 
      var 
    imageObj = new Image();
      
    imageObj.src document.getElementById('eg_photo').src

      
    o_width imageObj.width
      
    o_height imageObj.height

      if(
    o_width > {MAX_WIDTH} || o_height > {MAX_HEIGHT}){ 
        
    //do resize 
        
    perc_w = {MAX_WIDTH} / o_width
        
    perc_h = {MAX_HEIGHT} / o_height
         
        if(
    perc_h perc_w){ 
          
    width = {MAX_WIDTH}; 
          
    height Math.round(o_height perc_w); 
        } else { 
          
    height = {MAX_HEIGHT}; 
          
    width Math.round(o_width perc_h); 
        } 
         
        var 
    image document.getElementById('eg_photo');
        
    image.height height
        
    image.width width
         
        var 
    warning document.createElement('div'); 
        
    warning.className 'small'
        
    warning.style.width width 'px'

        
    warning.innerHTML '{EG_PHOTO_CLICK} [url="javascript:void(0);"]{EG_PHOTO_HERE}[/url] {EG_PHOTO_TO_VIEW} (' o_width ' * ' o_height ' {EG_PHOTO_PIXELS})'

        
    document.getElementById('eg_photo').parentNode.appendChild(warning); 
      } 


    Debian GNU/Linux sid
    Publishing a theory should not be the end of one's conversation with the universe, but the beginning. (Eric S. Raymond)
    Kernel 2.6.14-ck1

  6. #6
    Utente di HTML.it L'avatar di hcka
    Registrato dal
    Oct 2002
    Messaggi
    435
    Io ho un problema simile: sto scrivendo il codice per creare delle scrollbar personalizzate (visto che la personalizzazione dei colori è compatibile solo con IE).

    Ho bisogno delle dimensioni delle immagini delle frecce...

    scrollArrowUp = document.createElement("img");
    scrollArrowUp.src = "arrowUp.gif";

    Se mi faccio "allertare" (o comunque printare fuori) l'altezza o la larghezza dell'immagine (scrollArrowUp.height o scrollArrowUp.width) IE mi da 0 per entrambe, mentre FireFox mi restituisce il corrispondente valore. Consigli?

    Grazie...

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.