PHP 0: Difference between revisions
| Line 216: | Line 216: | ||
== Обработка на изображение == | == Обработка на изображение == | ||
<html> | |||
<body> | |||
<?php | |||
print "<pre>"; | |||
print "POST ->"; | |||
print_r( $_POST); | |||
print "FILES ->"; | |||
print_r($_FILES); | |||
print "</pre>"; | |||
if (isset($_POST['submit'])) | |||
{ | |||
$maxSize = 500; //kB maximum file size | |||
$errorList = ''; | |||
$newImageHeight = 400; | |||
$newImageWidth = 600; //px | |||
if($_FILES["image"]['name'] ) | |||
{ | |||
$fileNameParts = explode( ".", $_FILES["image"]['name']); | |||
$fileExtension = end( $fileNameParts ); // part behind last dot | |||
$photo_type = $_FILES["image"]['type']; | |||
} | |||
else $errorList = "<br />Моля изберете файл"; | |||
if ( !$errorList && $fileExtension != "jpg" && $fileExtension != "JPG" ) | |||
{ | |||
$errorList .= "<br /> format is not supported"; | |||
} | |||
$photoSize = $_FILES["image"]['size']; // size of uploaded file | |||
if ( !$errorList && $photoSize == 0 ) | |||
{ | |||
$errorList = "<br />$photoFileName - wrong data transfer"; | |||
} | |||
if ( !$errorList && $photoSize > $maxSize*1024) | |||
{ | |||
$errorList = "<br>$photoFileName - image is to large"; | |||
} | |||
if (!$errorList) | |||
{ | |||
$tempFileName = $_FILES['image']['tmp_name']; // temporary file at server side | |||
$tempFile = fopen( $tempFileName, "r" ); | |||
$binaryPhoto = fread( $tempFile, filesize( $tempFileName ) ); | |||
$src_img = imagecreatefromstring( $binaryPhoto ); // try to create image | |||
if ( !$src_img ) | |||
{ | |||
$errorList = "<br>$photoFileName - wrong image format";; | |||
exit( -1 ); | |||
} | |||
$width = imagesx( $src_img ); // get original source image width | |||
$height = imagesy( $src_img ); // and height | |||
$yCrop = 0; | |||
$xCrop = 0; | |||
$in_prop = $width / $height; | |||
$out_prop = $newImageWidth / $newImageHeight; | |||
//resize main image, crop if nessery | |||
$image = imagecreatetruecolor( $newImageWidth, $newImageHeight ); | |||
if ( $width > $height ) | |||
{ | |||
if ( $in_prop > $out_prop ) $xCrop = round( (($width - ( $height* $out_prop))/2) ); | |||
else $yCrop = round( (($height - ($width/$out_prop))/2)); | |||
} | |||
else | |||
{ | |||
$yCrop = round( (($height - ($width/$out_prop))/2)); | |||
} | |||
imagecopyresampled( $image, $src_img, 0, 0, $xCrop, $yCrop,$newImageWidth, $newImageHeight,($width-$xCrop*2), ($height-$yCrop*2) ); | |||
$textColor= imagecolorallocate($image,255,0,0); | |||
$font = "./VeraSe.ttf"; | |||
$font_size = 40; | |||
$angle = 25; | |||
$x = 40; | |||
$y = 30; | |||
imagettftext( $image, $font_size, $angle, $x, $y, $textColor, $font, 'TEST'); | |||
imagegammacorrect ( $image, 1.0, 1.15 ); | |||
if( imagejpeg( $image, '../user/test.JPG' )) $errorList = "Picture has been successfully uploaded!\n<br>\n"; | |||
} | |||
} | |||
print $errorList; | |||
?> | |||
<div style="position:relative; top:100px; left:100px"> | |||
<form method="post" name="comment" enctype="multipart/form-data" action="" > | |||
<table> | |||
<tr> | |||
<td>Email: </td> | |||
<td><input name="email" type="text" size="20" maxlength="199" value="" /></td></tr> | |||
<tr> | |||
<td>Име </td> | |||
<td><input name="name" type="text" size="20" maxlength="60" value="" /></td></tr> | |||
<tr> | |||
<td>Коментар: </td><td><textarea name="comment" cols="43" rows="4"></textarea></td></tr> | |||
<tr> | |||
<tr> | |||
<td>Картинка: </td><td><input type="file" name="image" title="Select Image"></td></tr> | |||
<tr> | |||
<td colspan="2" align="center"><input type="submit" name="submit" id="ss" value="Изпрати"> | |||
</td></tr></table> | |||
</form> | |||
</div> | |||
</body> | |||
</html> | |||
== Прехвърляне данни между скриптове == | == Прехвърляне данни между скриптове == | ||
Revision as of 10:20, 7 October 2011
HTML форма
<body>
<div style="position:relative; top:100px; left:100px">
<form method="post" name="comment" action="" >
<table>
<tr>
<td>Email: </td>
<td><input name="email" type="text" size="20" maxlength="199" value="" /></td></tr>
<tr>
<td>Име </td>
<td><input name="name" type="text" size="20" maxlength="60" value="" /></td></tr>
<tr>
<td>Коментар: </td><td><textarea name="comment" cols="43" rows="4"></textarea></td></tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" id="ss" value="Изпрати">
</td></tr></table>
</form>
</div>
</body>
</html>
PHP вградени промеливи
<html>
<body>
<?php
print "POST ->";
print_r( $_POST);
print "GET ->";
print_r($_GET);
print "SERVER ->";
print_r($_SERVER);
print "SESSION ->";
print_r($_SESSION);
?>
<div style="position:relative; top:100px; left:100px">
<form method="post" name="comment" action="" >
<table>
<tr>
<td>Email: </td>
<td><input name="email" type="text" size="20" maxlength="199" value="" /></td></tr>
<tr>
<td>Име </td>
<td><input name="name" type="text" size="20" maxlength="60" value="" /></td></tr>
<tr>
<td>Коментар: </td><td><textarea name="comment" cols="43" rows="4"></textarea></td></tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" id="ss" value="Изпрати">
</td></tr></table>
</form>
</div>
</body>
</html>
Запис във файл
<html>
<body>
<?php
print "POST ->";
print_r( $_POST);
print "GET ->";
print_r($_GET);
if (isset($_POST['submit']))
{
$fd = fopen('../user/data.txt' , 'a');
fwrite ( $fd , $_POST['email']."\n");
fwrite ( $fd , $_POST['email']."\n");
fwrite ( $fd , $_POST['comment']."\n");
fclose($fd);
}
?>
<div style="position:relative; top:100px; left:100px">
<form method="post" name="comment" action="" >
<table>
<tr>
<td>Email: </td>
<td><input name="email" type="text" size="20" maxlength="199" value="" /></td></tr>
<tr>
<td>Име </td>
<td><input name="name" type="text" size="20" maxlength="60" value="" /></td></tr>
<tr>
<td>Коментар: </td><td><textarea name="comment" cols="43" rows="4"></textarea></td></tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" id="ss" value="Изпрати">
</td></tr></table>
</form>
</div>
</body>
</html>
Качване на файл
<html>
<body>
<?php
print "POST ->";
print_r( $_POST);
print "FILES ->";
print_r($_FILES);
if (isset($_POST['submit']))
{
$fd = fopen('../user/data.txt' , 'a');
fwrite ( $fd , $_POST['email']."\n");
fwrite ( $fd , $_POST['email']."\n");
fwrite ( $fd , $_POST['comment']."\n");
fclose($fd);
}
?>
<div style="position:relative; top:100px; left:100px">
<form method="post" name="comment" enctype="multipart/form-data" action="" >
<table>
<tr>
<td>Email: </td>
<td><input name="email" type="text" size="20" maxlength="199" value="" /></td></tr>
<tr>
<td>Име </td>
<td><input name="name" type="text" size="20" maxlength="60" value="" /></td></tr>
<tr>
<td>Коментар: </td><td><textarea name="comment" cols="43" rows="4"></textarea></td></tr>
<tr>
<tr>
<td>Картинка: </td><td><input type="file" name="image" title="Select Image"></td></tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" id="ss" value="Изпрати">
</td></tr></table>
</form>
</div>
</body>
</html>
Запис на качен файл
<html>
<body>
<?php
print "POST ->";
print_r( $_POST);
print "FILES ->";
print_r($_FILES);
if (isset($_POST['submit']))
{
if($_FILES["image"]['name'] )
{
$tempFileName = $_FILES['image']['tmp_name']; // temporary file at server side
copy ( $_FILES['image']['tmp_name'], '../user/'.$_FILES["image"]['name'] );
}
}
?>
<div style="position:relative; top:100px; left:100px">
<form method="post" name="comment" enctype="multipart/form-data" action="" >
<table>
<tr>
<td>Email: </td>
<td><input name="email" type="text" size="20" maxlength="199" value="" /></td></tr>
<tr>
<td>Име </td>
<td><input name="name" type="text" size="20" maxlength="60" value="" /></td></tr>
<tr>
<td>Коментар: </td><td><textarea name="comment" cols="43" rows="4"></textarea></td></tr>
<tr>
<tr>
<td>Картинка: </td><td><input type="file" name="image" title="Select Image"></td></tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" id="ss" value="Изпрати">
</td></tr></table>
</form>
</div>
</body>
</html>
Обработка на изображение
<html> <body>
<?php
print "
"; print "POST ->"; print_r( $_POST); print "FILES ->"; print_r($_FILES); print "
";
if (isset($_POST['submit'])) {
$maxSize = 500; //kB maximum file size
$errorList = ; $newImageHeight = 400; $newImageWidth = 600; //px
if($_FILES["image"]['name'] )
{
$fileNameParts = explode( ".", $_FILES["image"]['name']);
$fileExtension = end( $fileNameParts ); // part behind last dot
$photo_type = $_FILES["image"]['type'];
}
else $errorList = "
Моля изберете файл";
if ( !$errorList && $fileExtension != "jpg" && $fileExtension != "JPG" )
{
$errorList .= "
format is not supported";
}
$photoSize = $_FILES["image"]['size']; // size of uploaded file
if ( !$errorList && $photoSize == 0 )
{
$errorList = "
$photoFileName - wrong data transfer";
}
if ( !$errorList && $photoSize > $maxSize*1024)
{
$errorList = "
$photoFileName - image is to large";
}
if (!$errorList)
{
$tempFileName = $_FILES['image']['tmp_name']; // temporary file at server side
$tempFile = fopen( $tempFileName, "r" );
$binaryPhoto = fread( $tempFile, filesize( $tempFileName ) );
$src_img = imagecreatefromstring( $binaryPhoto ); // try to create image
if ( !$src_img )
{
$errorList = "
$photoFileName - wrong image format";;
exit( -1 );
}
$width = imagesx( $src_img ); // get original source image width
$height = imagesy( $src_img ); // and height
$yCrop = 0; $xCrop = 0;
$in_prop = $width / $height; $out_prop = $newImageWidth / $newImageHeight;
//resize main image, crop if nessery $image = imagecreatetruecolor( $newImageWidth, $newImageHeight );
if ( $width > $height ) { if ( $in_prop > $out_prop ) $xCrop = round( (($width - ( $height* $out_prop))/2) ); else $yCrop = round( (($height - ($width/$out_prop))/2));
}
else
{
$yCrop = round( (($height - ($width/$out_prop))/2));
}
imagecopyresampled( $image, $src_img, 0, 0, $xCrop, $yCrop,$newImageWidth, $newImageHeight,($width-$xCrop*2), ($height-$yCrop*2) );
$textColor= imagecolorallocate($image,255,0,0);
$font = "./VeraSe.ttf"; $font_size = 40; $angle = 25; $x = 40; $y = 30;
imagettftext( $image, $font_size, $angle, $x, $y, $textColor, $font, 'TEST');
imagegammacorrect ( $image, 1.0, 1.15 );
if( imagejpeg( $image, '../user/test.JPG' )) $errorList = "Picture has been successfully uploaded!\n
\n";
}
} print $errorList; ?>
<form method="post" name="comment" enctype="multipart/form-data" action="" >
| Email: | <input name="email" type="text" size="20" maxlength="199" value="" /> |
| Име | <input name="name" type="text" size="20" maxlength="60" value="" /> |
| Коментар: | <textarea name="comment" cols="43" rows="4"></textarea> |
| Картинка: | <input type="file" name="image" title="Select Image"> |
| <input type="submit" name="submit" id="ss" value="Изпрати"> | |
</form>
</body> </html>