I am having problems with the imagecreatefromstring() function. It is returning false and I am not really sure why. The file uploaded is definetly a valid jpg.

Any suggestions or input would be greatly appreciated!


	$userfile  = addslashes (fread (fopen ($_FILES["image_file"]["tmp_name"], "r"), filesize($_FILES["image_file"]["tmp_name"])));
	$file_name = $_FILES["image_file"]["name"];
	$file_size = $_FILES["image_file"]["size"];
	$file_type = $_FILES["image_file"]["type"];

	 // read photo

	$old_error_reporting = error_reporting(E_ALL & ~(E_WARNING)); // ingore warnings
	if(!$src_image = imagecreatefromstring($userfile)){echo "false";} // try to create image
	error_reporting($old_error_reporting);

	$width = imagesx($src_img); // get original source image width
	$height = imagesy($src_img); // and height

	// create small thumbnail
	if($width > $height){
		$dest_width = 120;
		$dest_height = $height * ($dest_width/$width);
	}else{
		$dest_height = 120;
		$dest_width = $width * ($dest_height/$height);
	}

	$dest_img = imagecreatetruecolor($dest_width, $dest_height);
	//$dest_img = imagecreate($dest_width, $dest_height);

	$result = imagecopyresampled( $dest_img, $src_img, 0, 0, 0, 0, $dest_width, $dest_height, $width, $height); // resize the image 
	/* imagecopyresized( $dest_img, $src_img, 0, 0, 0, 0, $dest_width, $dest_height, $width, $height); // resize the image */
	ob_start(); // Start capturing stdout.
	imageJPEG($dest_img); // As though output to browser.
	$binaryThumbnail = ob_get_contents(); // the raw jpeg image data.
	ob_end_clean(); // Dump the stdout so it does not screw other output.
	$binaryThumbnail = addslashes($binaryThumbnail);

Thanks!
Brian

    Your use of addslashes is probably corrupting the binary data by adding slashes to it.

      Weedpacket wrote:

      Your use of addslashes is probably corrupting the binary data by adding slashes to it.

      I thought so too. I got this script elsewhere originally. The portion of the code I am having a problem with is the portion that doesn't have the "addslashes()"

        There's only one use of imagecreatefromstring() I can see in the code you posted, and that's the one that uses the $userfile you used addslashes() on. Or is the problem not with imagecreatefromstring()?

          Weedpacket wrote:

          There's only one use of imagecreatefromstring() I can see in the code you posted, and that's the one that uses the $userfile you used addslashes() on. Or is the problem not with imagecreatefromstring()?

          if(!$src_image = imagecreatefromstring($userfile)){echo "false";} // try to create image

            Yes .... and?

            [man]imagecreatefromstring[/man]
            FALSE is returned if the image type is unsupported, the data is not in a recognised format, or the image is corrupt and cannot be loaded [emphasis mine].

              well. I tried this with several images. Several jpegs. None of them work.

              I am so confused!

                Your use of addslashes is probably corrupting the binary data by adding slashes to it.

                A few other things:

                1. $old_error_reporting = error_reporting(E_ALL & ~(E_WARNING)); // ingore warnings Maybe not ignoring warnings would help? Use E_ALL in development.

                2. Is the source image called $src_img or $src_image?

                3. There is a function called [man]ob_get_clean[/man] that would tidy up the code at the end.

                4. The ob_ functions probably aren't even needed if you're just going to be saving that in a file (see [man]imagejpeg[/man] for why).

                5. There is a function called [man]file_get_contents[/man] that would tidy up the code at the beginning

                6. If an image is generated (even if it's invalid) looking at it as raw data may reveal something (like an error message)

                  7 days later

                  Okay...I tried all of those things and still nothing. I am very confused by this. I took out all of the jazz about warnings, and here's what it gives me now:

                  Warning: imagesx(): supplied argument is not a valid Image resource in /mnt/web_d/d01/s10/b021f6d8/www/thefamilyforest/personal_photos_process.php on line 37
                  
                  Warning: imagesy(): supplied argument is not a valid Image resource in /mnt/web_d/d01/s10/b021f6d8/www/thefamilyforest/personal_photos_process.php on line 38
                  
                  Warning: Division by zero in /mnt/web_d/d01/s10/b021f6d8/www/thefamilyforest/personal_photos_process.php on line 46
                  
                  Warning: imagecreatetruecolor(): Invalid image dimensions in /mnt/web_d/d01/s10/b021f6d8/www/thefamilyforest/personal_photos_process.php on line 49
                  
                  Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /mnt/web_d/d01/s10/b021f6d8/www/thefamilyforest/personal_photos_process.php on line 52
                  
                  Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /mnt/web_d/d01/s10/b021f6d8/www/thefamilyforest/personal_photos_process.php on line 68
                  

                  So basically, the imagecreatefromstring is not working, but I am not really sure why. The string being passed to it is

                  $userfile = (fread (fopen ($FILES["image_file"]["tmp_name"], "r"), filesize($FILES["image_file"]["tmp_name"])));

                  It gives me no errors regarding anything that has to do with imagecreatefromstring.

                  My version of GD is 2.0.28 with JPG support enabled.

                  Any more insight on this matter???

                    Right, you've looked at item 1 on my list. Did you go on to look at item 2?

                      I tried them all, but wasn't sure how to use number 5 and how to do 6.

                      thanks for your help.

                        Write a Reply...