JSFiddle - React, Tailwind, and code Playground

by Ivan

JavaScript

<!DOCTYPE html>

<title>Загрузка аватара|<?php echo $log_username; ?></title>
<link rel="stylesheet" type="text/css" href="css/imgareaselect-default.css"> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript" src="jquery.imgareaselect.pack.js"></script>
<?php include_once("template_pageTop.php"); ?>
<img id="photo" src="user/<?php echo $log_username;?>/<?php echo $avatar;?>" alt="" title="" style="margin: 0 0 0 10px;" />

<form action="crop.php" method="post">
    <input type="hidden" name="x1" value="" />
    <input type="hidden" name="y1" value="" />
    <input type="hidden" name="x2" value="" />
    <input type="hidden" name="y2" value="" />
    <input type="hidden" name="w" value="" />
    <input type="hidden" name="h" value="" />
    <input type="submit" value="Отправить" />
</form>

<script type="text/javascript"> 
var heo = $('#photo').width();
var wio = $('#photo').height();
function preview(img, selection) {
    var scaleX = 100 / (selection.width || 1);
    var scaleY = 100 / (selection.height || 1);
    $('#photo + div > img').css({
        width: Math.round(scaleX * heo) + 'px',
        height: Math.round(scaleY * wio) + 'px',
        marginLeft: '-' + Math.round(scaleX * selection.x1) + 'px',
        marginTop: '-' + Math.round(scaleY * selection.y1) + 'px'
    });
}

$(document).ready(function () {
    $('<div><img src="user/<?php echo $log_username;?>/<?php echo $avatar;?>" style="position: relative;" /><div>') .css({
        float: 'left',
        overflow: 'hidden',
        width: '100px',
        height: '100px'
    }) .insertAfter($('#photo')); 

    $('#photo').imgAreaSelect({
        aspectRatio: '1:1',
        handles: true,
        onSelectChange: preview,
        onSelectEnd: function ( image, selection ) {
            $('input[name=x1]').val(selection.x1);
            $('input[name=y1]').val(selection.y1);
            $('input[name=x2]').val(selection.x2);
           ...