<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>aspect-ratio &#8211; MaxVergelli.com</title>
	<atom:link href="https://www.maxvergelli.com/tag/aspect-ratio/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.maxvergelli.com</link>
	<description>sourcecode repository</description>
	<lastBuildDate>Tue, 07 Apr 2020 14:39:21 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://i2.wp.com/www.maxvergelli.com/wp-content/uploads/2020/04/wp-1586823685141.png?fit=32%2C32&#038;ssl=1</url>
	<title>aspect-ratio &#8211; MaxVergelli.com</title>
	<link>https://www.maxvergelli.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">34603511</site>	<item>
		<title>Resize an image with the same aspect ratio or with custom and crop sizes in PHP</title>
		<link>https://www.maxvergelli.com/how-to-resize-an-image-with-the-same-aspect-ratio-or-with-custom-and-crop-sizes-in-php/</link>
					<comments>https://www.maxvergelli.com/how-to-resize-an-image-with-the-same-aspect-ratio-or-with-custom-and-crop-sizes-in-php/#respond</comments>
		
		<dc:creator><![CDATA[Max]]></dc:creator>
		<pubDate>Tue, 07 Apr 2020 14:33:47 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[aspect-ratio]]></category>
		<category><![CDATA[crop]]></category>
		<category><![CDATA[custom-dimensions]]></category>
		<category><![CDATA[dimensions]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php-class]]></category>
		<category><![CDATA[php-function]]></category>
		<category><![CDATA[resize]]></category>
		<category><![CDATA[resize-image]]></category>
		<guid isPermaLink="false">http://www.maxvergelli.com/?p=193</guid>

					<description><![CDATA[In this article, I will explain how to resize an image in two different ways resizing with custom dimensions keeping the same proportions/aspect ratio Method 1: Resizing with custom dimensions The most practical method in PHP to resize an image &#8230; <a class="kt-excerpt-readmore" href="https://www.maxvergelli.com/how-to-resize-an-image-with-the-same-aspect-ratio-or-with-custom-and-crop-sizes-in-php/" aria-label="Resize an image with the same aspect ratio or with custom and crop sizes in PHP">Read More</a>]]></description>
										<content:encoded><![CDATA[
<p>In this article, I will explain how to resize an image in two different ways</p>



<ul><li>resizing with custom dimensions</li><li>keeping the same proportions/aspect ratio</li></ul>



<p><strong>Method 1: Resizing with custom dimensions</strong><br><br>The most practical method in PHP to resize an image with custom and crop sizes (without keeping the original proportions/aspect ratio) is implementing the following function: </p>



<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text/x-php&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;PHP&quot;,&quot;modeName&quot;:&quot;php&quot;}">function resize_image($file, $w, $h, $crop=false) {
    list($width, $height) = getimagesize($file);
    $r = $width / $height;
    if ($crop) {
        if ($width &gt; $height) {
            $width = ceil($width-($width*abs($r-$w/$h)));
        } else {
            $height = ceil($height-($height*abs($r-$w/$h)));
        }
        $newwidth = $w;
        $newheight = $h;
    } else {
        if ($w/$h &gt; $r) {
            $newwidth = $h*$r;
            $newheight = $h;
        } else {
            $newheight = $w/$r;
            $newwidth = $w;
        }
    }
    $src = imagecreatefromjpeg($file);
    $dst = imagecreatetruecolor($newwidth, $newheight);
    imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

    return $dst;
}</pre></div>



<p>if you set the $crop value on <em>true</em>, the image will be cropped starting from the center, otherwise it will be resized with the new values of width and height</p>



<p><strong>Method 2: keeping the aspect ratio</strong></p>



<p>The second method implements the SimpleImage class by Simon Jarvis, save in a new *.php file the following class and <em>require_once</em> it in your current script</p>



<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text/x-php&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;PHP&quot;,&quot;modeName&quot;:&quot;php&quot;}">/*
* File: SimpleImage.php
* Author: Simon Jarvis
* Copyright: 2006 Simon Jarvis
* Date: 08/11/06
* Link: http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
* http://www.gnu.org/licenses/gpl.html
*
*/

class SimpleImage {

   var $image;
   var $image_type;

   function load($filename) {

      $image_info = getimagesize($filename);
      $this-&gt;image_type = $image_info[2];
      if( $this-&gt;image_type == IMAGETYPE_JPEG ) {

         $this-&gt;image = imagecreatefromjpeg($filename);
      } elseif( $this-&gt;image_type == IMAGETYPE_GIF ) {

         $this-&gt;image = imagecreatefromgif($filename);
      } elseif( $this-&gt;image_type == IMAGETYPE_PNG ) {

         $this-&gt;image = imagecreatefrompng($filename);
      }
   }
   function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {

      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this-&gt;image,$filename,$compression);
      } elseif( $image_type == IMAGETYPE_GIF ) {

         imagegif($this-&gt;image,$filename);
      } elseif( $image_type == IMAGETYPE_PNG ) {

         imagepng($this-&gt;image,$filename);
      }
      if( $permissions != null) {

         chmod($filename,$permissions);
      }
   }
   function output($image_type=IMAGETYPE_JPEG) {

      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this-&gt;image);
      } elseif( $image_type == IMAGETYPE_GIF ) {

         imagegif($this-&gt;image);
      } elseif( $image_type == IMAGETYPE_PNG ) {

         imagepng($this-&gt;image);
      }
   }
   function getWidth() {

      return imagesx($this-&gt;image);
   }
   function getHeight() {

      return imagesy($this-&gt;image);
   }
   function resizeToHeight($height) {

      $ratio = $height / $this-&gt;getHeight();
      $width = $this-&gt;getWidth() * $ratio;
      $this-&gt;resize($width,$height);
   }

   function resizeToWidth($width) {
      $ratio = $width / $this-&gt;getWidth();
      $height = $this-&gt;getheight() * $ratio;
      $this-&gt;resize($width,$height);
   }

   function scale($scale) {
      $width = $this-&gt;getWidth() * $scale/100;
      $height = $this-&gt;getheight() * $scale/100;
      $this-&gt;resize($width,$height);
   }

   function resize($width,$height) {
      $new_image = imagecreatetruecolor($width, $height);
      imagecopyresampled($new_image, $this-&gt;image, 0, 0, 0, 0, $width, $height, $this-&gt;getWidth(), $this-&gt;getHeight());
      $this-&gt;image = $new_image;
   }      

}
</pre></div>



<p>then you can instantiate the above class in the following way in your script, to resize an image keeping the same proportions:</p>



<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror" data-setting="{&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text/x-php&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;PHP&quot;,&quot;modeName&quot;:&quot;php&quot;}">$filename = 'your filename here!';		

list($width, $height) = getimagesize($filename);
	
$max_width = 1920;
$max_height = 1080;
$new_width = 1280;
$new_height = 720;

if($width &gt;= $height){
  $ratio = $max_width / $width;
  $new_height = $height * $ratio;
} else {
  $ratio = $max_height / $height;
  $new_width = $width * $ratio;
}

$image = new SimpleImage();
$image-&gt;load($filename);
$image-&gt;resize($new_width, $new_height);
$image-&gt;save($filename);
</pre></div>
]]></content:encoded>
					
					<wfw:commentRss>https://www.maxvergelli.com/how-to-resize-an-image-with-the-same-aspect-ratio-or-with-custom-and-crop-sizes-in-php/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">193</post-id>	</item>
	</channel>
</rss>
