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

Image Resizing with PHP and SimpleImage Library

How to resize images with PHP using the SimpleImage Library

I always forget how to do this even though its pretty simple once you know what to do.

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.

Lets initialise the simple image class. (you can get the latest version from here : https://github.com/claviska/SimpleImage )

require_once("lib/SimpleImage.php");

$image_location = "myimage.jpg";
$image = new \crud\SimpleImage();
$image->fromFile($file_location);

Get the image width value in pixels

$image_width = $image->getWidth();

check if the image width is greater than the width you want to rezize.

if($image_width > 900) {
$image->resize(900,null);
}

if you leave the second variable null then it will auto resize to 900 width.

then lets save it as a jpg and return the image file name

$new_image_file = 'new-image.png';
$image->toFile($new_image_file, 'image/png');
return $new_image_file;

Nice and easy image resizing with PHP and SimpleImage.

View Statistics
This Week
74
This Month
230
This Year
265

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...

Subscribe to weekly updates about things i have added to the site or thought interesting during the last week.

You could also follow me on twitter or not... does anyone even use twitter anymore?

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
In this case my anchor this week becomes driving almost 2hrs outside of Atlanta to one of my favorite hard core gyms in the world.. MetroFlex aka The Dungeon. The gym owners turn the heat way up so it becomes a fun sweat box and the gym members just watch from afar and leave me alone. I happily drive myself long distances to find MY ANCHOR. Our anchor allows us to have balance, focus and be as productive as possible. And if you're in the middle of a heavy set and your headphones start to fall off your head, like mine did here.. well.. f*ck the headphones. Let em break and fall. You can always get a new pair, but the iron ain't ever gonna lift itself.
Unknown
Random CSS Property

user-select

The user-select CSS property controls whether the user can select text. This doesn't have any effect on content loaded as part of a browser's user interface (its chrome), except in textboxes.
user-select css reference