Difference between revisions of "PHP 0"
From Ilianko
| (21 intermediate revisions by the same user not shown) | |||
| Line 2: | Line 2: | ||
== HTML форма == | == HTML форма == | ||
<pre> | <pre> | ||
| + | <html> | ||
<body> | <body> | ||
| Line 33: | Line 34: | ||
<?php | <?php | ||
| − | |||
print "POST ->"; | print "POST ->"; | ||
print_r( $_POST); | print_r( $_POST); | ||
| Line 44: | Line 44: | ||
print_r($_SESSION); | print_r($_SESSION); | ||
| − | |||
?> | ?> | ||
| Line 69: | Line 68: | ||
</pre></code> | </pre></code> | ||
| + | |||
| + | == Запис във файл == | ||
| + | <code><pre> | ||
| + | |||
| + | <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> | ||
| + | </pre></code> | ||
| + | |||
| + | == Качване на файл == | ||
| + | <pre> | ||
| + | |||
| + | |||
| + | <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> | ||
| + | </pre> | ||
| + | |||
| + | == Запис на качен файл == | ||
| + | <pre> | ||
| + | <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> | ||
| + | </pre> | ||
| + | |||
| + | == Обработка на изображение == | ||
| + | <pre> | ||
| + | <html> | ||
| + | <body> | ||
| + | |||
| + | <?php | ||
| + | |||
| + | print "POST ->"; | ||
| + | print_r( $_POST); | ||
| + | |||
| + | print "FILES ->"; | ||
| + | print_r($_FILES); | ||
| + | |||
| + | |||
| + | 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> | ||
| + | </pre> | ||
| + | |||
| + | == Прехвърляне данни между скриптове == | ||
| + | === форма === | ||
| + | <pre> | ||
| + | <html> | ||
| + | <body> | ||
| + | |||
| + | <?php | ||
| + | |||
| + | |||
| + | print "GET ->"; | ||
| + | print_r( $_GET); | ||
| + | |||
| + | |||
| + | if(isset($_GET['errorList'])) | ||
| + | { | ||
| + | print $_GET['errorList']; | ||
| + | }; | ||
| + | ?> | ||
| + | |||
| + | <div style="position:relative; top:100px; left:100px"> | ||
| + | <form method="post" name="comment" enctype="multipart/form-data" action="form7a.php" > | ||
| + | <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> | ||
| + | </pre> | ||
| + | |||
| + | === обработка === | ||
| + | <pre> | ||
| + | <?php | ||
| + | |||
| + | 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); | ||
| + | |||
| + | |||
| + | $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 /> wrong data transfer"; | ||
| + | } | ||
| + | |||
| + | if ( !$errorList && $photoSize > $maxSize*1024) | ||
| + | { | ||
| + | $errorList = "<br>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>$ 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"; | ||
| + | |||
| + | } | ||
| + | else header("Location: form7.php?errorList=$errorList"); | ||
| + | } | ||
| + | else header("Location: form7.php"); /* Redirect browser */ | ||
| + | ?> | ||
| + | <html> | ||
| + | <body> | ||
| + | <?php print $errorList ?> | ||
| + | <img src="../user/test.JPG" /> | ||
| + | |||
| + | </body> | ||
| + | </html> | ||
| + | </pre> | ||
| + | |||
| + | == Сесии == | ||
| + | === форма === | ||
| + | <pre> | ||
| + | <html> | ||
| + | <body> | ||
| + | |||
| + | <?php | ||
| + | |||
| + | error_reporting( E_ALL ^ E_NOTICE); | ||
| + | session_start(); | ||
| + | |||
| + | |||
| + | if(isset($_GET['errorList'])) | ||
| + | { | ||
| + | print $_GET['errorList']; | ||
| + | }; | ||
| + | ?> | ||
| + | |||
| + | <div style="position:relative; top:100px; left:100px"> | ||
| + | <form method="post" name="comment" enctype="multipart/form-data" action="form8a.php" > | ||
| + | <table> | ||
| + | <tr> | ||
| + | <td>Email: </td> | ||
| + | <td><input name="email" type="text" size="20" maxlength="199" value="<?= $_SESSION['email'] ?>" /></td></tr> | ||
| + | <tr> | ||
| + | <td>Име </td> | ||
| + | <td><input name="name" type="text" size="20" maxlength="60" value="<?= $_SESSION['name'] ?>" /></td></tr> | ||
| + | <tr> | ||
| + | <td>Коментар: </td><td><textarea name="comment" cols="43" rows="4"><?= $_SESSION['comment'] ?></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> | ||
| + | </pre> | ||
| + | |||
| + | === обработка === | ||
| + | <pre> | ||
| + | <?php | ||
| + | session_start(); | ||
| + | |||
| + | if (isset($_POST['submit'])) | ||
| + | { | ||
| + | |||
| + | $_SESSION['email'] = $_POST['email']; | ||
| + | $_SESSION['name'] = $_POST['name']; | ||
| + | $_SESSION['comment'] = $_POST['comment']; | ||
| + | |||
| + | |||
| + | |||
| + | $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 /> wrong data transfer"; | ||
| + | } | ||
| + | |||
| + | if ( !$errorList && $photoSize > $maxSize*1024) | ||
| + | { | ||
| + | $errorList = "<br>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>$ 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 = 125; | ||
| + | $x = 140; | ||
| + | $y = 130; | ||
| + | |||
| + | 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"; | ||
| + | |||
| + | } | ||
| + | else header("Location: form8.php?errorList=$errorList"); | ||
| + | } | ||
| + | else header("Location: form8.php"); /* Redirect browser */ | ||
| + | ?> | ||
| + | <html> | ||
| + | <body> | ||
| + | <?php print $errorList ?> | ||
| + | <img src="../user/test.JPG" /> | ||
| + | |||
| + | </body> | ||
| + | </html> | ||
| + | </pre> | ||
| + | |||
| + | == CAPTCHA == | ||
| + | |||
| + | |||
| + | === генератор на картинки === | ||
| + | <pre> | ||
| + | <?php | ||
| + | |||
| + | session_start(); | ||
| + | |||
| + | class ImageGenerator{ | ||
| + | |||
| + | public $inputString; | ||
| + | |||
| + | private $width; | ||
| + | private $height; | ||
| + | private $bgColor; | ||
| + | private $textColor; | ||
| + | private $img; | ||
| + | |||
| + | // initialize input arguments | ||
| + | |||
| + | public function __construct($inputString='Default Input String',$width=140,$height=40) | ||
| + | { | ||
| + | $this->inputString=$inputString; | ||
| + | $this->width=$width; | ||
| + | $this->height=$height; | ||
| + | $this->bgColor=array(rand(0,128), rand(0,128), rand(0,128)); | ||
| + | $this->textColor=array(rand(128,255), rand(128,255), rand(128,255)); | ||
| + | } | ||
| + | |||
| + | // create image stream | ||
| + | |||
| + | private function buildImageStream() | ||
| + | { | ||
| + | if(!$this->img=imagecreate($this->width,$this->height)) | ||
| + | { | ||
| + | throw new Exception('Error creating image stream'); | ||
| + | } | ||
| + | |||
| + | // allocate background color on image stream | ||
| + | imagecolorallocate($this->img,$this->bgColor[0],$this->bgColor[1],$this->bgColor[2]); | ||
| + | |||
| + | // allocate text color on image stream | ||
| + | $textColor=imagecolorallocate($this->img,$this->textColor[0],$this->textColor[1],$this->textColor[2]); | ||
| + | |||
| + | $font = "./VeraSe.ttf"; | ||
| + | $font_size = 16; | ||
| + | $angle = 0; | ||
| + | $x = (int)($this->width/2-strlen($this->inputString)*5); | ||
| + | $y = (int)($this->height/2+9); | ||
| + | |||
| + | imagettftext( $this->img, $font_size, $angle, $x, $y, $textColor, $font, $this->inputString); | ||
| + | |||
| + | for ($i = 1; $i<3; $i++) | ||
| + | { | ||
| + | $a = rand(1, $this->width-1); | ||
| + | $b = rand(1, $this->width-1); | ||
| + | //imageline ( $this->img , $a , 0 , $b , $this->height-1 , $textColor); | ||
| + | //imageline ( $this->img , $a-1 , 0 , $b-1 , $this->height-1 , $textColor); | ||
| + | |||
| + | } | ||
| + | |||
| + | for ($i = 1; $i<3; $i++) | ||
| + | { | ||
| + | $a = rand( 1,$this->height-1); | ||
| + | $b = rand( 1,$this->height-1); | ||
| + | //imageline ( $this->img , 0, $a, $this->width-1, $b, $textColor); | ||
| + | //imageline ( $this->img , 0, $a+1, $this->width-1, $b+1, $textColor); | ||
| + | } | ||
| + | |||
| + | |||
| + | } | ||
| + | |||
| + | // display image stream on the browser | ||
| + | public function displayImage() | ||
| + | { | ||
| + | $this->buildImageStream(); | ||
| + | header("Content-type: image/jpeg"); | ||
| + | // display image | ||
| + | imagejpeg($this->img); | ||
| + | // free up memory | ||
| + | imagedestroy($this->img); | ||
| + | } | ||
| + | |||
| + | public function randStr() | ||
| + | { | ||
| + | for ($i = 0; $i < 5; $i++) { | ||
| + | // this numbers refer to numbers of the ascii table (small-caps) | ||
| + | $str .= strtoupper(chr(rand(97, 122))); | ||
| + | } | ||
| + | return $str; | ||
| + | } | ||
| + | |||
| + | public function randMath() | ||
| + | { | ||
| + | $a = rand(1,11); | ||
| + | $b = rand(1,11); | ||
| + | $d = rand(11,21); | ||
| + | $c = rand(0,1); | ||
| + | |||
| + | if ($c) | ||
| + | { | ||
| + | $str = "$a + $b = ?"; //string kojto se izvezhda | ||
| + | $c = $a+$b; // string s kojtose sravnqva | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | $str = "$a * $b = ?"; | ||
| + | $c = $a*$b; | ||
| + | } | ||
| + | $res = array ($c, $str); | ||
| + | return $res; | ||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | |||
| + | // display sample input string | ||
| + | |||
| + | try{ | ||
| + | |||
| + | // create new instance of 'ImageGenerator' class | ||
| + | |||
| + | $imgGen=new ImageGenerator(); | ||
| + | |||
| + | // display image stream on the browser | ||
| + | $_SESSION['rand_code'] = $imgGen->randMath(); //dobaveno ot ilianko | ||
| + | $imgGen->inputString = $_SESSION['rand_code'][1]; //string , kojto se izvezhda v kartinkata | ||
| + | //print $imgGen->inputString; | ||
| + | $imgGen->displayImage(); | ||
| + | } | ||
| + | |||
| + | catch(Exception $e) | ||
| + | { | ||
| + | echo $e->getMessage(); | ||
| + | exit(); | ||
| + | } | ||
| + | |||
| + | ?> | ||
| + | </pre> | ||
| + | |||
| + | === форма === | ||
| + | <pre> | ||
| + | <html> | ||
| + | <body> | ||
| + | |||
| + | <?php | ||
| + | |||
| + | error_reporting( E_ALL ^ E_NOTICE); | ||
| + | session_start(); | ||
| + | |||
| + | |||
| + | if(isset($_GET['errorList'])) | ||
| + | { | ||
| + | print $_GET['errorList']; | ||
| + | }; | ||
| + | |||
| + | ?> | ||
| + | |||
| + | <div style="position:relative; top:100px; left:100px"> | ||
| + | <form method="post" name="comment" enctype="multipart/form-data" action="form9a.php" > | ||
| + | <table> | ||
| + | <tr> | ||
| + | <td>Email: </td> | ||
| + | <td><input name="email" type="text" size="20" maxlength="199" value="<?= $_SESSION['email'] ?>" /></td></tr> | ||
| + | <tr> | ||
| + | <td>Име </td> | ||
| + | <td><input name="name" type="text" size="20" maxlength="60" value="<?= $_SESSION['name'] ?>" /></td></tr> | ||
| + | <tr> | ||
| + | <td>Коментар: </td><td><textarea name="comment" cols="43" rows="4"><?= $_SESSION['comment'] ?></textarea></td></tr> | ||
| + | <tr> | ||
| + | <tr> | ||
| + | <td>Картинка: </td><td><input type="file" name="image" title="Select Image"></td></tr> | ||
| + | |||
| + | <tr> | ||
| + | <td><img src="validationImage.class.php"></td> | ||
| + | <td><input name="code" type="text" maxlength="3" size="5" /> Въведете резултата! </td></tr> | ||
| + | <tr> | ||
| + | |||
| + | <tr> | ||
| + | |||
| + | <td colspan="2" align="center"><input type="submit" name="submit" id="ss" value="Изпрати"> | ||
| + | </td></tr></table> | ||
| + | </form> | ||
| + | </div> | ||
| + | |||
| + | </body> | ||
| + | </html> | ||
| + | </pre> | ||
| + | |||
| + | === обработка === | ||
| + | <pre> | ||
| + | <?php | ||
| + | session_start(); | ||
| + | |||
| + | if (isset($_POST['submit'])) | ||
| + | { | ||
| + | |||
| + | $_SESSION['email'] = $_POST['email']; | ||
| + | $_SESSION['name'] = $_POST['name']; | ||
| + | $_SESSION['comment'] = $_POST['comment']; | ||
| + | |||
| + | if(!($_POST['code'] == $_SESSION['rand_code'][0] && $_SESSION['rand_code'][0])) | ||
| + | { | ||
| + | header("Location: form9.php?errorList=Грешна сметка!<br />"); | ||
| + | exit(); | ||
| + | } | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | $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 /> wrong data transfer"; | ||
| + | } | ||
| + | |||
| + | if ( !$errorList && $photoSize > $maxSize*1024) | ||
| + | { | ||
| + | $errorList = "<br>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>$ 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 = 125; | ||
| + | $x = 140; | ||
| + | $y = 130; | ||
| + | |||
| + | 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"; | ||
| + | |||
| + | } | ||
| + | else header("Location: form9.php?errorList=$errorList"); | ||
| + | } | ||
| + | else header("Location: form9.php"); /* Redirect browser */ | ||
| + | ?> | ||
| + | <html> | ||
| + | <body> | ||
| + | <?php print $errorList ?> | ||
| + | <img src="../user/test.JPG" /> | ||
| + | |||
| + | </body> | ||
| + | </html> | ||
| + | |||
| + | </pre> | ||
| + | [[Category:PHP]] | ||
Latest revision as of 13:56, 8 November 2015
Contents
HTML форма
<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 "POST ->";
print_r( $_POST);
print "FILES ->";
print_r($_FILES);
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>
Прехвърляне данни между скриптове
форма
<html>
<body>
<?php
print "GET ->";
print_r( $_GET);
if(isset($_GET['errorList']))
{
print $_GET['errorList'];
};
?>
<div style="position:relative; top:100px; left:100px">
<form method="post" name="comment" enctype="multipart/form-data" action="form7a.php" >
<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>
обработка
<?php
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);
$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 /> wrong data transfer";
}
if ( !$errorList && $photoSize > $maxSize*1024)
{
$errorList = "<br>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>$ 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";
}
else header("Location: form7.php?errorList=$errorList");
}
else header("Location: form7.php"); /* Redirect browser */
?>
<html>
<body>
<?php print $errorList ?>
<img src="../user/test.JPG" />
</body>
</html>
Сесии
форма
<html>
<body>
<?php
error_reporting( E_ALL ^ E_NOTICE);
session_start();
if(isset($_GET['errorList']))
{
print $_GET['errorList'];
};
?>
<div style="position:relative; top:100px; left:100px">
<form method="post" name="comment" enctype="multipart/form-data" action="form8a.php" >
<table>
<tr>
<td>Email: </td>
<td><input name="email" type="text" size="20" maxlength="199" value="<?= $_SESSION['email'] ?>" /></td></tr>
<tr>
<td>Име </td>
<td><input name="name" type="text" size="20" maxlength="60" value="<?= $_SESSION['name'] ?>" /></td></tr>
<tr>
<td>Коментар: </td><td><textarea name="comment" cols="43" rows="4"><?= $_SESSION['comment'] ?></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>
обработка
<?php
session_start();
if (isset($_POST['submit']))
{
$_SESSION['email'] = $_POST['email'];
$_SESSION['name'] = $_POST['name'];
$_SESSION['comment'] = $_POST['comment'];
$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 /> wrong data transfer";
}
if ( !$errorList && $photoSize > $maxSize*1024)
{
$errorList = "<br>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>$ 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 = 125;
$x = 140;
$y = 130;
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";
}
else header("Location: form8.php?errorList=$errorList");
}
else header("Location: form8.php"); /* Redirect browser */
?>
<html>
<body>
<?php print $errorList ?>
<img src="../user/test.JPG" />
</body>
</html>
CAPTCHA
генератор на картинки
<?php
session_start();
class ImageGenerator{
public $inputString;
private $width;
private $height;
private $bgColor;
private $textColor;
private $img;
// initialize input arguments
public function __construct($inputString='Default Input String',$width=140,$height=40)
{
$this->inputString=$inputString;
$this->width=$width;
$this->height=$height;
$this->bgColor=array(rand(0,128), rand(0,128), rand(0,128));
$this->textColor=array(rand(128,255), rand(128,255), rand(128,255));
}
// create image stream
private function buildImageStream()
{
if(!$this->img=imagecreate($this->width,$this->height))
{
throw new Exception('Error creating image stream');
}
// allocate background color on image stream
imagecolorallocate($this->img,$this->bgColor[0],$this->bgColor[1],$this->bgColor[2]);
// allocate text color on image stream
$textColor=imagecolorallocate($this->img,$this->textColor[0],$this->textColor[1],$this->textColor[2]);
$font = "./VeraSe.ttf";
$font_size = 16;
$angle = 0;
$x = (int)($this->width/2-strlen($this->inputString)*5);
$y = (int)($this->height/2+9);
imagettftext( $this->img, $font_size, $angle, $x, $y, $textColor, $font, $this->inputString);
for ($i = 1; $i<3; $i++)
{
$a = rand(1, $this->width-1);
$b = rand(1, $this->width-1);
//imageline ( $this->img , $a , 0 , $b , $this->height-1 , $textColor);
//imageline ( $this->img , $a-1 , 0 , $b-1 , $this->height-1 , $textColor);
}
for ($i = 1; $i<3; $i++)
{
$a = rand( 1,$this->height-1);
$b = rand( 1,$this->height-1);
//imageline ( $this->img , 0, $a, $this->width-1, $b, $textColor);
//imageline ( $this->img , 0, $a+1, $this->width-1, $b+1, $textColor);
}
}
// display image stream on the browser
public function displayImage()
{
$this->buildImageStream();
header("Content-type: image/jpeg");
// display image
imagejpeg($this->img);
// free up memory
imagedestroy($this->img);
}
public function randStr()
{
for ($i = 0; $i < 5; $i++) {
// this numbers refer to numbers of the ascii table (small-caps)
$str .= strtoupper(chr(rand(97, 122)));
}
return $str;
}
public function randMath()
{
$a = rand(1,11);
$b = rand(1,11);
$d = rand(11,21);
$c = rand(0,1);
if ($c)
{
$str = "$a + $b = ?"; //string kojto se izvezhda
$c = $a+$b; // string s kojtose sravnqva
}
else
{
$str = "$a * $b = ?";
$c = $a*$b;
}
$res = array ($c, $str);
return $res;
}
}
// display sample input string
try{
// create new instance of 'ImageGenerator' class
$imgGen=new ImageGenerator();
// display image stream on the browser
$_SESSION['rand_code'] = $imgGen->randMath(); //dobaveno ot ilianko
$imgGen->inputString = $_SESSION['rand_code'][1]; //string , kojto se izvezhda v kartinkata
//print $imgGen->inputString;
$imgGen->displayImage();
}
catch(Exception $e)
{
echo $e->getMessage();
exit();
}
?>
форма
<html>
<body>
<?php
error_reporting( E_ALL ^ E_NOTICE);
session_start();
if(isset($_GET['errorList']))
{
print $_GET['errorList'];
};
?>
<div style="position:relative; top:100px; left:100px">
<form method="post" name="comment" enctype="multipart/form-data" action="form9a.php" >
<table>
<tr>
<td>Email: </td>
<td><input name="email" type="text" size="20" maxlength="199" value="<?= $_SESSION['email'] ?>" /></td></tr>
<tr>
<td>Име </td>
<td><input name="name" type="text" size="20" maxlength="60" value="<?= $_SESSION['name'] ?>" /></td></tr>
<tr>
<td>Коментар: </td><td><textarea name="comment" cols="43" rows="4"><?= $_SESSION['comment'] ?></textarea></td></tr>
<tr>
<tr>
<td>Картинка: </td><td><input type="file" name="image" title="Select Image"></td></tr>
<tr>
<td><img src="validationImage.class.php"></td>
<td><input name="code" type="text" maxlength="3" size="5" /> Въведете резултата! </td></tr>
<tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" id="ss" value="Изпрати">
</td></tr></table>
</form>
</div>
</body>
</html>
обработка
<?php
session_start();
if (isset($_POST['submit']))
{
$_SESSION['email'] = $_POST['email'];
$_SESSION['name'] = $_POST['name'];
$_SESSION['comment'] = $_POST['comment'];
if(!($_POST['code'] == $_SESSION['rand_code'][0] && $_SESSION['rand_code'][0]))
{
header("Location: form9.php?errorList=Грешна сметка!<br />");
exit();
}
$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 /> wrong data transfer";
}
if ( !$errorList && $photoSize > $maxSize*1024)
{
$errorList = "<br>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>$ 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 = 125;
$x = 140;
$y = 130;
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";
}
else header("Location: form9.php?errorList=$errorList");
}
else header("Location: form9.php"); /* Redirect browser */
?>
<html>
<body>
<?php print $errorList ?>
<img src="../user/test.JPG" />
</body>
</html>