Skip to content
This repository has been archived by the owner on Jun 9, 2019. It is now read-only.
/ smart_resize_image Public archive

(Written in 2007-08) PHP function for image resizing that does what you think it should, the right way.

Notifications You must be signed in to change notification settings

maxim/smart_resize_image

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 

Repository files navigation

smart_resize_image

Should be easy to find, right? All you want is a function that resizes an image to constraints (doesn’t care if it should scale up or down), with possibility to select if you want to keep it proportional, and possibility to use either width or height as the constraint. Also, you want it to preserve transparency damn it! Surprisingly, I was unable to find a good function that does all that, so I decided to attempt writing it. This should do it.

Note: Original announcement and comments for the function are available at mediumexposure.com/techblog/smart-image-resizing-while-preserving-transparency-php-and-gd-library

Features/Usage:

* If you pass width as 0 (zero) -- this function will disregard width, and use height as constraint. Same vice versa.
* If you set "proportional" to false - the function will simply stretch (or shrink) the image to its full constraints.
* If one of the dimensions is set to zero, and proportional set to "false" - then the image will be forced to stretch or shrink the other dimension, and disregard the zeroed dimension (leave it the same).
* If proportional is set to true - the image will resize to constraints proportionally, once again, with possibility to have either width or height set to zero.
* The function can use either linux "rm" command, or php @unlink. Most probably you don't need to ever use that flag, but on some setups - @unlink won't work due to user access restrictions.
* The function will simply replace the file that you give it, with the resized file.
* The function supports gif, png, and jpeg, and preserves the transparency of gif and png images.
* Tested on GD version 2.0.28 only.

Essentially, everything happens just as logically expected. Please, if you see anything wrong, and you know ways to enhance, or optimize it, let me know.

Update

There is a new parameter “output” which can be set to either

* "file" - overwrite the given file (default)
* "browser" - output image through http - with correct mime type
* "return" - return GD Library Image object
* or any filename of choice - save changed version to output path

Update

There is another new parameter “delete_original”. Speaks for itself.

License

Copyright © 2008 Maxim Chernyak

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

(Written in 2007-08) PHP function for image resizing that does what you think it should, the right way.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages