Page 1 of 1

Create new image

Posted: Sun Feb 12, 2006 6:00 pm
by bimo
feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


I have been having problems getting the imageJpeg() function to work and am not sure what's wrong.  I've done a lot of research and (think that I) have (probably) ruled out character set and permissions conflicts by setting permissions to 777 and saving the files as western char. set.  

However, What I am trying to do is use the gd functions to create thumbnails and then save as a new jpg.  In other words, each time the create flv file runs (I'm using flash communications sserver in a separate but complementary ap) or plays for the first time, I want it to create a new jpeg from the flv.  

I am basing (copying) my script off of Jorge Solis' and Sephiroth's tutorials and some comments I found at the php.net imagejpeg function and can't figure it out.  Here's what it's doing (the front end was taken directly from J.Sollis' site (address un-remembered ) - my site is http://www.ahsodes.com/vidbb/flash/video/snapshot.html.  

my [their] code:

Code: Select all

<?php

error_reporting(1);
/**
 * Get the width and height of the destination image
 * from the POST variables and convert them into
 * integer values
 */
$w = (int)$_POST['width'];
$h = (int)$_POST['height'];

// create the image with desired width and height

$img = imagecreatetruecolor($w, $h);

// now fill the image with blank color
// do you remember i wont pass the 0xFFFFFF pixels
// from flash?
imagefill($img, 0, 0, 0xFFFFFF);

$rows = 0;
$cols = 0;

// now process every POST variable which
// contains a pixel color
for($rows = 0; $rows < $h; $rows++){
    // convert the string into an array of n elements
    $c_row = explode(",", $_POST['px' . $rows]);
    for($cols = 0; $cols < $w; $cols++){
        // get the single pixel color value
        $value = $c_row[$cols];
        // if value is not empty (empty values are the blank pixels)
        if($value != ""){
            // get the hexadecimal string (must be 6 chars length)
            // so add the missing chars if needed
            $hex = $value;
            while(strlen($hex) < 6){
                $hex = "0" . $hex;
            }
            // convert value from HEX to RGB
            $r = hexdec(substr($hex, 0, 2));
            $g = hexdec(substr($hex, 2, 2));
            $b = hexdec(substr($hex, 4, 2));
            // allocate the new color
            // N.B. teorically if a color was already allocated
            // we dont need to allocate another time
            // but this is only an example
            $test = imagecolorallocate($img, $r, $g, $b);
            // and paste that color into the image
            // at the correct position
            imagesetpixel($img, $cols, $rows, $test);
        }
    }
}

// print out the correct header to the browser
header("Content-type:image/jpeg; charset=Western ISO-1");
// display the image
imagejpeg($img, "http://www.ahsodes.com/flash/video/mypic.jpg", 90);
imagedestroy( $image );
print("POST: ");
print_r($_POST);
?>

I'd appreciate any help,

b


feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Sun Feb 12, 2006 6:59 pm
by josh

Code: Select all

imagejpeg($img, "http://www.ahsodes.com/flash/video/mypic.jpg", 90);
The second paramater for imagejpeg is the filename you want to save the image to, pass this either a path on your filesystem or a blank string to output the image without saving it anywhere. I can't tell which you want to do because your code hints at both, you're outputting the content-type which is how you would display the image, then you're using print_r. You need to remove the calls to print_r if you want to display the image. Also you do not need a character encoding header to display the image (I'm pretty sure) since its binary data and has no charset

Posted: Sun Feb 12, 2006 10:24 pm
by bimo
Thanks, that helped.

The reason I entered the charset param into the header function was that I read in the imagejpeg documentation comments that someone wasn't getting anything back either and when they changed the charset from utf-8 to western it fixed it (b/c utf-8 encoding adds an invisible character at the beginning which messes up the image data). It didn't seem to do anything, though, so I took it out as you advised.

I had the full address as the second param because the relative path wasn't working but I tried putting a relative path again as you suggested (I tried just the image name since it's in the same folder) and I tried an absolute address (/vidbb/flash/video/imagename.jpg). With those, an image-missing icon shows up.

When I put '' for the 2nd param, it seems like an image is there but it is blank. Thant's the reason why I had the print_r . I was printing the POST array to make sure that the Flash movie was sending the pixel data. I also wanted to view source and thought if I printed something then it would let me view source (when the print() is not there, 'view source' is greyed out.) After I took the print_r out I do get the blank image again.

I do want to save the image as a real jpg, though, so the '' is sort of

Sorry for my long-windedness. I'm trying to give you all the information that might be relevant.

here's the code that gives the no-img icon:

Code: Select all

// print out the correct header to the browser
header("Content-type:image/jpeg;"); 
// display the image
$jpg = "mypicjpg";  // have also tried /vidbb/flash/video/mypic.jpg
imagejpeg($img,"$jpg", 75);
Thanks,

b

Posted: Sun Feb 12, 2006 11:27 pm
by josh
Of course that shows a broken image, you're not outputting anything to the browser aside from the image header.

Posted: Mon Feb 13, 2006 7:44 am
by bimo
but it hasn't created a new image on the server, either so what am I supposed to output?

Posted: Mon Feb 13, 2006 11:42 am
by josh
Comment your content-type header so youll be able to see what error it is outputting (make sure error reporting is turned on first), if you can't interpret the error message please post it here.


** fixed typo (I told you to UN-comment instead of comment by mistake)

Posted: Mon Feb 13, 2006 7:24 pm
by bimo
When I have a second parameter, it didoesn't give me any results (blank screen) and when I select view source, it has

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=windows-1252"></HEAD>
<BODY></BODY></HTML>
WhenI have no second parameter, it produces no reaults.

Posted: Mon Feb 13, 2006 7:40 pm
by josh
Try this:

Code: Select all

$im=imagecreate(200,200);
$black = imagecolorallocate($im, 0x00, 0x00, 0x00);

header('Content-type:image/jpeg');
imagejpeg($im);
My guess is you removed the second parameter but kept the quality argument (which became the second parameter). If you need to set the quality of the jpeg and you don't want to pass it a filename then I suggest you read the documentation

Posted: Mon Feb 13, 2006 8:51 pm
by bimo
this is what I had after you told me to comment the header

Code: Select all

// print out the correct header to the browser
//header("Content-type:image/jpeg;"); 
// display the image
$jpg = "mypic.jpg"; 
imagejpeg($img,"", 75);
I'll try what you suggested, though.

Posted: Mon Feb 13, 2006 10:16 pm
by josh
What is your error reporting set to? Did the test I just posted work?

Posted: Mon Feb 13, 2006 10:26 pm
by bimo
Yes, the test you posted worked, which made me go back to my code and get to a point where it showed the image but didn't write a new file on the server.

I then added the second param to imagejpeg() and it gives me a missing image icon. Here's all of that code

Code: Select all

error_reporting(8);
/**
 * Get the width and height of the destination image
 * from the POST variables and convert them into
 * integer values
 */
	//If GD library is not installed, say sorry
	if(!function_exists("imagecreate")) die("Sorry, you need GD library to run this example");
	//Capture Post data
	$data = explode(",", $_POST['img']);
	$width = $_POST['width'];
	$height = $_POST['height'];
	//Allocate image
	$image=(function_exists("imagecreatetruecolor"))?imagecreatetruecolor( $width ,$height ):imagecreate( $width ,$height );
	$background = imagecolorallocate( $image ,0 , 0 , 0 ); //imagecolorallocate($img, 0x00, 0x00, 0x00);
	//Copy pixels
	$i = 0;
	for($x=0; $x<=$width; $x++){
		for($y=0; $y<=$height; $y++){
			$r = 255-hexdec("0X".substr( $data[$i] , 0 , 2 )); 
			$g = 255-hexdec("0x".substr( $data[$i] , 2 , 2 )); 
			$b = 255-hexdec("0x".substr( $data[$i++] , 4 , 2 ));
			$color =  ($r << 16) | ($g <<  | $b; 
			imagesetpixel ( $image , $x , $y , $color );
		}
	}
	// print out the correct header to the browser
	header( "Content-type: image/jpeg" );
	$jpg = "mypic.jpg";
	imagejpeg( $image, $jpg, 75 );  // if I write, imagejpeg( $image ); the pic appears, but with the extra parameters, it doesn't. 
	imagedestroy( $image );

Posted: Mon Feb 13, 2006 10:27 pm
by John Cartwright
Please start using [syntax=php][/syntax] tags around your php code.

Posted: Mon Feb 13, 2006 11:21 pm
by josh
Which brings me back to my point,

if you save the image to the server you're not outputting it, therefore there will be no image output. I see no point in continuing this thread any further, if you want to output your image then pass a blank filename. If you want to save it pass a filename. If you pass a filename nothing will be outputted to the browser.

Posted: Mon Feb 13, 2006 11:48 pm
by neophyte
I think he wants to save the image and display it at the same time. So here's my solution....

Put the script that generates the image in a function call. Before the page is displayed make the image and save it to a directory. Then just build an img tag to the saved file.

Posted: Tue Feb 14, 2006 7:20 am
by bimo
Sorry, jshpro. I didn't understand that you were saying that it's an either-or thing

Thanks for your help