PDA

View Full Version : UNSOLVED: Dynamically changing colors and resizing image



techno_race
08-14-2008, 03:32 AM
Well, here's my problem. I have a GIF image that has black and white.
I need to be able to use $_GET['black'] and $_GET['white'] (or something like that) to get two user-submitted hex triplets (with leading #).
Then replace all black in the image with $_GET['black'] and all white with $_GET['white'].
Then I have to resize the image to predefined (larger) dimensions using a bicubic method (as to create a gradient).
Finally, I have to send the finished image to the user as a GIF.
Then I have to repeat the process for multiple users (at once?).

I know I'll probably have to install some sort of image library, like ImageMagick or GD.
I don't know how to do this. I have very little experience with PHP, let alone PHP extensions.
Is there any way to do this on a remote server? If so, how? If not, now what?

james438
08-14-2008, 08:20 AM
Do you have GD or Imagemagik? What I mean is are you sure you do not have these? I have recently started learning about php's ability to process images. It is interesting and rather fun.

To see if you have GD try running this snippet of code:
<pre><?php
var_dump(gd_info());
?></pre>Other than that all I really know how to do thus far is use php to generate a high quality thumbnail that loads really fast without creating a new image file and use php to create a file that is a thumbnail version of an image which is also in really high quality depending on whether you want to create a new image file or just view what it would like to help decide the format of the file to be created.

Are you saying that $_GET['black'] could be any color depending on what the user submits?

techno_race
08-14-2008, 04:51 PM
It turns out I have GD...I think...

array(12) { ["GD Version"]=> string(27) "bundled (2.0.34 compatible)" ["FreeType Support"]=> bool(true) ["FreeType Linkage"]=> string(13) "with freetype" ["T1Lib Support"]=> bool(false) ["GIF Read Support"]=> bool(true) ["GIF Create Support"]=> bool(true) ["JPG Support"]=> bool(true) ["PNG Support"]=> bool(true) ["WBMP Support"]=> bool(true) ["XPM Support"]=> bool(true) ["XBM Support"]=> bool(true) ["JIS-mapped Japanese Font Support"]=> bool(false) }
:confused:
And I think the hosting feature list mentioned ImageMagick...
:moreconfused:


Are you saying that $_GET['black'] could be any color depending on what the user submits?
Yes, any hex triplet.

So, the user goes to, say, image.php?black=#09F673&white=#9FD33E
I have a predefined image, say, image.gif
It replaces all #000000 in image.gif with #09F673
It replaces all #FFFFFF in image.gif with #9FD33E
It saves the file as, say, image0548394748.gif (PIN)
It bicubically (?) resizes image0548394748.gif to predefined dimensions, preferably using variables, as I have to do this for different images at different sizes.
It resaves image0548394748.gif
It gives image0548394748.gif to the user
After, say, 5 minutes, it deletes image0548394748.gif
It repeats this for diferent users, possibly simultaneously.

techno_race
08-15-2008, 12:33 AM
I have GD 2.0.34, but not ImageMagick.

james438
08-15-2008, 03:14 AM
Hi,

Still working on it. I have only very very recently started looking at the GD functions of PHP, so my skills are certainly lacking. However here are a few scripts I have discovered.

Resizes the image and outputs it directly to a web browser witout saving the file anywhere.
<?php
// The file
$filename = 'images/picture.jpg';
$percent = 0.25;

// Content type
header('Content-type: image/jpeg');

// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;

// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// Output
imagejpeg($image_p, null, 100);
?>

Add the following at the end of the script to save the resized image to a file on your server. The original file is not destroyed.
$dest="try1.jpg";
imagepng($image_p,$dest);

There are a great deal of terms that I am still unfamiliar with, but the following will replace one color with another in a created image. This is not exactly what we want, but at least we are getting closer. It can be applied to an image, but I do not know how to do that yet.

<?php
header("Content-type: image/png");
$im = imagecreate(200, 200);
$red = imagecolorallocate($im, 255, 0, 0);
$offblue = imagecolorallocate($im, 90, 90, 200);
imagefill($im, 0, 0, $red);
imagefilledrectangle($im, 10, 10, 40, 40, $offblue);
// above could come from an uploaded image
// find a blue in the image
$newblue = imagecolorclosest($im, 0, 0, 255);
// change it to green
imagecolorset($im, $newblue, 0, 255, 0);
imagepng($im);
imagedestroy($im);
?php>
I got the above code from http://www.phpdig.net/ref/rn25re393.html

james438
08-15-2008, 05:08 AM
frustrating, because I can't get this or anything to work on any image I try to manipulate. I have tried converting the image to grayscale and then replacing the black, I have tried a loop to replace a range of colors, I have tried imagecolorset, imagecolorallocate, and just can't get anything to work. I can resize the image, mess with the tint, and mess with the image as a whole to invert the colors using imagecolorfilter, but I can't change one color to another.

hmmm...

P.S. I did find this article to read for later: http://www.dushu.de/englishbook/sams.php.5.unleashed/source/067232511x/ch27lev1sec3.html

techno_race
08-15-2008, 06:57 PM
So, here's what's left:
- Generating a PIN (text file containing number that updates automatically?)
- changing the colors.
:):):):):)

techno_race
08-15-2008, 07:11 PM
When resizing the image, is it bicubic, bilinear, or neighbor?

james438
08-15-2008, 10:47 PM
So, here's what's left:
- Generating a PIN (text file containing number that updates automatically?)
- changing the colors.
I don't know too much about PINs. I never had the need for them, but why not store the PIN in the database using MySQL and store the time that it was generated and have it auto-update based on the time it was submitted. For example exactly 10 minutes after the PIN is generated the PIN will reset.

Yeah, changing colors is a bugger. Remember I have only started looking at GD for the first time 2 or 3 days ago. Can you restate how you want it to change the colors, because If I were to change one color in a b&w image to another color the color would indeed change, but since there are so many different shades of gray only a few pixels in the image would change.

When resizing the image, is it bicubic, bilinear, or neighbor?
Kinda hard to find the info on that one. I went to php.net and the best I could find was that it was not known which algorithm PHP's GD uses. I am not the best judge of image quality, but I found the quality to be quite good. I hear photoshop is better though.

If you can figure out the color change problem before me please post the answer here. I am eager to see how it is done! You probably already know this, but the image processing capabilities of GD is quite processor heavy.

techno_race
08-15-2008, 11:34 PM
I have an extremely small image.
One pixel is black, the other one is white.
I need to change black to x and white to y.
The user submits the new colors.
What about (for the PIN) something like:
YYYYMMDDMinuteSecondMillisecond
It would be a freak coincidence if those were exactly the same.
Why don't we just do:


<?php
header("Content-type: image/png");
$im = imagecreate(200, 200);
$red = imagecolorallocate($im, 255, 0, 0);
$offblue = imagecolorallocate($im, 90, 90, 200);
imagefill($im, 0, 0, $red);
imagefilledrectangle($im, 10, 10, 40, 40, $offblue);
// above could come from an uploaded image
// find a blue in the image
$newblue = imagecolorclosest($im, 0, 0, 255);
// change it to green
imagecolorset($im, $newblue, 0, 255, 0);
imagepng($im);
imagedestroy($im);
?php>
twice, once for each color?

james438
08-16-2008, 04:07 AM
Without looking at it too closely it should work. I thought you were talking about altering a photograph or something like that.

techno_race
08-25-2008, 04:45 PM
BUMP
--------
I do, indeed, have ImageMagick, if that's of any use.
--------
I still need help. I know next-to-no PHP.

james438
08-26-2008, 04:57 AM
Sorry, thought that your problem was solved. I'm afraid I have told you about all I know about the GD aspect of php. I am hoping someone else who is more familiar with it can help out from here. Can you post a link to the image you are talking about so I can see what you are working with?

Out of curiosity what is the area of web design that you are most skilled at if it is not php?

techno_race
08-28-2008, 04:32 AM
Can you post a link to the image you are talking about so I can see what you are working with?

Eh... http://www.seangillen.com/Untitled-1.gif :p


Out of curiosity what is the area of web design that you are most skilled at if it is not php?
Like, languages?
I guess HTML; it's the only thing I've ever successfully written without using references or copy-and-pasting code... :)

techno_race
09-14-2008, 03:25 PM
bump.

james438
09-14-2008, 06:27 PM
lazy reply on my part, but try playing around with some of the scripts from my website. In it there is a page titled PHP -- Image Processing (http://www.animeviews.com/article.php?ID=51&table=programming) where I discuss what I have learned so far with GD. I am still expanding it and scanning for errors, but I think a lot of what you are looking for is listed there and there are a lot of sample scripts that you can try and adapt for your own purposes.

If you do find something useful there please post it here so others can see the solution as well. I am still learning, but I am about ready to start learning about some other aspects of PHP such as php.ini files or more on PCRE syntax.

EDIT: I had to sleep. Long day/night at work. Anyway, look at the following:

<?php
header('Content-type: image/jpeg');
$filename = 'http://www.animeviews.com/images/screenshots/cowboybebop27.JPG';
$im = imagecreatefromjpeg($filename);
imagetruecolortopalette($im, false, 255);
$ig = imagecolorat($im, 20, 20);
imagecolorset($im, $ig, 0, 0, 255);
imagejpeg($im, null, 100);
?>

In your case the code would look something like the following since you are working with a gif image.

<?php
header('Content-type: image/gif');
$filename = 'http://www.seangillen.com/Untitled-1.gif';
$im = imagecreatefromgif($filename);
imagetruecolortopalette($im, false, 255);
$ig = imagecolorat($im, 0, 0);
imagecolorset($im, $ig, 0, 99, 0);
imagegif($im, null, 100);
?>

Look at the other sample scripts too and let me know where you get stuck. I'll help if I can.