Tuesday, June 8, 2010

Save JPEG file and force download

//
//get our width and height in integers from the post values
$width = (int)$_POST['width'];
$height = (int)$_POST['height'];
$data = $_POST['data']; //data of the image
//begin our image
$temp_img = imagecreatetruecolor($width, $height);
//fill in white space
imagefill($temp_img, 0, 0, 0xFFFFFF);

$rows = 0;
$cols = 0;
$data_row = explode("|", $data);
//replace data with color values
for($rows = 0; $rows < $height; $rows++){
$c_row = explode(",", $data_row[$rows]);
for($cols = 0; $cols < $width; $cols++){
$value = $c_row[$cols];
if($value != ""){
$hex = $value;
while(strlen($hex) < 6){
$hex = "0" . $hex;
}
$r = hexdec(substr($hex, 0, 2));
$g = hexdec(substr($hex, 2, 2));
$b = hexdec(substr($hex, 4, 2));
$test = imagecolorallocate($temp_img, $r, $g, $b);
imagesetpixel($temp_img, $cols, $rows, $test);
}
}
}
//output to jpg and prompt download
header("Content-Type: image/jpeg");
header("Content-Disposition: attachment; filename=image_save.jpg");
imagejpeg($temp_img, "", 100);

1 comment:

  1. Kalakkuringa ponga, Web Technologies la pisthu pola neenga..

    ReplyDelete