Posted in php
173
12:45 am, September 12, 2018
 

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-&gt;fromFile($filelocation);</code></pre>
<p>Get the image width value in pixels</p>
<pre><code>$image
width = $image-&gt;getWidth();</code></pre>
<p>check if the image width is greater than the width you want to rezize.</p>
<pre><code>if($imagewidth &gt; 900) {</code><br /><code>$image-&gt;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>$new
imagefile = 'new-image.png';</code><br /><code>$image-&gt;toFile($newimagefile, 'image/png');</code><br /><code>return $newimage_file;</code></pre>
<p>Nice and easy image resizing with PHP and SimpleImage.</p>

View Statistics
This Week
93
This Month
370
This Year
247

No Items Found.

Add Comment
Type in a Nick Name here
 
Search Articles
Search Articles by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code here, mostly for my reference. Also if i find a good link, i usually add it here and then forget about it. more...

You could also follow me on twitter. I have a couple of youtube channels if you want to see some video related content. RuneScape 3, Minecraft and also a coding channel here Web Dev.

If you found something useful or like my work, you can buy me a coffee here. Mmm Coffee. ☕

❤️👩‍💻🎮

🪦 2000 - 16 Oct 2022 - Boots
Random Quote
“We ought to take outdoor walks in order that the mind may be strengthened and refreshed by the open air and much breathing.".
Seneca
Latest News
## 🚀 AI Giants Hit Bullseye: Anthropic & OpenAI Achieve Product-Market Fit Anthropic and OpenAI have reached a significant milestone, finding product-market fit with their AI technologies, which means their products effectively meet the needs of their customers, driving growth and adoption. This achievement showcases the practical value of their innovations, enabling businesses and individuals to leverage AI for enhanced productivity and efficiency. With this alignment of product and market needs, these companies are poised to transform industries and shape the future of technology.