<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Ridimensionare immagine in una quadrata con sfondo bianco]]></title><description><![CDATA[<p dir="auto">Ciao<br />
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:<br />
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<br />
grazie</p>
<pre><code>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-&gt;image_to_resize))
        {
          exit("File ".$this-&gt;image_to_resize." does not exist.");
        }

        $info = GetImageSize($this-&gt;image_to_resize);

        if(empty($info))
        {
          exit("The file ".$this-&gt;image_to_resize." doesn't seem to be an image.");
        }

        $width         = $info[0];
        $height         = $info[1];
        $mime         = $info['mime'];

        /*
        Keep Aspect Ratio?

        */

        if($this-&gt;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-&gt;new_width))
            {
                $factor = (float)$this-&gt;new_width / (float)$width;
                $this-&gt;new_height = $factor * $height;
            }
            else if (isset($this-&gt;new_height))
            {
                $factor = (float)$this-&gt;new_height / (float)$height;
                $this-&gt;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-&gt;new_width, $this-&gt;new_height);

        $new_image = $image_create_func($this-&gt;image_to_resize);

        imagealphablending( $image_c, false );
        imagesavealpha( $image_c, true );
        
        ImageCopyResampled($image_c, $new_image, 0, 0, 0, 0, $this-&gt;new_width, $this-&gt;new_height, $width, $height);

        if($this-&gt;save_folder)
        {
            if($this-&gt;new_image_name)
            {
                $new_name = $this-&gt;new_image_name;//.'.'.$new_image_ext;
            }
            else
            {
                $new_name = $this-&gt;new_thumb_name( basename($this-&gt;image_to_resize) ).'_resized.'.$new_image_ext;
            }

            $save_path = $this-&gt;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' =&gt; $process, 'new_file_path' =&gt; $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;
    }
}
</code></pre>
]]></description><link>https://dev.connect.gt/topic/212980/ridimensionare-immagine-in-una-quadrata-con-sfondo-bianco</link><generator>RSS for Node</generator><lastBuildDate>Wed, 25 Nov 2026 16:05:33 GMT</lastBuildDate><atom:link href="https://dev.connect.gt/topic/212980.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 08 Apr 2015 22:44:56 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Ridimensionare immagine in una quadrata con sfondo bianco on Wed, 08 Apr 2015 23:20:25 GMT]]></title><description><![CDATA[<p dir="auto">Potresti sottrarre ai valori massimi di larghezza e altezza (500x500?) i valori finali dell'immagine ridimensionata (es.500x300)</p>
<p dir="auto">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.</p>
<p dir="auto">Il risultato lo usi come coordinata quando piazzi l'immagine ridimensionata nel quadro bianco.</p>
<p dir="auto">Credo sia qui:</p>
<p dir="auto">ImageCopyResampled($image_c, $new_image, $this-&gt;margin_x, $this-&gt;margin_y, 0, 0, $this-&gt;new_width, $this-&gt;new_height, $width, $height);</p>
]]></description><link>https://dev.connect.gt/post/1195069</link><guid isPermaLink="true">https://dev.connect.gt/post/1195069</guid><dc:creator><![CDATA[gigi991]]></dc:creator><pubDate>Wed, 08 Apr 2015 23:20:25 GMT</pubDate></item></channel></rss>