Log in

View Full Version : Create solid color image



corbo950
04-28-2010, 12:15 AM
is there a way using JS, Flash or PHP to generate a jpeg of on color on demand? i need to make like a 500 X 500 solid jpeg of one solid color biased on a color code and then save it so it can be used.... is this possible?

djr33
04-28-2010, 12:24 AM
Very difficult in flash to actually save the image. Of course you could make a color object that displays within flash, but if you want to save it that'll become difficult.
Javascript can't do much of anything here (though it could obviously generate a square object with a background color on the page, but not an image).

PHP can do this using an image function library. Included with most installs of PHP is the gd library. It's documented (if badly) on php.net. It's processor intensive and hard to work with. Basically it's no fun but if you have to make images it can get the job done. It'll crash your server if you overuse it, but creating one-color images and saving them shouldn't be too bad. I wouldn't recommend doing that every page load though. Caching is better.
Slightly better is image_magick, another image functions library but it's not included so you'd have to add it. It's got most of the same problems but works a little differently. Some people like it more.

BLiZZaRD
04-28-2010, 12:32 AM
I would do it in Flash. You have to adjust your Classes a bit, but in AS3.0 it isn't hard. I was going to type it all out for you then I remembered about Google, and found someone that already typed it :D I am way too lazy today...

Link to the post cause I am too lazy to type my own version for you (http://actionscript-blog.imaginationdev.com/5/save-jpg-jpeg-png-bmp-image-action-script-3/)

corbo950
04-28-2010, 05:06 AM
cool thanks looks like what i needed... and thanks for the warning about server load djr33 the good news it is should only ever be done once.... i need to fill in somebody elses image to mask something so when the user of my project sets things up they will set a background color and the image will be generated to be the same color... but once they have finished and installed it on there sever and it has generated it the first time it should just sit there forever... just didnt want to tell people using my project that they had to open up Photoshop and make one by hand for their chosen background color

djr33
04-28-2010, 05:30 AM
Yeah, using the gd library (and other methods) in PHP is just fine as long as you use it in moderation. For example, I crashed a server once using a pixel-by-pixel image generator (creating patterns of colors) at wallpaper resolution (1024x768, I think). It can also overwhelm a server even for simple images if you have a lot of people accessing it at once.
As a general rule I think it's fine to do whatever you want with images 300x300 or smaller. With anything bigger, think about it carefully and as long as you're keeping the processes simple and using only the default functions (rather than doing complex pixel-by-pixel stuff). If you go over 1000x1000, REALLY think about whether it's worth it.
Regardless, it's probably fine to go up to 1500x1500 if you are only doing it once and on a server that isn't otherwise busy BUT keep an eye on usage levels once you get that high.

If this is too much info, don't worry about it. Flash may be your answer. But PHP can do this; it's just annoying at times.
By the way, frequently the maximum script processing time (30 seconds on many servers) will stop everything before the server gets overloaded. Once you turn that off or extend it, that might get to be a problem.

Also, I've created .exe's from PHP and they get very slow, but then on my personal computer they don't crash anything (and any of my computers, including the much older ones). By "crash" above I meant that it hit some server limits on my account, though it was just a warning.

The main trick with GD is to know how to limit its use and also to only generate images once-- for example, you can use it to create thumbnails even from huge images but just do it once and cache the thumbnails after they're created once. Then you don't need to do it again, etc.

My main objections to GD are that it's slow (and server intensive-- remember that while it's being "slow" the server is running at full power) and that it's horribly frustrating to work with :p
...but it's often useful too. So I still use it when I have to.