Go Back   SitePoint Forums > Forum Index > Program Your Site > PHP
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Sep 20, 2006, 15:07   #1
vidar.ostevik@ly
SitePoint Member
 
Join Date: Sep 2006
Posts: 8
Problems with GD?

Hi!
Im having a problem with some code used to let visitors take a snapshot with their webcam and post it to a imagegallery. This uses Flash's Camera API and flash.display.BitmapData Class with server side PHP to take snapshots from a webcam and output them as PNG files.
The problem is that some visitors have dark settings on their camera, and in the dark places in the image, the color becomes purple (so it looks like a negative of the picture). Do you see anything in the code beneath, that could cause this?
Is there any simple solution to try ImagMagic instead of GD? (It is installed on my webserver).
If not, is it possible to let the visitor see the image (like he does now), and put up a button labeled "Save", and let the user save the image ONLY if he pushes the "Save" button?

Here is the code:
PHP Code:

<?


    
/*
    ///////////////////////////////////////////////
                
                EXAMPLE: Webcam Snapshot
                AUTHOR: Robert M. Hall
                CREATED: September 1st, 2005
                MODIFIED: September 6th, 2005
                
                ASBE == Great Software Examples
                http://www.ifbin.com/asbe
                
    ///////////////////////////////////////////////
    */
    
    /*
        FUNCTIONALITY:
        Utilize the new bitmap features, draw method of movieclip and camera API and a
        server side PHP script to take individual snapshots from the locally attached webcam
        and generate standalone JPEGs
        
        USAGE:
        Great for kiosk style applications to allow sending of email postcards with image snapshots
        without requiring Flash Communications server or other expensive software
        
    */
    
    
    // convert the six digit hex values for our pixel color data into the individual
    // Red,Green,Blue values to allow us to re-create our image data
    
function hex2int( $hex ) {
            return array(
'r' => hexdec( substr( $hex , 0 , 2 ) ), // 1st pair of digits
                          
'g' => hexdec( substr( $hex , 2 , 2 ) ), // 2nd pair
                          
'b' => hexdec( substr( $hex , 4 , 2 ) )  // 3rd pair
                        
);
    }

    
// Split apart the POST data on commas and put it into an array
    
$img_arr=explode( "," , $_POST["pixels_arr"] );

    
// Check to make sure we received the image width from the camera
    // if not set it to default value
    
if( !isset( $_POST["pixels_width"] ) ) {
        
$img_width=319;
    } else {
        
$img_width=$_POST["pixels_width"];
    }
    
    
// Check to make sure we received the image height from the camera
    // if not set it to default value
    
if( !isset( $_POST["pixels_height"] ) ) {
        
$img_height=239;
    } else {
        
$img_height=$_POST["pixels_height"];
    }

    
// Output our header data to indicate to the browser we are returning a JPEG image
    
header( "Content-type: image/png" );

    
// Create our JPEG object (requires GD library 2.0 in your PHP installation)
    
$image=imagecreatetruecolor( $img_width ,$img_height );
    
    
// Set our default background color - it will be overwritten but needs to be set first
    
$background = imagecolorallocate( $image , 0 , 0 , 0 );
   
    
// Temporary counter value for walking through our pixel array
    
$cnt=0;

    
// Set up our for loop to parse through the array we received in POST data from Flash with
    // all our pixel color information
    
for ( $xx=0 ; $xx < $img_width ; $xx++ ) {
    
        for (
$yy=0 ; $yy < $img_height ; $yy++ ) {  
        
            
// Get the RGB color values from hex data
            
$color_arr = hex2int( $img_arr[$cnt] );
        
            
// assign the new color temporarily to allow us to color our next pixel
            
$newcolor = imageColorAllocate( $image , $color_arr['r'] , $color_arr['g'] , $color_arr['b'] );
           
               
// set our pixel at xx,yy ith newcolor
               
imagesetpixel ( $image , $xx , $yy , $newcolor );
           
               
// increase our temp counter for walking through the pixel array
               
$cnt++;

       }
    }
    
    
// create our JPEG image from the image data
    // (This could also be used to output to a local file for later usage or storage
    
$random = mt_rand(0,1000);
    
$date = date('dmYHis');
    
//ImageJPEG($image,  'snapshots/'.$date.$random.'.jpg'); //Modifisert 20.09.06 VØ
    //ImageJPEG($image,  'snapshots/'.date('dmYHis').mt_rand(0,1000).'.jpg');
    //Outputs the image to browser
    //ImageJPEG($image);
    
$filename = '../media/snapshots/'.$date.$random.'.png';
    
ImagePNG($image, $filename, 100);
    echo
"<img src=\"$filename\" alt=\"Her er bildet du tok!\" />";
    echo
"<br />Her er bildet du tok!!";

    
    
// Destroy/release memory used to create our JPEG
    
imagedestroy( $image );

    
// Thats all folks!
?>
Regards Vidar
vidar.ostevik@ly is offline   Reply With Quote
Old Sep 21, 2006, 02:39   #2
Forbes
It's been real...
 
Forbes's Avatar
 
Join Date: Dec 2004
Location: Yorkshire, England
Posts: 537
Hi Vidar!

I'm not too surprised that no one has taken this on, GD stuff is damned awkward at the best of times.

Assuming that you understand all of the above and you haven't just hacked this together from other sources, the best thing for you to do is to break this whole thing down into small segments and work through it one stage at a time.

That's what I do. That way, you'll be able to isolate the source of the problem and then figure your solution from there.

Hope that helps?
Forbes is offline   Reply With Quote
Old Sep 21, 2006, 06:18   #3
vidar.ostevik@ly
SitePoint Member
 
Join Date: Sep 2006
Posts: 8
Hi Forbes!

I'm sorry to disapoint you, but I do not understand everything in this script, it was premade. The only thing I have modified, is the file type, and that it saves the image instead of just outputing it to the explorer (as it would if you just use
imagePNG($image)).

So, I'm quite lost here

Regards Vidar
vidar.ostevik@ly is offline   Reply With Quote
Old Sep 21, 2006, 06:21   #4
Forbes
It's been real...
 
Forbes's Avatar
 
Join Date: Dec 2004
Location: Yorkshire, England
Posts: 537
Have you not tried getting in contact with the guy(s) that wrote the script?

That'd be my first port of call.

Hope that helps?
Forbes is offline   Reply With Quote
Old Sep 21, 2006, 06:55   #5
kgun
SitePoint Addict
 
Join Date: Nov 2005
Location: Moss, Norway.
Posts: 280
Quote:
Originally Posted by vidar.ostevik@ly
Hi!
Im having a problem with some code used to let visitors take a snapshot with their webcam and post it to a imagegallery. This uses Flash's Camera API and flash.display.BitmapData Class with server side PHP to take snapshots from a webcam and output them as PNG files.
The problem is that some visitors have dark settings on their camera, and in the dark places in the image, the color becomes purple (so it looks like a negative of the picture). Do you see anything in the code beneath, that could cause this?
Is there any simple solution to try ImagMagic instead of GD? (It is installed on my webserver).
If not, is it possible to let the visitor see the image (like he does now), and put up a button labeled "Save", and let the user save the image ONLY if he pushes the "Save" button?
1. I cut and pasted that code into dreamweaver and the code in itself seems OK. The only place I can see problems is in the hex2int function, the imagecreatetruecolor function, the other image related functions and in the for loop.

I see potential problems here:

flash.display.BitmapData Class Do not know the class, but do you have control of what it does?

Output them as PNG files. Have you tried other formats?

You write: "The problem is that some visitors have dark settings on their camera, and in the dark places in the image, the color becomes purple (so it looks like a negative of the picture)".

Are you sure that the error is in the code? What about the libraries?

You write:
"Is there any simple solution to try ImagMagic instead of GD? (It is installed on my webserver)".

May be I do not know them. Did you mean ImageMagick or ImagMagick?

Natural KW search on Google:

"PHP GD problem with PNG"

or variations thereof.

You are aware of this page http://no2.php.net/gd that says:

Requirements
If you have the GD library (available at http://www.boutell.com/gd/ ) you will also be able to create and manipulate images.

Last edited by kgun; Sep 21, 2006 at 07:31.
kgun is offline   Reply With Quote
Old Sep 21, 2006, 11:48   #6
vidar.ostevik@ly
SitePoint Member
 
Join Date: Sep 2006
Posts: 8
Hi Kjell Gunnar!

I will try to answer your questions breefly.

The flash.display.BitmapData class passes the image as a HEX file, therefore the hex2int function.

I have tried to output them as jpg and GIF, with the same problem.

I don't really know if it is problems in the librarys (I'm guessing you mean Flash's libraries?), but it doesn't semm like that is the problem, cause after you have taken the snapshot, a image of that is shown to the right (in the flash aplication, the "Live" camera image is on the left, and the snapshot appears to the right), and this looks just fine. It is when the PHP script returns the image after convering it to PNG, the problem appears.

Regarding ImageMagick, I ment http://www.imagemagick.org/

I have tried googeling (before posting to the forum), and I still can't figure out the reason...

And I have GD 2.0.33 installed on the webserver.

I appreciate all the answers here. Thankyou very much!

Regards Vidar
vidar.ostevik@ly is offline   Reply With Quote
Old Sep 21, 2006, 12:37   #7
kgun
SitePoint Addict
 
Join Date: Nov 2005
Location: Moss, Norway.
Posts: 280
Quote:
Originally Posted by vidar.ostevik@ly
It is when the PHP script returns the image after convering it to PNG, the problem appears.
Then you have restricted the problem very much.

You must then

1. Search for a better function or class or

2. Locate the part of the code that causes the problem.

Have you asked on the other two large PHP forums?

"http://phpbuilder.com/board/

http://forums.devshed.com/

If you are from Norway, somebody at the http://phpbb.no/ may also help you since I think there are som clever PHP guys there.

It is difficult for me to help you more since I do not have the full code. It may be a colour / bitmap function, class that causes the problem.

This site http://www.tufat.com/ also has a forum that you may try. As of this writing I get the following message:

"The forum is temporarily down for maintenance".

The site has a FlashChat.

When locating errors in code I use the following technique:

1. Echo "I am here."
2. Output intermediate results.
3. In a nutshell, test the output of each transformation / mapping in each step.
kgun is offline   Reply With Quote
Old Sep 21, 2006, 15:06   #8
vidar.ostevik@ly
SitePoint Member
 
Join Date: Sep 2006
Posts: 8
Hi Kjell Gunnar!

Thank you very much for your reply!
I will take a look at the other forums, and post a message there!

Regards "the Viking" Vidar
vidar.ostevik@ly is offline   Reply With Quote
Old Sep 21, 2006, 18:36   #9
kgun
SitePoint Addict
 
Join Date: Nov 2005
Location: Moss, Norway.
Posts: 280
The Norwegian company RefsnesData owns W3 Schools that I assume you know since it is one of the best known resources for webmasters. It has more than 30 million visitors / month.

Their forum may be the natural starting point.

How did I forget that forum?

A final word about technique:

I do no know so much about image manipulation since it is not my business, but a natural KW search is:

(Digital) Image Processing

The expert groups are here: DSP Related Discussion Groups

Here is a collection of image filters written in C++ that can be downloaded and perhaps translated to PHP.

May be you should use a form of inverted filter, since the colour gets purple on the dark places.

Then a natural search (class browsing see OopSchool in my signature for resources) becomes something like

InvertedColo(u)r

filter / method / function / class

in PHP

Last edited by kgun; Sep 21, 2006 at 19:30.
kgun is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Sponsored Links
 
Forum Jump


All times are GMT -7. The time now is 14:46.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved