Здравствуйте, у меня такая проблема делаю загрузку аватара на сайте, для начала нашел простой код где просто картинка загружается на сервер, вот код PHP: require_once '../_scripts/database.php'; $link = db_connect(); $id = (int)$_POST['id'];// user id if (isset($_POST['submit'])) { $file = $_FILES['file']; $fileName = $file['name']; $fileTmpName = $file['tmp_name']; $fileSize = $file['size']; $fileError = $file['error']; $fileType = $file['type']; $fileExt = explode('.', $fileName); $fileActualExt = strtolower(end($fileExt)); $allowed = array('jpg'); if (in_array($fileActualExt, $allowed)) { if ($fileError === 0) { if ($fileSize < 1048576) { $fileNameNew = $id.".".$fileActualExt; $fileDestination = '../_images/avatar/'.$fileNameNew; move_uploaded_file($fileTmpName, $fileDestination); $sql = "UPDATE users SET avatar='$id' WHERE id='$id'"; $result = mysqli_query($link, $sql); header('Location: /profil'); } else { echo "Your file is too big!"; } } else { echo "There was an error uploading your file!"; } } else { echo "You cannot upload files of this type!"; } } При загрузке картинок с разными пропорциями, все это смотрится не красиво, надо было сделать так что бы картинка обрезалось, и стала квадратной форма, я нашел такой код, но не знаю как его вписать в код указанный выше вот сам код PHP: public function igImagePrepare($img,$name){ $dir = 'my-images/'; $img_name = $name.'-'.uniqid().'.jpg'; //Your Image $imgSrc = $img; //getting the image dimensions list($width, $height) = getimagesize($imgSrc); //saving the image into memory (for manipulation with GD Library) $myImage = imagecreatefromjpeg($imgSrc); $square_size = 100; $width = imagesx( $myImage ); $height = imagesy( $myImage ); //set dimensions if($width> $height) { $width_t=$square_size; //respect the ratio $height_t=round($height/$width*$square_size); //set the offset $off_y=ceil(($width_t-$height_t)/2); $off_x=0; } elseif($height> $width) { $height_t=$square_size; $width_t=round($width/$height*$square_size); $off_x=ceil(($height_t-$width_t)/2); $off_y=0; } else { $width_t=$height_t=$square_size; $off_x=$off_y=0; } /* Create the New Image */ $new = imagecreatetruecolor( $square_size , $square_size ); /* Transcribe the Source Image into the New (Square) Image */ $bg = imagecolorallocate ( $new, 255, 255, 255 ); imagefill ( $new, 0, 0, $bg ); imagecopyresampled( $new , $myImage , $off_x, $off_y, 0, 0, $width_t, $height_t, $width, $height ); //final output imagejpeg($new, $dir.$img_name); return $dir.$img_name; } Помогите пожалуйста решить это проблему!!!
В нете я смотрю не пробовал искать. Там материала валом разного. вот пример http://www.php.su/articles/?cat=graph&page=014