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 500x500 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:
JResizr
This plugin have ability to override default worpdress behavior and disable croping image but try to resize image to fit rectangle container size and fill space with background color. You also able to choose background color to use.
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
contain
This keyword specifies that the background image should be scaled to be as large as possible while ensuring both its dimensions are less than or equal to the corresponding dimensions of the background positioning area.
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 your functions.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 google PHP image resize.
In your html where you have <img> tag, add a width attribute, and set it to 100% that should auto resize your image to its containers size
<img src="imageNamw.jpg" alt="imageAlt" width="100%" />
You 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
image_resize( $file, $max_w, $max_h, $crop, $suffix, $dest_path, $jpeg_quality );
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.
Related
I am using ImageMagick with PHP to resize images (using its commandline tool "convert").
I am able to resize images and everything works.
What I want to do is I want to resize an image (of any dimension) to an image setting the minimum width and maximum height and at the same time preserve the image resolution/aspect ratio. How do I do this?
Why I want this: I am putting these images in a news feed in my website where I require images to be of width:100% but the problem is height becomes too large for certain images and occupies an entire page. I want to avoid it.
This is what I am using for resize:
$cmd="convert -thumbnail ".$width."x".$height." \"".$source."\" \"".$dest."\";";
You can find the resize options on the Imagemagick website: http://www.imagemagick.org/script/command-line-processing.php#geometry
I do not think these will achieve what you want so I would check the dimensions of your image and calculate the height to go with your width which you can use in your convert code.
I am a newbie to web development and am trying to build my first website. I use PHP, Jquery and Bootstrap3 for my development. I want to add a Faceebook like cover photo upload and reposition feature on my site and it's been bit of a head ache. I have implemented all the features successfully by taking help from tutorials available here and here, but both of them are not perfect.
In both the tutorials, everything works fine until the final stage where you save the new repositoned image.
In the first approach, upon save, the new position of the image, the height and width of the container div are taken as inputs and a new image is generated using imagecreatetruecolor($width, $height);. The original image is saved separately and used if you want to reposition again.
In the second approach, simply the new position of the image is saved in the db and the same is set as "margin-top" for the (so called) repositioned image on page load.
Problems
The first approach is actually doing a good job by saving the images separately. However, the issue here is, the dimensions of the new image are set as per the dimensions of the container div. Since my container div is responsive, the height and width vary on screen size, and so does the actual size of the image. This is a disaster because if you crop the image on a mobile screen, the width is set to 200px and the same image is loaded on large screens, where the container width is close to 600px. It creates an empty whitespace.
The second approach is not doing anything but position the same image using css. The original image of big size is unnecessarily loaded every time.
I have implemented the first approach and made a lot of changes to fit my needs.
The problem is better illustrated in this .gif image on Imagur. Also please refer my other Stack Overflow question Scale down images effectively without losing aspect ratio or quality in php for more detailed explanation.
I have been struggling for more than a month now to figure out how to effectively crop the new image and save it so that it is consistent on all screen sizes.
I hope some expert can give me a solution for this so that I can launch my dream website without further delay.
Thank you
You don't have to generate a new image if the size of the screen changes. You can simply use percentages to maintain the original aspect ratio of the image.
You simply need to set the container to whatever width you want, then don't set the height, and set a child img to fill the entire width of the container. The height of the image will automatically scale to maintain the original aspect ratio:
div.aspect
{
width: 75%;
}
div.aspect img
{
width: 100%;
}
JSFiddle
Hi Im using the_post_thumbnail for the nivo slider, my issue is that I cant seem to find a nice easy way to automatically resize the images to exact dimensions.
When I use add_image_size it doesnt stretch it correctly, it keeps the constraint on. I dont want cropping or anything. I just want the user to upload the picture and not have to do anything after that.
Its probably a stupid but I have googled it and even though there is lots about it, including various plugins, I cant seem to find a solid answer to this.
Thanks
I think you need to clarify your question. If I understand correctly you want the uploaded image to be the exact image without cropping? Make sure that the upload is saved before calling the_post_thumbnail function of the nivo slider. That way you have the raw image to manipulate before the function gets it.
The add_image_size()-method has a flag for the crop method you wish to use. both of them will keep the proportions of the image intact however.
You could extend the functionality or build your own plugin to handle the resizing in the way you want - you may want to take a look at /wp-includes/media.php and a simple resizing class like https://github.com/mdular/ReSample.php/blob/master/ReSample.php
I'm not a big fan of distorting the image proportions though. If the client is unable to differentiate between a portrait or landscape aspect ratio then cropping the image rather than stretching it seems like the more elegant solution in most cases.
For a quick & dirty solution to your problem though i suggest the following:
add the image size and contrain it by the width OR height to fit your sliders width OR height (this example constrains only by width):
add_image_size( 'slider-image', 760, 9999 ); //760 pixels wide, "unlimited" height
when outputting the image tags, force the dimensions to 760 x 474 in the image tag
< img src="" width="760" height="474" ...>
This will leave you with image files that are potentially taller or wider than what you want but will be forced to the set dimensions in the browser, and therefore fit the slider. It's dirty, yes, but it will work with the tools you currently have.
Update
in response to your comment: you can construct the image tag yourself by getting the raw image info.
1) grab the post meta, like so (assuming the_post() is already executed, it should be if you are in the while-loop):
$meta = get_post_meta($post->ID)
2) check for the thumbnail id to avoid empty output (and do steps 3 + 4 inside the if-block)
if(!empty($meta['_tumbnail_id'][0])
3) grab the actual thumbnail raw data - be sure to fill in the 'THUMBNAIL VERSION' (size) you want ('thumbnail', 'medium', etc.. or your own if you used add_image_size() in your functions.php)
$image = wp_get_attachment_image_src( $meta['_thumbnail_id'][0], 'VERSION');
4) construct the image tag:
$image[0] = the src url
$image[1] = the width - you may use this
$image[2] = the height - you may wish to force this to your liking
hope this helps :)
I have a mobile slideshow that I want to have a fullscreen option for. I have created a pseudo-fullscreen CSS that works fine but it stretches the image to fill. I have tried some techniques like wrapping it in a div and setting the CSS to auto for height and width but it still stretches.
The CSS just sets all to 0 top bottom left and right. I know its a "hack" but this works great for my HTML5 video it even keeps the ratio. not so hot for images though. I have tried the CSS3 object-fit but that has little to no browser support afaik.
Is there a way to get the image to go fullscreen and keep the aspect ratio? maybe using a letterbox like look to fill the other space(either vertically or horizontally). I need this to be dynamic so it will work on multiple mobile devices with different screen sizes. I have seen this work on google images on my mobile devices but obviously they are more clever than I am.
I am using jQuery Mobile and PHP on the back end. I was thinking a JS solution would be best but PHP would be welcome too.
I believe you're looking for the background-size property. You can set it to either contain or cover as of CSS3.
contain, which specifies that the background image should be scaled to
be as large as possible while ensuring both its dimensions are less
than or equal to the corresponding dimensions of the background
positioning area.
cover, which specifies that the background image should be scaled to
be as small as possible while ensuring both its dimensions are greater
than or equal to the corresponding dimensions of the background
positioning area.
MDN Docs: https://developer.mozilla.org/en/CSS/background-size
background-size : cover would be a full-screen image and background-size : contain would be the letterbox effect.
I'm using phpThumb in a script to clean my images and add a watermark to them. We have images of very different sizes (from 100px width to 800px), so no matter what watermark image I use, it's either going to look too small or too big on the image.
Do you know of a way to tell phpThumb to resize the watermark? Or is there a way to resize the watermark image (depending on size of image being watermarked)?
Thanks a lot guys!
Ali
I am facing the same issue.
Maybe you could put some if statements in the phpthumb.config file in the DEFAULT PARAMETER SECTION
then depending on the height and width parameters you can decide to use different files with varying sizes for the watermark.
make sure you change $PHPTHUMB_DEFAULTS_GETSTRINGOVERRIDE to false
This is not perfect cause you could not know the source image size to start with.
but it would be a better fit based on the parameters passed to phpthumb.
I guess the only other solution would be to use phpthumb as an object but that would require a lot of work.