17-01-2012, 20:38
Proszę oto kody 
index.php
motivational.php
functions.php
uploadscript.php

index.php
Kod PHP:
<html>
<head>
<title>Letsgeek motivational poster maker</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>Create your own motivational poster!</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>Upload image:</td><td><input type="file" name="file_upload"></td>
</tr>
<tr>
<td colspan=2>What format?</td>
</tr>
<td><input type="radio" name="format" value="horizontal" onclick="changeimage(this)" checked /> Horizontal</td><td><input type="radio" name="format" value="vertical" onclick="changeimage(this)" /> Vertical</td>
</tr>
<tr>
<td>Title:</td><td><input type="text" name="titletext"></td>
</tr>
<tr>
<td>Motivational text:</td><td><input type="text" name="motivationaltext"></td>
</tr>
<tr>
<td>Background color:</td><td><input type="text" name="backgroundcolor" value="000000" size=6 MAXLENGTH=6></td>
</tr>
<tr>
<td>Text color:</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="Motivate!">
</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("Wrong RGB value");
</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>';