Posted in
2922
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
69
This Month
376
This Year
787
Add Comment
Other Items in php
Related Search Terms
Other Categories in Articles
ai api and javascript apps bootstrap data imports docker forster funny images gaming linux marriage minecraft nft newsletters osrs operating systems photoshop pi rs3 slayer random code random things site development site updates tech tool documentation tools and utilities utilities vpn video notes videos web dev windows apps android apple apps atom bootstrap charts code code thoughts comments cooking core crypto css designs factorio games google hardware how to howto ios ideas linux mac music new world osrs php pi400 privacy python random random stats random web code random web things rs3 runescape site bugs slick slider terraria valheim videos web development webdev windows wordpress wow wow classic youtube