Create image Thumbnails before or after upload the best way? - php

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.

Related

Dynamic image resize system

I need a image resize system for a project so I decided to upload original images into DB encoded with with base64, I would like to keep stored only original images since they are easy to manage(add/edit/delete).
The output is made dynamically with php and IMagick atm.
The main problem I met is the output slow time, specifically the processing time needed it too long because I use resize+compress.
I need this compression because my visitors have slow internet connection and sometimes images are really big for just a preview.
Alternative solutions I though is to store in DB some resized images but won't be eficient because it will take more space and images size will change over time.
So, my question is: is there a method to deliver images dynamically faster? How?
You won't get around caching the resized images in some way. The thumbnails don't need to be stored in the data base; you could also write them to disk. Only recompute the resized versions if the original image has changed.

php image loading time consumption

I have a products website where in I have around 100 images of high quality. Each image is around 6-7MB in size.
In my database I have stored the path of all the images along with their names. The images are saved in a folder /images/product_name/, But when I go to display these images in a web page, the page takes forever to load. All I do is send the id to the table, get the image paths and display it in the products page.
It would be very helpful if I could get any sort of advice on how to optimize the process.
The images you send to the client are most likely way too big. 7MB of size sounds very large for a product picture so 100*7MB = 700MB of data transfered if you display all the product images.
If you only need small images, scale them down to some KB's (thumbnails) and use those to display in your table.
NOTE: you can just preprend a prefix like "tmb_" or "tmb_200x200_" to the original filename, and you won't have to touch the paths in the database.
From reading your comments I think you are looking for some automated process to optimize the files and serve them at a more decent filesize.
You should take a look at imageMagick or the GD library which allows you to resize images (among many other things - http://php.net/manual/en/book.imagick.php) and optimize them. This can be combined with something like the YUI Image cropper to allow you to choose certain parts of the image to show in the thumbnail.
This is best done at the point of upload so the Server doesn't unnecessarily regenerate the images each time they are requested, and stored under a thumbnail column in the database.
If you must show bigger images I suggest you use a lightbox (see an example here - http://leandrovieira.com/projects/jquery/lightbox/) or similar technique that only loads the larger image when the client requests it.
Use http://luci.criosweb.ro/riot/ to compress images.
It is one of the simple and free tools recommended by Google to compress images.
Also as it was mentioned before make sure the thumbs you send to the listing pages are around 100px and only if the user clicks to zoom in show him the large ones one by one, and still even in the zoom option you should only display a 1000px size image max and that should not have more then 200-500k depending on format and quality.
Do you have optimized your images (in term of file size) ? Like on Smush.it for example http://www.smushit.com/ysmush.it/
You should really optimize your images. You can use a commercial product like Photoshop or use a free one like the GIMP to optimize the files.
Loading about 700 MBs of images will take exactly 1 day for me!!
reduce the size of the images to a thumbnail and when the user clicks on the thumbnail then load the original file. This can increase the performance.

understanding how timthumb works

Coming from Rails background, I used to work with paperclip plugin, which was creating from images attachments thumbs with predefined sizes.
In wordpress, I am little confused. Here is my question or points that need more clarification:
Does timthumb creates thumbs and saves it to disk upon uploading images for the first time? or It just resizes images on the fly and caches them?
If it resizes on the fly, why I see different sizes of each image in uploads directory, such as filename.jpg filename-150x150.jpg and so on?
Isn't better for performance to just create the thumbs once upon uploading and serving them directly without calling a script? and if so, how to implement this?
Typically it resizes and caches the images upon their first request.
You're probably seeing the image resizing that WP does. These are controlled in the settings
Debatable. Yes in that it could all be done at your command, no in that someone could upload 1000 images and resizing them all at once could cause problems with the site. Thus spreading out the resizing could result in balancing the load demands. Also, parameters can be passed via the script when a page loads that creates a custom thumbnail. So if you decide your thumbnails were 10px too narrow, you can run it again and it will resize from the original. Plus the filename of the base image remains untouched - if your code says image.jpg, it will always be image.jpg, not matter the size. So if you've got 10,000 instances of thumbnails, and they all reference image-150x150 and now you want them to be 160x160, you either have to change the image names being referenced, or have a nonsensical filename. TimThumb provides a pretty good workaround for that.
Here's some basic timthumb/wordpess performance tips http://www.dollarshower.com/timthumb-and-wordpress-blog-performance/

PHP image thumbnail performance?

I've recently started trying to increase my sites performance. One of the biggest obstacles I have are the number of thumbnails being displayed.
I currently use the full size image and scale it down by defining a height/width value in the img tag. This doesn't seem very efficient so my question is whats the recommended way to display thumbnails? Should I maintain a second table in the DB for thumbnails or is there a better solution?
Processing images takes its (cpu) toll, you should better avoid it where possible.
My advice:
Create the thumbnails while uploading the images into separate image files, this way you can determine when to create/resize them - and not during runtime.
If you want the links to the thumbnails in a separate database field or derive it from the original name, is entirely up to you - both ways work.
This makes additional performance boosters easier to implement too (p.e. caching).
I've implemented a similar process in a php based project, its a good way to scale out. In my case, I am creating the thumbnails nightly via cron, because system load is very low in that time.
If using an uploader to get the images on your website, have PHP resize the original image and upload both the original and a smaller thumb with some kind of prefix in the name.
This way you can easily get the images from your database and just use "filename.jpg" for your normal images and "thumb_filename.jpg" for your thumb.
Same can be done without an uploader of course but you'd have to manually create/upload the thumbnails.
For example create seperate folder in images call it thumnails (images/thumbnails) add there put files prefixed with file size for ex: "original_file_name_200X200.jpg", store on database "original_file_name" and extension "jpg" to seperate fields (name, ext) then when you need to display it select name add size prefix and add extension you get /images/thumbnails/file_200X200.jpg this way you can add later more sizes leaving original untouched.
You are looking for something like what can be found here: http://dtbaker.com.au/random-bits/php-simple-image-resize-thumbnail-generator.html (this turns all images into jpegs)
Like #martincarlin87 stated - Just need to add a check to see if it exists in the thumbnail directory and either send the information or create and send it through. This can be turned into a function as well.
I use phpTumb to create thumbnails on the fly.
You could also use it while uploading a picture to change its dimesions.
It has many other features. Check it out.
you could use a program like imagemagick to make proper thumbnails.
If its your personal site you could batch resize (theres a powertool for xp that does this) and then upload to a 200 directory and change your code. Obviously this relies on you uploading the images.
imagemagick will need to be installed on the server but will resize and allow you to play with the size of the images

A Question About Image Files

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.

Categories