PHP image resize ratio issue - php

Php thumb resize image based on provided width and height, I am working on profile image which is: 130x130
While users are uploading image of long heights(rectangle) which leads to strecthed image in its width and when uploading long width image(rectangle) then strecthed, I hope you understand what I mean. Because when anyone uses a large square image it resize to correct 130x130 but longer width and height get strecthed.
I am thinking of having a good suggestion or idea to work around such images, either crop them from top to make them perfect square first.
Thanks,
Najm.

Usually you will take the longer side, scale it up and scale the shorter side by the same percentage. One such snippet is here. You can also prefill the canvas with a smooth background that fits with your overall design.

Related

PHP imagecopyresampled + how to get the best results

I have the following working fine:
User uploads an image and the image is resized using imagecopyresampled and imagejpeg (keeping the proportions) so it fits the cropping DIV.
The user then crops the image with JCrop and then the resized above image is then cut into the 5 required crop sizes using imagecopyresampled and imagejpeg.
The problem I've found is by cropping from the resized image doesn't take advantage of the very original image that is often larger and better quality.
So I want to display an image that fits the DIV but crop from the very original image to get the best quality.
When I just change the cropping to the original image the crop is of the wrong part of the image as the sizing is different. This is obvious I assume. Diff sized images.
How can I display an image that fits the DIV for Jcrop but then actually crop off the original which is often larger?
Ideas I'm testing:
displaying the original image in the DIV (not resized by php but resized by CSS). I'm not sure if this will make any difference.
try to use some math based equation to get the correct crop. Unsure how I'll do this currently.
Ideas would be great...
thx
Personally I would go with the maths based solution if you have to work with the full sized image server side. Re-scaling in the browser directly or through CSS is not ideal IMO and differs greatly between browsers. If you use the math based option you could calculate the co-ordinates on the smaller image crop and scale that up to get the right portion of the original image.
On a side note you may find (depending on the image sizes and image types you allow uploaded) that copying and resizing on the PHP side is very CPU and memory intensive. A png file for example of roughly 1024 x 768 in high resolution can take up to 60MB to 80MB of RAM just to resize (per image re-sample due to compression on the png file) and that is regardless of which PHP image manipulation modules (ImageMagick, etc) you use. There is no perfect way to handle image uploads from the PHP server end (other than throwing heaps of memory and CPU at the task). There are some good jQuery solutions however that resize on the client side before the upload (e.g. Plupload, etc) which means you will only be working with a reduced size image on the server side. There are also some JQuery client side cropping scripts which are good. Either way a combination of PHP and jQuery would be the best IMO.

How to crop large image in small form without trimming before crop?

Does someone knows the image-crop which correctly works with the 'dynamically changed in the CSS image size (I mean width and height)'?
All image-crops works fine if there is original image size (for example size: 400px x 400px) and width:100%;
But if show to user picture with sizes 1500px x 1500px and make it smaller just with css to 100px x 100px (because 1500px - usually to much for screen and looks not pretty) - in this case image-croppers what i saw before issued an error or makes a thumbnail with initial values (top-left corner). I need to pull out pictures from Facebook albums, and usually they are too large.
I know that it is possible to reduce the image before crop, but then deteriorates the quality.
By the way, looks like there is way to make this, because in jCrop preview thumbnail looks fine even if i 'dynamically' change size of image in css. Just preview! Completed cropped image is not correct, as I said there would be the upper left corner of the image.
Okay, the solution was: divide the original image size to "smaller in the css" size.
We obtain the coefficient, before sending it to crop - multiply x1, y1, height, width to this coefficient. profit!
If what you're looking for is a responsive or fluid image cropper (that adapts to the screen) you should try Guillotine, check out the demo and resize the browser's window to see how it adapts.

php resize thumbnail to fixed dimensions - no crop

I want to resize an image to a fixed size of 400x300.
I do not want to zoom crop it, I simply want the image constrained to either the width 400 or height 300 (depending on orientation), the image centered, and the rest of the image filled with black.
Does anyone know of a library/code that does this? (for GD)
Thanks,
Wesley
GD or ImageMagick can easily do this for you.
Create a new image with a black background of the desired height, then copy from the original, and place in the new.
Perhaps another answer will come along with the full code, but this will get you started. If not, I will post code for you later.

Picture Size problems in HTML when working is screen resolutions

I'm running a small application which has an 70% width, and 90% height value, as the rest of the screen is menus.
In this space I need to echo images which isn't problem, but if the image is too big it creates an overflow. If have fixed properties for the image depending on what the images' original size is, it will destroy the quality or look of the image. Can someone tell me how it is possible to echo the image without overflowing or destroying the images quality?
Something along the lines of how Facebooks image viewer works.
Don't constrain both width and height then.
Constrain one or the other and let the browser resize the image according to its ratio. If you tell the browser to resize your 100x100 image to 60x2, it's going to look terrible, no matter what. But if you tell your browser to resize your image to height 60, it will automatically make the width 60 to keep the image's proportions, and it won't be distorted.
If you want to go with an image gallery, you can. Here's one that looks similiar to facebook's from a quick google : http://phpimagegallery.com/

Automatically align and resize 3 images into a small image with PHP?

I don't know if this is even possible with PHP, but I figured if it is, someone here will know how. I'm currently working on a project where users can customize a full body main avatar to be used throughout the site. There are a bunch of different face, hair, etc transparent png images that can be selected to make their custom avatar. I have this working great, but here is the hard part. I want to be able to use the face, hair, and beard (if male), and automatically create an 80x80 image that will be used as their small avatar for forum posts, etc.
There are a few obstacles with this. First, all of the images are 187x404 (big amounts of the image are transparent, the character body image is achieved by stacking the images, so a face image isn't actually that big). For this to work, the images would effectively have to be automatically cropped so that all of the extra space was removed and the actual face, hair, or beard part showed in the 80x80 spot.
The second issue is that some of the hair or beards (when placed on the full-size face image) would extend past the 80x80 and be chopped off. So the image would have to be pieced together at full size, and then resized to fit in 80x80.
I know the basic way of combining the 3 images into one (Combine 2-3 transparent PNG images on top of each other with PHP), but that is as far as I've gotten. If I'm crazy and this isn't possible then tell me. I'm probably way overcomplicating this, so if you see and obviously easier way to achieving this, I would love to hear it.
I think you need to decide first, cropping, resizing or a combination of both (cropping to a bigger square and resizing that).
Anyway, if you already have the images combined into one, all three options are easy to do in php. Take a look at imagecopyresampled().
The easiest way is just to always fit the face/hair/beard in the same area of the image. Then just crop that area out.
If you must, you can store extra data for each image specifying a rectangle in the image that must be visible in the small avatar. Then take the maximum extremities of these rectangles in all the images you compose, and crop+shrink that down to your small avatar size.
However, be aware that resizing PNG images by a few pixels (e.g. 83x83 -> 80x80) can substantially reduce the quality, particularly for images with lots of defined edges. This is because there are many pixels in the new image that are [nearly] evenly split between 4 pixels from the original image, and in images with sharp edges this leads to blurring.
So, shrinking an image to fit a portrait is not just difficult but also reduces quality. I'd cut off the beard instead!
I may be oversimplifying this, but can you try:
Keep track of max face size dimensions pre-compositing.
Output the composite image to a temporary file.
Crop square of largest values from step 1
Resize cropped image portion to 80 x 80

Categories