24-01-2012, 14:39
(Ten post był ostatnio modyfikowany: 27-01-2012, 00:22 przez dawidirzyk.)
Witam. Przepraszam że się podpisałem pod cudzą własnością intelektualną.
Proszę oto kody
index.php
motivational.php
functions.php
uploadscript.php
Ale jest mały problem otóż chciałbym żeby kod z index.php został dodany do bazy dancyh. Chodzi mi o ten kod:
Mam także skrypt dodawania obrazków do bazy dancyh ale z uploadu :/ znaleziony na wsieci.org.pl
Zmieszczam kod:
Struktura tabeli pliki
struktura formularza
upload.php
wyświetl.php
I odnosząc się do powyższego tematu chcę aby ten kod a nie plik z formularza został umieszczony do bazy danych. Bo mi generuje się obrazek z MD 5 kodowaniem i demo znajdziecie na http://irzykpol.cba.pl/plakat i jak stworzycie obrazek to wygeneruje wam się obrazek i ten wyświetlony obrazek ma się dodać do bazy danych. Dzięki wielkie za pomoc. Trochę się rozpisałem :p
Proszę oto kody

index.php
Kod PHP:
<html>
<head>
<title>Stwórz plakat</title>
<script language="javascript" type="text/javascript">
function upload_file() {
document.getElementById('initial').style.display = 'none';
document.getElementById('loading').style.display = 'inline';
}
function upload_file_complete(path) {
document.getElementById('initial').style.display = 'none';
document.getElementById('image').src = path;
document.getElementById('image').style.display = 'inline';
document.getElementById('result').style.display = 'inline';
}
function changeimage(target) {
document.getElementById('exampleimage').src = 'images/' + target.value + '.png';
}
</script>
</head>
<body>
<div id="container" style="width:1024px;">
<div>
<h1>Stwórz plakat:</h1>
<div style="width:650px;" id="initial">
<form id="upload_form" action="motivational.php" method="post" enctype="multipart/form-data" target="motivational">
<div style="float:left;">
<img src="images/horizontal.png" id="exampleimage">
</div>
<div style="float:right;">
<table>
<tr>
<td>Adres obrazka:</td><td><input type="file" name="file_upload"></td>
</tr>
<tr>
<td colspan=2>Wybierz format</td>
</tr>
<td><input type="radio" name="format" value="horizontal" onclick="changeimage(this)" checked />Standardowy</td><td><input type="radio" name="format" value="vertical" onclick="changeimage(this)" />Szerszy</td>
</tr>
<tr>
<td>Tytuł:</td><td><input type="text" name="titletext"></td>
</tr>
<tr>
<td>Tekst motywujący:</td><td><input type="text" name="motivationaltext"></td>
</tr>
<tr>
<td>Kolor tła:</td><td><input type="text" name="backgroundcolor" value="000000" size=6 MAXLENGTH=6></td>
</tr>
<tr>
<td>Kolor tekstu::</td><td><input type="text" name="textcolor" value="FFFFFF" size=6 MAXLENGTH=6></td>
</tr>
</table>
<input type="hidden" value="1" name="send_upload"/>
<input type="submit" value="Generuj!">
</div>
</form>
</div>
<div style="width:600px;display:none;" id="result">
<img src="images/t.gif" id="image" />
</div>
<iframe id="motivational" name="motivational" src="" style="width:0;height:0;border:0px solid #fff;">
</iframe>
</div>
</div>
</body>
</html>
motivational.php
Kod PHP:
<?php
include 'functions.php';
// Variables
$textcolor = $_POST['textcolor'];
$titletext = $_POST['titletext'];
$motivationaltext = $_POST['motivationaltext'];
$backgroundcolor = $_POST['backgroundcolor'];
$format = $_POST['format'];
$allowed_ext = "jpg, gif, png, jpeg, JPG";
$top = 50;
$filename = $_FILES['file_upload']['name'];
// Validation
if(!checkRGB($textcolor))
goto SkipToEnd;
if(!checkRGB($backgroundcolor))
goto SkipToEnd;
/*if(!checkText($titletext))
goto SkipToEndTwice;
if(!checkText($motivationaltext))
goto SkipToEndTwice;*/
if($format == 'horizontal'){
//Dimensions of the finished image
$width = 750;
$height = 650;
//Dimensions of the uploaded image
$imgx = 600;
$imgy = 450;
}else{ // Vertical
//Dimensions of the finished image
$imgx = 600;
$imgy = 550;
//Dimensions of the uploaded image
$width = 650;
$height = 750;
}
// Is the extension allowed?
$extension = checkext($filename, $allowed_ext);
if ($extension != False)
{
// Move the temporary file to the image folder
$save_to="images/";
if ($_POST['send_upload']==1)
{
$file_name=$_FILES['file_upload']['name'];
move_uploaded_file($_FILES['file_upload']['tmp_name'], $save_to.basename($file_name)) or die('Cannot upload image');
$filepath = $save_to.basename($file_name);
}
// Create an image resource from the uploaded image
if($extension == "jpg" || $extension == "jpeg" || $extension == "JPG"){
$source=imagecreatefromjpeg($filepath);
}elseif($extension == "png") {
$source=imagecreatefrompng($filepath);
}elseif($extension == "gif") {
$source=imagecreatefromgif($filepath);
}
// Resize the image resource
$width_orig = imagesx( $source ); // width of image resource
$height_orig = imagesy( $source ); // height of image resource
$uploadedimage = imagecreatetruecolor($imgx, $imgy); // Create space for the resized image
imagecopyresampled($uploadedimage, $source, 0, 0, 0, 0, $imgx, $imgy, $width_orig, $height_orig); // Resize the image to the correct dimensions. Outputs to $uploadedimage.
//Save the resized image
if($extension == "jpg" || $extension == "jpeg" || $extension == "JPG"){
imagejpeg($uploadedimage, $filepath, 100);
}elseif($extension == "png") {
imagepng($uploadedimage, $filepath);
}elseif($extension == "gif") {
imagegif($uploadedimage, $filepath);
}
// Create the background image
$im = @imagecreatetruecolor($width, $height) or die('Cannot Initialize new GD image stream');
// Allocate colors
$textcolor = rgb2array($_POST['textcolor']);
$text_color = imagecolorallocate($im, $textcolor[0], $textcolor[1], $textcolor[2]);
$bordercolors = imagecolorallocate($im, $textcolor[0], $textcolor[1], $textcolor[2]);
$backgroundcolor = rgb2array($backgroundcolor);
$backgroundcolor = imagecolorallocate($im, $backgroundcolor[0], $backgroundcolor[1], $backgroundcolor[2]);
$watermark_color = imagecolorallocate($im, 233, 14, 91);
// Fill Im with the background color.
imagefill ($im, 0, 0, $backgroundcolor);
// Draw a border on the background image
$size = getimagesize($filepath);
$x = ($width / 2) - ($size[0] / 2 ) - 2;
$y = $top - 2;
$w = $imgx + 2;
$h = $imgy + 2;
imageline($im, $x,$y,$x,$y+$h,$bordercolors);
imageline($im, $x,$y,$x+$w,$y,$bordercolors);
imageline($im, $x+$w,$y,$x+$w,$y+$h,$bordercolors);
imageline($im, $x,$y+$h,$x+$w,$y+$h,$bordercolors);
// Merge the original image resource with the background image resource
imagecopy($im, $uploadedimage, $x + 2, $top, 0, 0, $size[0], $size[1]);
// Write out the text
CenterImageString($im, $width, strtoupper($titletext), 40, ($top + $imgy + 50), $text_color);
CenterImageString($im, $width, $motivationaltext, 16, ($top + $imgy + 74), $text_color);
// Watermark. Be a sport and dont remove this.
ImageString($im, 2, $width-160, $height-15, "www.letsgeek.com/motivator", $watermark_color);
//Print and clean stuff
imagepng($im, "posters/" . md5(mktime()) . $filename );
imagedestroy($im);
imagedestroy($uploadedimage);
imagedestroy($source);
}
$filename = "posters/" . md5(mktime()) . $filename;
echo '
<html>
<body>
<script language="javascript" type="text/javascript">
var path = "'.$filename.'";
window.parent.upload_file_complete(path);
</script>
<body>
<html>';
die();
/* ERROR HANDLING RGB */
SkipToEnd:
echo '
<html>
<body>
<script language="javascript" type="text/javascript">
alert("Podaj wartość RGB");
</script>
<body>
<html>';
die();
/* ERROR HANDLING TEXT */
SkipToEndTwice:
echo '
<html>
<body>
<script language="javascript" type="text/javascript">
alert("'.$titletext.'");
</script>
<body>
<html>';
?>
functions.php
Kod PHP:
<?php
//Fix rbg colors
function rgb2array($rgb) {
return array(
base_convert(substr($rgb, 0, 2), 16, 10),
base_convert(substr($rgb, 2, 2), 16, 10),
base_convert(substr($rgb, 4, 2), 16, 10),
);
}
//Center image
function CenterImageString($image, $image_width, $string, $font_size, $y, $color)
{
$text_width = imagefontwidth($font_size)*strlen($string);
$center = ceil($image_width / 2);
$x = $center - (ceil($text_width/2));
### Get exact dimensions of text string
$box = @imageTTFBbox($font_size,0, 'arial.ttf', $string);
### Get width of text from dimensions
$textwidth = abs($box[4] - $box[0]);
### Get x-coordinate of centered text horizontally using length of the image and length of the text
$xcord = ($image_width/2)-($textwidth/2)-2;
imagettftext($image, $font_size, 0, $xcord, $y, $color, 'arial.ttf', $string);
}
function checkext($image, $allowed_ext)
{
$allowed_ext = explode(", ", $allowed_ext);
$info = pathinfo($image);
$extension = $info['extension'];
$ext = False;
for($i = 0; $i < count($allowed_ext); $i++)
{
if ($allowed_ext[$i] == $extension) {
$ext = $allowed_ext[$i]; }
}
return $ext;
}
function resize($source, $imgx, $imgy)
{
$width_orig = imagesx( $source ); // width of original image
$height_orig = imagesy( $source ); // height of original image
$uploadedimage = imagecreatetruecolor($imgx, $imgy); // Create space for the resized image
imagecopyresampled($uploadedimage, $source, 0, 0, 0, 0, $imgx, $imgy, $width_orig, $height_orig); // Resize the image to the correct dimensions. Outputs to $uploadedimage.
return $uploadedimage;
}
function checkRGB($input) {
if(preg_match('/^[a-fA-F0-9]{6}$/', $input)) //Is it valid?
Return true;
Return false;
}
?>
uploadscript.php
Kod PHP:
<?php
//Upload image
$save_to="images";
if ($_POST['send_upload']==1) {
$file_name=$_FILES['file_upload']['name'];
move_uploaded_file($_FILES['file_upload']['tmp_name'], $save_to."/".basename($file_name)) or die('FEL');
$filepath = $save_to."/".basename($file_name);
}
if($_POST['format'] == "horizontal"){
$format = "horizontal";
$width = 600;
$height = 450;
}else{
$format = "vertical";
$width = 600;
$height = 550;
}
?>
<script language="javascript" type="text/javascript">
var path = "<?php echo $filepath; ?>";
var format = "<?php echo $format; ?>";
var width = "<?php echo $width; ?>";
var height = "<?php echo $height; ?>";
window.top.upload_file_complete();
window.parent.document.getElementById('exampleimage').src = path;
window.parent.document.getElementById('exampleimage').width = width;
window.parent.document.getElementById('exampleimage').height = height;
window.parent.document.getElementById('image').value = path;
window.parent.document.getElementById('format').value = format;
</script>';
Ale jest mały problem otóż chciałbym żeby kod z index.php został dodany do bazy dancyh. Chodzi mi o ten kod:
Kod PHP:
<img src="images/t.gif" id="image" />
Mam także skrypt dodawania obrazków do bazy dancyh ale z uploadu :/ znaleziony na wsieci.org.pl
Zmieszczam kod:
Struktura tabeli pliki
Kod PHP:
CREATE TABLE pliki (
id int(5) NOT NULL auto_increment,
typ varchar(30) NOT NULL,
rozmiar int(11) NOT NULL,
nazwa varchar(200) NOT NULL,
dane mediumblob NOT NULL,
PRIMARY KEY (id)
);
struktura formularza
Kod PHP:
<form method="POST" enctype="multipart/form-data" action="upload.php">
<input type="hidden" name="ok" value="1">
<input type="hidden" name="MAX_FILE_SIZE" value="665600">
<input type="file" name="plik" size="40" />
<input e="submit" value="Uploaduj" />
</form>
upload.php
Kod PHP:
<?php $user = "root";
$password = "";
$host = "localhost";
$db = "nazwa_bazy";
$link = @mysql_connect($host, $user, $password);
if (!$link) die ("
Nie mozna sie polaczyc z serwerem MySQL!
");
@mysql_select_db($db, $link) or die ("
Nie mozna sie polaczyc z baza!
Spróbuj za chwilę! PRZEPRASZAMY
");
$katalog = "pliki/";
if($HTTP_POST_VARS['ok'] == 1){
if($HTTP_POST_FILES['plik']['size'] < 665600 || $HTTP_POST_FILES['plik']['size'] != 0){
if(is_uploaded_file($HTTP_POST_FILES['plik']['tmp_name']) || !isset($HTTP_POST_FILES['plik'])){
$HTTP_POST_FILES['plik']['name'] = strtolower($HTTP_POST_FILES['plik']['name']);
$HTTP_POST_FILES['plik']['name'] = str_replace(" ", "_", $HTTP_POST_FILES['plik']['name']);
copy($HTTP_POST_FILES['plik']['tmp_name'], $katalog.$HTTP_POST_FILES['plik']['name']);
$adres_pliku = $katalog.$HTTP_POST_FILES['plik']['name'];
$uchwyt = fopen($adres_pliku, "r");
$tresc = fread($uchwyt, filesize($adres_pliku));
fclose($uchwyt);
unlink($adres_pliku);
$tresc = addslashes($tresc);
$wielkosc = $HTTP_POST_FILES['plik']['size'];
$n_pliku = $HTTP_POST_FILES['plik']['name'];
$typ = $HTTP_POST_FILES['plik']['type'];
$query= "INSERT INTO zalacznik(id,typ,rozmiar,nazwa,dane) VALUES('$typ','$wielkosc','$n_pliku','$tresc')";
mysql_query($query, $link);
}
else{
echo = "Błąd";
}
}
else{
echo = "Błąd";
}
}
mysql_close($link);
?>
wyświetl.php
Kod PHP:
<?php
if(isset($_GET['id']))
{
//tu połącz się z bazą danych
$id = $_GET['id'];
$zapytanie = "SELECT typ, rozmiar, nazwa, dane FROM pliki WHERE id = '$id'";
$wynik = mysql_query($zapytanie);
$plik = mysql_fetch_array($wynik);
header("Content-length: $plik[rozmiar]");
header("Content-type: $plik[typ]");
header("Content-Disposition: attachment; filename=$plik[nazwa]");
echo $plik[dane];
//w tym miejscu rozłącz się z bazą danych
exit();
}
?>
I odnosząc się do powyższego tematu chcę aby ten kod
Kod PHP:
<img src="images/t.gif" id="image" />