Code Sport 16 Sep , 2013
Hi All. Ever wanted an all purpose, easy to use method of resizing your images in PHP? Well that’s what PHP classes are for – reusable pieces of functionality that we call to do the dirty work behind the scenes. We’re going to learn how to create our own class that will be well constructed, as well as expandable. Resizing should be easy. How easy? How about three steps!
Let’s say we have a Products listing page & various food products are listed with its thumbnail images like Cheese packet, Honey Bottle, Tea packets etc. These products are not supposed to be having all same size images. But we have to show that as thumbnail with proper resolution.
So here is what we can do to achieve it easily.
1. Decide the maximum image width & height
2. Include the function code below into your code & call it like this where you want to display the image
$image = Path of the original image
$maxwidth = maximum width decided for the image
$maxheight = maximum height decided for the image
As you can see, this was a very simple 2 step task to do. The way this function will work is, it will first decide the New height of the image Based on the Maximum Width.
After that it will check if the New Height goes beyond the Maximum height, then it will calculate the Maximum Width based on the New Height. This way it will calculate both Height & the Width to fit in the decided Maximum Height/Width.
or you can also do this by the help of following code
So guys I hope these peace of code gets helpful for you in coding. Leave a comment if you like this post.