Jump to content

Recommended Posts

Hello -

 

I'm using flash to create a desktop wallpaper, and them using PHP to render the image and send it to someone. Everything is working fine, except that when the email is sent, the image that is attached is corrupt and 0kb. The image itself and the to, from, and corresponding email address variables are passed on from flash. Here is my code:

 

<?

error_reporting(0);
/**
 * Get the width and height of the destination image
 * from the POST variables and convert them into
 * integer values
 */
$w = 1024;
$h = 768;

// 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");
// display the image
imagejpeg($img, "wallpaper.jpg", 100);



function createThumbnail($imageName, $thumbWidth) {
	$srcImg = imagecreatefromjpeg($imageName);
	$origWidth = imagesx($srcImg);
	$origHeight = imagesy($srcImg);

	$ratio = $origWidth / $thumbWidth;
	$thumbHeight = $origHeight / $ratio;

	$thumbImg = imagecreatetruecolor($thumbWidth, $thumbHeight);
	imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, 800, 600, $origWidth, $origHeight);

	imagejpeg($thumbImg, "small_wallpaper.jpg", 100);
}

createThumbnail("wallpaper.jpg", 800);


// Take the image to send it by e-mail
function decoder($texte){
	$texte = utf8_decode($texte); // converti en iso-8859-1
	$texte = stripslashes($texte); // élimine les anti-slashs d'échappement
	$texte = trim($texte);  // élimine les '\n', '\r', '\t' etc
	$texte = htmlentities($texte, ENT_QUOTES);
	$texte = strip_tags($texte);
	//$texte = nl2br($texte);  // converti les retours  en <br />
	$texte = str_replace(">", ">", $texte);
	$texte = str_replace("<", "<", $texte);
	return $texte;
}

$width  	= (int)$_POST["imageWidth"];
$toMail 	= $_POST["toEmail"];
$toName 	= $_POST["toName"];
$fromMail 	= $_POST["fromEmail"];
$fromName 	= $_POST["fromName"];


$toMail = decoder($toMail);
$toName = decoder($toName);
$fromMail = decoder($fromMail);
$fromName = decoder($fromName);

$boundary = "-----=".md5(uniqid(rand()));

$headers = "From: ".$fromMail."\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= 'Content-Type: multipart/mixed; boundary="'.$boundary.'"'; 

$emailSubject 	= "You've received a Custom Wallpaper!";


$mailText = '--'.$boundary."\n"; 
$mailText .= 'Content-Type: text/plain; charset="iso-8859-1"'."\n"; 
$mailText .= 'Content-Transfer-Encoding: 8bit'."\n\n"; 
$mailText .= $toName.", You've just received a Custom Wallpaper !"."\n\n"; 

$mailText .= "--". $boundary ."\n";

if ($width==100) {
	$mailText .= 'Content-Type: image/jpeg; name="small_wallpaper.jpg"'."\n"; 
	$mailText .= 'Content-Transfer-Encoding: base64'."\n"; 
	$mailText .= 'Content-Disposition:attachement; filename="small_wallpaper.jpg"'."\n\n"; 
	$mailText .= chunk_split(base64_encode(file_get_contents('small_wallpaper.jpg')))."\n";
}
else {
	$mailText .= 'Content-Type: image/jpeg; name="wallpaper.jpg"'."\n"; 
	$mailText .= 'Content-Transfer-Encoding: base64'."\n"; 
	$mailText .= 'Content-Disposition:attachement; filename="wallpaper.jpg"'."\n\n"; 
	$mailText .= chunk_split(base64_encode(file_get_contents('wallpaper.jpg')))."\n";
}


if (mail($toMail,$toName." - ".$emailSubject,$mailText,$headers)) {
	echo "sent=ok";
} else {
	echo "sent=failed";
}

?>

 

Thanks for any help you can give!

Link to comment
https://forums.phpfreaks.com/topic/107717-email-sending-jpg-as-0kb/
Share on other sites

this is a wild stab in the dark and probably wrong, but maybe you need to actually save the created image into an actual file before you can mail it? I'm really not sure if you're already doing that somewhere, as there are so many things in your script that I'm unfamiliar with, but at first glance, it doesn't look like you're doing that, and I really don't know if that's necessary or not even if you aren't doing it, but hey, maybe, lol.

 

edit:

 

or I guess

 

imagejpeg($thumbImg, "small_wallpaper.jpg", 100);

 

does that, so oh well.  Wild stab in the dark, lol.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.