Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: UNSOLVED: Dynamically changing colors and resizing image

  1. #1
    Join Date
    Feb 2007
    Location
    🌎
    Posts
    528
    Thanks
    10
    Thanked 10 Times in 10 Posts
    Blog Entries
    2

    Question UNSOLVED: Dynamically changing colors and resizing image

    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?
    Last edited by techno_race; 08-15-2008 at 07:12 PM.
    ....(o_ Penguins
    .---/(o_- techno_racing
    +(---//\-' in
    .+(_)--(_)' The McMurdo 500

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    1,702
    Thanks
    82
    Thanked 90 Times in 88 Posts

    Default

    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:
    PHP 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?
    Last edited by james438; 08-14-2008 at 08:28 AM.

  3. The Following User Says Thank You to james438 For This Useful Post:

    techno_race (08-14-2008)

  4. #3
    Join Date
    Feb 2007
    Location
    🌎
    Posts
    528
    Thanks
    10
    Thanked 10 Times in 10 Posts
    Blog Entries
    2

    Default

    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) }

    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.
    ....(o_ Penguins
    .---/(o_- techno_racing
    +(---//\-' in
    .+(_)--(_)' The McMurdo 500

  5. #4
    Join Date
    Feb 2007
    Location
    🌎
    Posts
    528
    Thanks
    10
    Thanked 10 Times in 10 Posts
    Blog Entries
    2

    Default

    I have GD 2.0.34, but not ImageMagick.
    ....(o_ Penguins
    .---/(o_- techno_racing
    +(---//\-' in
    .+(_)--(_)' The McMurdo 500

  6. #5
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    1,702
    Thanks
    82
    Thanked 90 Times in 88 Posts

    Default

    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 Code:
    <?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$image0000$new_width$new_height$width$height);

    // Output
    imagejpeg($image_pnull100);
    ?>
    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.
    PHP Code:
    $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.
    Code:
    <?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
    Last edited by james438; 08-15-2008 at 03:57 AM.

  7. The Following User Says Thank You to james438 For This Useful Post:

    techno_race (08-15-2008)

  8. #6
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    1,702
    Thanks
    82
    Thanked 90 Times in 88 Posts

    Default

    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...7lev1sec3.html

  9. #7
    Join Date
    Feb 2007
    Location
    🌎
    Posts
    528
    Thanks
    10
    Thanked 10 Times in 10 Posts
    Blog Entries
    2

    Default

    So, here's what's left:
    - Generating a PIN (text file containing number that updates automatically?)
    - changing the colors.
    ....(o_ Penguins
    .---/(o_- techno_racing
    +(---//\-' in
    .+(_)--(_)' The McMurdo 500

  10. #8
    Join Date
    Feb 2007
    Location
    🌎
    Posts
    528
    Thanks
    10
    Thanked 10 Times in 10 Posts
    Blog Entries
    2

    Default

    When resizing the image, is it bicubic, bilinear, or neighbor?
    ....(o_ Penguins
    .---/(o_- techno_racing
    +(---//\-' in
    .+(_)--(_)' The McMurdo 500

  11. #9
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    1,702
    Thanks
    82
    Thanked 90 Times in 88 Posts

    Default

    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.
    Last edited by james438; 08-16-2008 at 04:06 AM.

  12. #10
    Join Date
    Feb 2007
    Location
    🌎
    Posts
    528
    Thanks
    10
    Thanked 10 Times in 10 Posts
    Blog Entries
    2

    Default

    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 Code:
    <?php
    header
    ("Content-type: image/png");
    $im imagecreate(200200);
    $red imagecolorallocate($im25500);
    $offblue imagecolorallocate($im9090200);
    imagefill($im00$red);
    imagefilledrectangle($im10104040$offblue);
    // above could come from an uploaded image
    // find a blue in the image
    $newblue imagecolorclosest($im00255);
    // change it to green
    imagecolorset($im$newblue02550);
    imagepng($im);
    imagedestroy($im);
    ?
    php>
    twice, once for each color?
    ....(o_ Penguins
    .---/(o_- techno_racing
    +(---//\-' in
    .+(_)--(_)' The McMurdo 500

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •