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.
Related
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 …
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.
I've read many articles and discussions in forums and I still cann't figure out the difference between resizing a large image with php(not with html) and creating thumbnails. Except for the fact that the most people suggest phpthumb and imageMagick for thumbnails I don't understand why I should or not prefer to use these and not php functions like imagecreatefromgif(png,jpeg) and imagecopyresampled which can do this job(according to what I read.). What I'm trying to do is to create many galleries, so I want the images the user uploads to be resized in a smaller size if they are too large. Moreover, I need these images to be resized in smaller pictures that will be shown to gallery and when the user clicks on them they get their real size. So far, the user can upload an image that will be saved in a folder. I resize my images for galleries with html(which I know it's wrong) and I use fancybox for clicking and enlarging part with which I am satisfied. Please help understand the difference and give me the your advice what is the best thing to do. Thanks
Take this scenario.
I'm a highly equiped professional photographer and your photo application will store every image I take. I shoot in RAW so my images are massive. I also want to upload the original hi res JPEGs.
Now if you do not resize these images to thumbnails (by whatever mechanism you choose), if I try and view a particular album, I might have to load a 10x 20Mb images, only to have them resized down to 80x60pixels (and become totally crackling/noisy and extremely pixelated).
So what can you do?
Resize the images on your server, using something like ImageMagick as you suggest to generate multiple versions of this image.
This way you can deliver optimzed qualities for every occassion.
You should always intend to display an image in it's native resolution/dimension to avoid rastering or resizing noise.
And also, you want to save the different versions of the images.
imagecreatefromgif() will use the GD library by default.
Some reasons why one might prefer ImageMagick are detailed here and here.
That said, if you're happy with what you've got already and if there's no realistic chance of running into any GD dealbreakers later on, you certainly may reasonably decide to stay with what you have.
I am writing an PHP script to upload image files to the server and I have a few questions?
Is there a way to decrease the size of images in terms of kilobytes?
When I am using those files what is the best practice to embed those images into page? I mean do I always have to download the whole page?
Lastly, When resizing the pages(like 250X250 pixels) what is the pratice in order not to face the resolution problems?
I hope my questions are not too general. Thanks in advance!
Take a look at this http://www.webmotionuk.co.uk/php-jquery-image-upload-and-crop/ - There are many other free tools out there to convert images after the upload. The resolution shouldn't be an issue that is usually a problem when you give a size to an image object in html that is huge and not proportioned.
Here is another php resizer I have used this code before to do what you are requesting.
Is there a way to decrease the size of
images in terms of kilobytes?
You'll need to create a new image, based on the one you received.
You should first take a look at the PHP GD functions.
For instance: http://ch2.php.net/manual/en/function.imagecreatefromjpeg.php
When I am using those files what is
the best practice to embed those
images into page? I mean do I always
have to download the whole page?
What do you mean by embedding?
If you are talking about decreasing the size of the image before is uploaded, you can't with PHP, unless you use something like HTML5/Flash to pre-process the file before being sent.
What you want to do, after a user uploads that image, verify that the file is a real image, then using some library (if you use a framework, it probably has a way to resize images) resize the image to whatever max width or max height or max width and height you are wanting.
This will decrease the size of an image.
For the 2nd question, if I understand it, you are talking about what about when a user uploads a 500x500 and you want it to be 400x250, then you must scale and crop, this way the image is never stretched but a few pixels from the top or the bottom will probably be removed.
Imagine you have a classifieds website...
When searching ads you want image thumbnails of the "real" image which displays in its real size after clicking the ad...
Would it be faster to create the thumbnails per search, or create the thumbnails on and then just display them?
Storage is not a problem on my server...
Thanks
I would create the thumbnail once the original image has been uploaded, this way there's no slowdown when the person's page is first hit.
However, lately I've been using an image resize script from Shifting Pixel (http://shiftingpixel.com/2008/03/03/smart-image-resizer/). It creates a resized version on the fly, and caches it, so subsequent hits to the page will use the cached version. This could be useful if you don't want to create the thumbnail yourself.
Assuming that the images are of a fixed size, then it is better to create them upfront. Otherwise you have at minimum a per-request check for existence per image. If they are created ahead of time, we assume that the images exist.
Creating the thumbnail dynamically is your best bet, especially considering you'll probably reformat the search results at some point and will likely end up choosing a different size for the thumbnail. If you only have a thumbnail thats 150x150 pixels but after a redesign of the results page you want thumbnails to be a little larger, say 300x300, you'll have to regenerate all the thumbnails again. If you create them dynamically on request you can just alter your resize script to give you thumbnails that are 300x300.