i have image with various size. and i also have various container to place those image.
example : i have 680 x 1024 image that will placed on 500×500 container without cropping.
so i thought that i will need to build image with container size, than put resized image on top of it.
result that i expected is something like this
or this
how the best way to achieve this on PHP or wordpress?
I wrote a plugin, currently at WordPress plugin repository:
There are several ways to accomplish this. One alternative to javascript/php, if the images are roughly the right size, is just use CSS, specifically
background-size: contain
You can experiment with it at http://jsfiddle.net/RxTLS/
Keep in mind this is mostly supported, although IE8 and below do not. I believe there are some polyfills our there though if you’re worried about IE users.
This solution is only ideal if the images you are uploading are close to the size they will be displayed however. Trying to do this with images that are over 3000 pixels wide/tall is not advised for several different reasons.
In that case, you’d want to shrink them in PHP first. If you’re uploading the images through WordPress then you can use
add_image_size
in yourfunctions.php
and the images will be automatically resized when you upload them. Alternatively, you can do it manually in PHP as well and there are plenty of tutorials on how to do that out there. I’d just googlePHP image resize
.In your html where you have
<img>
tag, add awidth
attribute, and set it to100%
that should auto resize your image to its containers sizeYou could write a simple Js file that would accomplish that.
I have done such thing before; So basically you compare the height and the width of the image in its container. then if the height is longer than the width you set the height to 500px and set the width to auto;
else if the width is longer than the height you set the width to 500px and the height to auto;
else you set the width and the height to 500px;
set the container vertical align to middle and text-align to center in css and that should do the trick;
You can also try a simple timthumb library to resize images in any size.
Please check the timthumb library code here :
http://timthumb.googlecode.com/svn/trunk/timthumb.php
In WordPress it would be simply by using their image_resize function
where you would set the file, width of the container, height of the container and false (as you want to resize, not crop). The others are optional and you should fill them if you have special needs for the new file destination or name or quality. If you entered everything correctly, the function should return path to your newly resized image.
Note that with WordPress you can actually do this automatically when uploading pics so then later you just retrive the already resized picture – take a look at add_image_size function.