Salta al contenuto
  • CATEGORIES
  • La Community Connect.gt
  • Presentati alla Community
  • News Ufficiali da Connect.gt
  • Internet News
  • A Scuola Di
  • Digital Marketing
  • Intelligenza Artificiale
  • SEO & GEO
  • Web Marketing e Content
  • Google Analytics e Web Analytics
  • Google Ads & Advertising
  • YouTube
  • TikTok, Instagram e altri Social
  • Google Adsense, Affiliazioni e Guadagno
  • E-Commerce
  • Coding e Sistemistica
  • Help Center: consigli per il tuo progetto
  • Hosting e Cloud
  • Coding
  • CMS & Piattaforme Self-Hosted
  • Annunci e Collaborazioni
  • Annunci Vendita e Acquisto: Pubblicità, Domini, Link e altro
  • Collaborazioni WEB e Lavoro: offro e cerco
  • Impresa, Fisco e Leggi
  • Consulenza Legale e Professioni Web
  • Consulenza Fiscale, Imprese, Startup e P.Iva
  • Gli Off Topic
  • PC, Smartphone e altri Hardware
  • Tutti i Software
  • GT Fetish Cafè
  • Discussioni
  • 0 Non letti 0
  • Recenti
  • Hashtag
  • Popolare
  • Utenti
  • Privacy
  • Termini e condizioni
  • © 2026 Search On Media Group S.r.l.
Collassa
Connect.gt
Il network Search On
Connect.gt WMF AI Festival Accademia Advanced SEO Tool AgentCy Social Media Strategies Search Tech
Registrati Accedi
Connect.gt
Stream Interest Categorie Hashtag
  1. La community italiana di SEO, marketing digitale e AI
  2. Categorie
  3. Coding e Sistemistica
  4. Coding
  5. Ridimensionare immagine in una quadrata con sfondo bianco

Ridimensionare immagine in una quadrata con sfondo bianco

Pianificato Fissato Bloccato Spostato Coding
2 Post 2 Autori 507 Visualizzazioni 1 Seguito
  • Da Vecchi a Nuovi
  • Da Nuovi a Vecchi
  • Più Voti
Rispondi
  • Risposta alla discussione
Effettua l'accesso per rispondere
Questa discussione è stata eliminata. Solo gli utenti con diritti di gestione possono vederla.
  • B
    B
    bonox
    scritto su ultima modifica di
    User 0
    #1
    Ridimensionare immagine in una quadrata con sfondo bianco

    Ciao
    tramite questa funzione in php e ajax carico un'immagine che vienne ridimensionata, vorrei che questa immagine venisse inserita in un quadrato con sfondo bianco per diventare l'immagine definitiva, per spiegarmi meglio:
    se la mia immagine è 800x600 vorrei che venisse inserita in una immagine quadrata bianca di 500x500 mantenendo le stesse proporzioni e avendo, quindi, un borrdo bianco sopra e uno sotto
    grazie

    class Resize_Image {
    
        var $image_to_resize;
        var $new_width;
        var $new_height;
        var $ratio;
        var $new_image_name;
        var $save_folder;
    
        function resize()
        {
            if(!file_exists($this->image_to_resize))
            {
              exit("File ".$this->image_to_resize." does not exist.");
            }
    
            $info = GetImageSize($this->image_to_resize);
    
            if(empty($info))
            {
              exit("The file ".$this->image_to_resize." doesn't seem to be an image.");
            }
    
            $width         = $info[0];
            $height         = $info[1];
            $mime         = $info['mime'];
    
            /*
            Keep Aspect Ratio?
    
            */
    
            if($this->ratio)
            {
                // if preserving the ratio, only new width or new height
                // is used in the computation. if both
                // are set, use width 
    
                if (isset($this->new_width))
                {
                    $factor = (float)$this->new_width / (float)$width;
                    $this->new_height = $factor * $height;
                }
                else if (isset($this->new_height))
                {
                    $factor = (float)$this->new_height / (float)$height;
                    $this->new_width = $factor * $width;
                }
                else
                    exit('neither new height or new width has been set');
            }
    
            // What sort of image?
    
            $type = substr(strrchr($mime, '/'), 1);
    
            switch ($type)
            {
            case 'jpeg':
                $image_create_func     = 'ImageCreateFromJPEG';
                $image_save_func     = 'ImageJPEG';
                $new_image_ext         = 'jpg';
                break;
    
            case 'png':
                $image_create_func     = 'ImageCreateFromPNG';
                $image_save_func     = 'ImagePNG';
                $new_image_ext         = 'png';
                break;
    
            case 'bmp':
                $image_create_func     = 'ImageCreateFromBMP';
                $image_save_func     = 'ImageBMP';
                $new_image_ext         = 'bmp';
                break;
    
            case 'gif':
                $image_create_func     = 'ImageCreateFromGIF';
                $image_save_func     = 'ImageGIF';
                $new_image_ext         = 'gif';
                break;
    
            case 'vnd.wap.wbmp':
                $image_create_func     = 'ImageCreateFromWBMP';
                $image_save_func     = 'ImageWBMP';
                $new_image_ext         = 'bmp';
                break;
    
            case 'xbm':
                $image_create_func     = 'ImageCreateFromXBM';
                $image_save_func     = 'ImageXBM';
                $new_image_ext         = 'xbm';
                break;
    
            default:
                $image_create_func     = 'ImageCreateFromJPEG';
                $image_save_func     = 'ImageJPEG';
                $new_image_ext         = 'jpg';
            }
    
            // New Image
            $image_c = ImageCreateTrueColor($this->new_width, $this->new_height);
    
            $new_image = $image_create_func($this->image_to_resize);
    
            imagealphablending( $image_c, false );
            imagesavealpha( $image_c, true );
            
            ImageCopyResampled($image_c, $new_image, 0, 0, 0, 0, $this->new_width, $this->new_height, $width, $height);
    
            if($this->save_folder)
            {
                if($this->new_image_name)
                {
                    $new_name = $this->new_image_name;//.'.'.$new_image_ext;
                }
                else
                {
                    $new_name = $this->new_thumb_name( basename($this->image_to_resize) ).'_resized.'.$new_image_ext;
                }
    
                $save_path = $this->save_folder.$new_name;
            }
            else
            {
            /* Show the image without saving it to a folder */
               header("Content-Type: ".$mime);
    
               $image_save_func($image_c);
    
               $save_path = '';
            }
    
            $process = $image_save_func($image_c, $save_path);
    
            return array('result' => $process, 'new_file_path' => $save_path);
    
        }
    
        function new_thumb_name($filename)
        {
            $string = trim($filename);
            $string = ereg_replace("%20", " ", $string);
            $string = strtolower($string);
            $string = trim(ereg_replace("[^ A-Za-z0-9_]", "", $string)); // tolto spazio
            $string = ereg_replace("[ tnr]+", "_", $string);
            $string = str_replace(" ", '_', $string);
            $string = ereg_replace("[ _]+", "_", $string);
            
    
            return $string;
        }
    }
    
    1 Risposta Ultima Risposta
    Miglior risposta 0
    0
    • G
      G
      gigi991
      scritto su ultima modifica di
      User Attivo 0
      #2

      Potresti sottrarre ai valori massimi di larghezza e altezza (500x500?) i valori finali dell'immagine ridimensionata (es.500x300)

      Quindi prendi il valore al di sotto del valore massimo (in questo caso 300, ma hai già il check sopra per capire quale dei due avrà bordo bianco) e lo dividi per 2.

      Il risultato lo usi come coordinata quando piazzi l'immagine ridimensionata nel quadro bianco.

      Credo sia qui:

      ImageCopyResampled($image_c, $new_image, $this->margin_x, $this->margin_y, 0, 0, $this->new_width, $this->new_height, $width, $height);

      1 Risposta Ultima Risposta
      Miglior risposta 0
      0

      Ciao! Sembra che tu sia interessato a questa conversazione, ma non hai ancora un account.

      Stanco di dover scorrere gli stessi post a ogni visita? Quando registri un account, tornerai sempre esattamente dove eri rimasto e potrai scegliere di essere avvisato delle nuove risposte (tramite email o notifica push). Potrai anche salvare segnalibri e votare i post per mostrare il tuo apprezzamento agli altri membri della comunità.

      Con il tuo contributo, questo post potrebbe essere ancora migliore 💗

      Registrati Accedi
      Rispondi
      • Risposta alla discussione
      Effettua l'accesso per rispondere
      • Da Vecchi a Nuovi
      • Da Nuovi a Vecchi
      • Più Voti


      • Accedi

      • Non hai un account? Registrati

      • Accedi o registrati per effettuare la ricerca.
      • Primo post
        Ultimo post
      Home Accedi Registrati
      Menu
      • CATEGORIES
      • La Community Connect.gt
      • Presentati alla Community
      • News Ufficiali da Connect.gt
      • Internet News
      • A Scuola Di
      • Digital Marketing
      • Intelligenza Artificiale
      • SEO & GEO
      • Web Marketing e Content
      • Google Analytics e Web Analytics
      • Google Ads & Advertising
      • YouTube
      • TikTok, Instagram e altri Social
      • Google Adsense, Affiliazioni e Guadagno
      • E-Commerce
      • Coding e Sistemistica
      • Help Center: consigli per il tuo progetto
      • Hosting e Cloud
      • Coding
      • CMS & Piattaforme Self-Hosted
      • Annunci e Collaborazioni
      • Annunci Vendita e Acquisto: Pubblicità, Domini, Link e altro
      • Collaborazioni WEB e Lavoro: offro e cerco
      • Impresa, Fisco e Leggi
      • Consulenza Legale e Professioni Web
      • Consulenza Fiscale, Imprese, Startup e P.Iva
      • Gli Off Topic
      • PC, Smartphone e altri Hardware
      • Tutti i Software
      • GT Fetish Cafè
      • Discussioni
      • Non letti 0
      • Recenti
      • Hashtag
      • Popolare
      • Utenti
      • Privacy
      • Termini e condizioni
      • © 2026 Search On Media Group S.r.l.