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:
Code: Select all
<?
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";
}
?>