Approach for storing image crops - php

We have a custom built CMS which allows for image uploads. In the CMS we have implemented
jCrop. When cropping an image (with GD in PHP), we are storing the original image name and crop image name in the database (MySQL), aswel as the original image and crop image on the server.
When we need an alternate crop, we use PHP to create another crop of the cropped image (and save it to the server). Because such an image has now been processed by GD twice, the result often looks bad.
A possible use case: in the CMS we manage persons. Each person can have an image. Since persons are usually shown in portrait mode, we let the user crop in portrait. On the website this works out fine, but on the mobile website, we actually need a square image. Hence we need two crops.
Lately we've been wondering how we could improve our crop workflow. Would an approach of only storing crop coordinates in the database work on the long term? What is a common approach of dealing with crops?
Thanks in advance!

I would use this approach:
Upload an image. Assign it an unique ID (i.e. an MD5 hash of the name).
Let the user crop it, store only the coordinates and image name in the db
Store the cropped image, give it a filename that is made for example out of the original file name plus the coordinates of the crop.
In this way you will be able to retrieve the cropped image only by knowing it's original name and the coordinates of the crop. In addition an exact duplicate of any crop would not be stored.
Example:
md5('image.jpg' . $crop->x0 . $crop->x1 . $crop->y0 . $crop->y1);

Would an approach of only storing crop coordinates in the database work on the long term?
Not if by that you mean you would also do the cropping each time the picture is requested. That puts unnecessary load on both your server (doing the actual cropping on-the-fly each time) and on the client as well (because there will be no caching of those dynamically generated crop images, unless you implement that on top of the whole thing).
Since persons are usually shown in portrait mode, we let the user crop in portrait. On the website this works out fine, but on the mobile website, we actually need a square image. Hence we need two crops.
Maybe a better approach would be what f.e. Facebook does in some places: Instead of cropping an image to be square server-side, the just have the client load the non-square version – and then let the client do the “cropping” by simply displaying the image as a positioned background image within a square element …

Related

Converting images from a rectangle to a square (prefer PHP)

I've been running a site where users can upload and crop photos of themselves and the enforced image shape was a rectangle.
Lately, I've been developing the new version of the site, and the image shape I'm now using is square.
I might have around 30,000+ images, and I want to move them from the old site to the new, but I need to convert them from rectangles to squares:
Is there a way to to do this conversion without losing parts of the image (eg: without cutting off part of it) and without distorting the image eg: it becomes squished and doesn't look like it should?
The images are of people's faces.
Any advice on a conversion like this? I'm good with PHP, so maybe I can script a job to do the entire conversion. I just need to be able to convert one image successfully.

Site needs Several Different Sizes of Each Photo + What is most efficient method to provide this

Users join a site I'm building and will update an image (or several).
The image (especially if main image - any any image can be this or changed to this) the image will be displayed in different sizes - total of 5 different sizes.
1 - full original image - lightbox image
2. - cropped large - main profile image
3. - cropped medium - search image
4. - cropped smaller - display of multiple users image
5. - cropped very small - chat image
All images will be changed to jpg and compressed to 80% and the user will do the cropping using Jcrop.
I wanted to know what would be the most efficient method to have these images.
My current plan is to save the original and post crop create 4 more images.
This means for each image there will be 5 images.
Is there a better way to do this?
Should I make the last 3 (3/4/5) all be the same image just sized differently with CSS?
I've heard about a PHP function to change the image size on the fly but I've also heard this is heavy on the processing and might be inefficient in its own way.
thoughts? advice?
thankyou
Your solutions sounds right. Store somewhere image resize settings, then upload image, create required images, crop, resize etc. Keep the original, so in case later you will need to add another size or make other changes.
You could only store original file name into database, like image.jpg, then before displaying add specific resized image prefix like: image_croped_large.jp.
As of choosing CSS i would resize images, won't use CSS , because image quality will be better, and image size will probably be smaller.
And it's cheaper to store few more images, than download big pictures.
Disc space is cheap and if you have a lot of people viewing those images, why do it on the fly?
Just have a naming convention.
Saves using the processor, it can do better and more productive things.

Retrieving a jpeg image over Facebook loses quality drastically.

I've been struggling to an issue of saving an image into jpeg which loses quality. As a project for my degree, I'm making an app where i upload the images on facebook.
I have a nice colored image, for which i shuffle the pixel values to get a mapped image. Then I upload it onto the facebook. When, i download and move the pixels back to original image, it loses the quality too much.
Below are the original, mapped and retrieved images:
I've tried many strategies like using png files, which works. But, currently, whatever the image is, facebook converts it to jpeg. So, if i upload maskedimage.png to facebook, it converts it to .jpg and loses some of the pixel-correlation values, which doesnt end up giving me the right image to retrieve original.
I'm using PHP and facebook-php sdk.
Please suggest.
Thank you
-Kaur
You're stuck. JPEG is a lossy algorithm which depends on human vision characteristics to mask the inaccuracies. By scrambling the pixels you've changed the nature of the loss so that it's not invisible anymore.
This isn't primarily about JPEG being lossy; it's about Facebook forcing image recompression, even if you check "High quality" when uploading.
The best you can do is to double the source image resolution, and for solid colors, try to introduce noise. Details in this GD.SE question
Note that the trick of uploading images <100kb has no longer been working, since at least June 2012.

PHP - Crop an image, and center the resulting image onto a background, using GD

I'm trying to make a simple app which gives a user a standard background/template image onto which they can place their logo/brand/whatever. I've had a look at a few jQuery plugins which allows the user to upload and crop an image, and most of them seem to work by posting the x and y coords, and the width and height, to the server.
I'm able to then use imagecopyresampled() to merge the image with the background image, but (a) the user's image seems to end up really skewed, even when I hard-code dimensions that I know should work, and (b) I need to position the uploaded image pretty much dead-center -- what's the best way to achieve this?
Any help? This is my first time using GD. I'll add my own code if required, but I'm messing with the different GD functions, so I've butchered the one I had before. Will reproduce it.
Thanks

Image processing class in PHP

I need to build a PHP photo processing class, I know there are MANY that already exist to the public but I need to build one to do just what I need done and nothing extra and nothing less.
I need my class to do this...
1)
I create a new instance of my class and I pass in either a URL of a photo, or the path to a local photo being uploaded using POST form.
2)
I then need to take the main image and check it's dimensions, if it is wider the 800 pixels, I need to resize it down, if it is not wider then 800 then I just leave it
3)
We now need to build 2 different sized thumbnails from this image, if we resized the image to meet our 800 pixel requirement then we use that image to make the thumbnails from, otherwise we make our thumbnails from the original image.
4)
We then update a few a database records
Then completed.
I know this is not that difficult but I need to build this with best performance in mind, for example if a user uploads a 2mb photo, I don't want to hog up memory and keep building thumbnails from that photo if we already madea smaller image I think it should then use the smaller image to build out thumbnails from.
With all this information now, do you have any suggestions on how to do this in GD or imagemagick. If I make a method that does the thumbnails, how should I make sure it keeps using the smaller images to make other smaller images? I have looked at some existing image classes and they all are very complex and over done IMO however none do exactly this simple task.
The PHP GD library uses a resource object to represent images. You can use imagecopyresampled or imagecopyresized to resize the image.
If it has been resized, use the new resized image object for future operations, otherwise use the original resource object.

Categories