Image Resizing with PHP and SimpleImage Library
<p><img src="https://kruxor.com/images/measurements.jpg" /></p>
<p>How to resize images with PHP using the SimpleImage Library</p>
<p>I always forget how to do this even though its pretty simple once you know what to do.</p>
<p>Here is how i resize images if they are over a certain width. All you should need for this is the simple image class and the image file location on the server.</p>
<p>Lets initialise the simple image class. (you can get the latest version from here : https://github.com/claviska/SimpleImage )</p>
<pre><code>requireonce("lib/SimpleImage.php");</code><br /><br /><code>$imagelocation = "myimage.jpg";</code><br /><code>$image = new \crud\SimpleImage();</code><br /><code>$image->fromFile($filelocation);</code></pre>
<p>Get the image width value in pixels</p>
<pre><code>$imagewidth = $image->getWidth();</code></pre>
<p>check if the image width is greater than the width you want to rezize.</p>
<pre><code>if($imagewidth > 900) {</code><br /><code>$image->resize(900,null);</code><br /><code>}</code></pre>
<p>if you leave the second variable null then it will auto resize to 900 width.</p>
<p>then lets save it as a jpg and return the image file name</p>
<pre><code>$newimagefile = 'new-image.png';</code><br /><code>$image->toFile($newimagefile, 'image/png');</code><br /><code>return $newimage_file;</code></pre>
<p>Nice and easy image resizing with PHP and SimpleImage.</p>