I am working on a gallery concept for something I am building up, and I am trying to figure out what would be my best approach at taking an image file uploaded to the server, and then converting it 3 times to different sizes within a set ratio with a max height or width. Depending on which is greater.
So the upload class, and image manipulation class are pretty straight forward on a one to one basis, though I know you can loop through an array and process things over and over. However, the tricky part is the 3 different sizes.
Essentially I plan to take the uploaded file and store it in a different directory than uploaded to with a new name and all else we will call that dir "org". Then I want to take this original, keeping the file name consistant with the original after renaming it, and then resize the original within a certain ratio with a max size of either certain width or height pending on which is greater. Which the image class handles, now the part thats confusing me, is how do I do this 3 times one smaller then the next. I want a "lrg", "med", "sml" folder set. Would I build those into the same function some how or would I pass one upon completion to the next function and so on? If its all in the same function, then thats where I am confused to how I should handle this.
Also for reference I haven't tried much outside of the standard information found on the CI docs, as I am trying to think this through before getting deep into the code and finding out it wont work.
Maybe you could try timthumb insted of storing x3 elements.
this way you store Org. image and just in case you need specific size it process and store in cache folder for specific time in case you need same image again.
http://code.google.com/p/timthumb/
Related
I am working with a project. And there is feature user can upload image. That image will be used in different pages of website, with different sizes(eg: 200*300, 360*520, 700*1000).
I can create thumbnail two ways
while uploading image, create thumbnail with different size and store.
While displaying image src to some server side script, re-size image from there and print image, instead of displaying.
Which is the correct way to do? If I use 1st method, I think disk space will get full very fast, Is there any issue with 2nd method?
The advantage of method 1 is that you won't risk resizing the same image twice simultaneously, and that you can provide quickly a version to display to your user.
But why would your disk get full fast if you size the images beforehand? That would happen if you re-size them to every possible size, so that's not a good idea.
Method 2 is more flexible. You just ask for an image in a given size and your script will produce it on the fly. This is a bit slower, of course, but you could cache the resized image so visitors will get the images fast, unless they are the first one to request an image in a specific size.
My suggestion:
If you know which sizes you use on your website, you could use those sizes to resize the images in an early stage. You could even make a configuration on your website with a bunch of predefined image dimensions, which you can use on your website, and use those same configurations to scale the images when you upload them. This has some advantages:
Having a limited set of sizes will increase the chances of hitting the cache when visitors browse through your website. For instance, if you show a picture of X in the X detail page, and overview page includeing X, search results etcetera, and each of those pages uses a slightly different size, it is a waste of disk space and bandwidth.
And, if disk size is an issue, having a limited number of sizes also limits the disk space that the cached versions of these images consume.
Moreover, if you change or add a dimension, you could pregenerate all images for that size immediately, so visitors would benefit right away from the caches version.
And of course, it also makes it easier to purge the cache for images of a dimension that is no longer in the list, should you remove or change one.
If you choose this method, it makes it very easy to implement method 1 and pre-cache everything, but even if you would choose method 2 (if only as a fallback, should a cached version not exist), then still this would have benefits.
I'm attempting to utilize PHP to compare 2 separate images which may or may not contain the same image. Essentially, they are from an item database application and would have been saved over a year apart. The images are usually resized from roughly 600x600 to 300x300 and in one I checked by eye the image was the same but the file size varied by about 8kb.
Example,
Image created 1/25/2012 is 7,102 bytes
Image created 2/1/2014 is 7,094 bytes
Any suggestions would be greatly appreciated.
Here is a quick and easy way of comparing two images in PHP, based on this blog post.
Tweak the size of $new_img and whether to use grayscaling or not, and find that sweet spot that works on the data you have. When implementing this you should manually check the results a (preferably large) dataset to find out what works best for you.
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/
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
I'm currently dealing with a large number of images (over 3000), and I have two copies of each; one large, one small. The problem is I don't have any kind of link to say which small image maps to which large image.
I know roughly which large image goes to which small image (I have an array which contains up to 5 possibilities for each small image), and I'd like to loop round the array and compare each large image to the small image, to see if it's the same image but resized.
TLDR/Didn't Understand: Is there any easy way in PHP to compare two image that are the same, one being (say) 200x200 and the other being (say) 500x500, and determine if they're an image of the same thing?
The easiest way would be resize both to a small size (16x16 or 32x32 for example depending on how much CPU you want to use keeping track of the names of each file throughout the process.
Then use imagecolorat() to compare pixel colours through each row/column. You can then define a percentage of which need to match to be considered the same picture.
Edit: here's an example of this implemented http://www.thismayhem.com/php/comparing-images-with-php-gd/
Have you tried if resizing 1 of the big image creates the exact same image as one of the smaller ones(manually use Md5 to find that out).
If it does, then your solution is to batch resize the bigger images to a tmp file and md5 it building an array of bigimagemd5s and then you md5 the smaller ones.
This method will only work if the small images were generated using the php GD algorithm and you have the same compression level and size that was used before.
A thing you could do is, take the big picture, scale it to the size of the small picture and check some random pixel-areas there. If the pictures are really different, you could use something like a avg. colour of both images.
But this way is really performance sucking...
No, there is not a same_image_but_in_different_sizes() function that returns true or false.
If I were you, I'd either sort them manually or choose a color histogram based approach.
I just saw that all "images are JPGs, and they're saved in two folders; large and small". Why don't you just throw small away and recreate it, but this time you remember which belongs to which?
I would use the imagemagick library for resize and compare the images. I would call it using exec()
First, resize the large image using the convert command http://www.imagemagick.org/script/convert.php
Then, compare the small image with the resized image using compare command http://www.imagemagick.org/script/compare.php
to sort them manually will take you much, much less time.
Please check this link:
Compare 2 images in php
It seems that using ImageMagick extension is your best bet, although you'd probably have to scale down the larger image to the same size as your smaller one first.