How to have my PHP script make images square, while maintaining their original ratio? -



How to have my PHP script make images square, while maintaining their original ratio? -

i have been developing website time now, , looking create improve 1 little detail. currently, when user creates , account, can view "my page" , sorts of things, including alter profile image default 1 have provided, whatever want. unfortunately, if upload image ratio of greater or less 1:1, i.e. 300x200, script re-size fit avatar form, keeps ratio, , makes image float top of box. here image of avatar not have 1:1 ratio:

as can see, image describe above. time makes perfect square when image user uploads has 1:1 ratio. here example:

obviously, unreasonable expect user upload 1:1 image. looking help me tweak script, provide, on avatar uploads, if image has 1:1 ratio, pretty much mount image black "canvas", lack of improve wording, , center image on canvas both vertically , horizontally. here mean, using image edited in photoshop give black canvas:

as can see, profile image in lastly image need script do. provide script:

php

<?php if (isset($_files["avatar"]["name"]) && $_files["avatar"]["tmp_name"] != ""){ $filename = $_files["avatar"]["name"]; $filetmploc = $_files["avatar"]["tmp_name"]; $filetype = $_files["avatar"]["type"]; $filesize = $_files["avatar"]["size"]; $fileerrormsg = $_files["avatar"]["error"]; $kaboom = explode(".", $filename); $fileext = end($kaboom); list($width, $height) = getimagesize($filetmploc); if($width < 10 || $height < 10){ header("location: ../message.php?msg=error: image has no dimensions"); exit(); } $db_file_name = rand(100000000000,999999999999).".".$fileext; if($filesize > 2048576) { header("location: ../message.php?msg=error: image file larger 2mb"); exit(); } else if (!preg_match("/\.(gif|jpg|png)$/i", $filename) ) { header("location: ../message.php?msg=error: image file not jpg, gif or png type"); exit(); } else if ($fileerrormsg == 1) { header("location: ../message.php?msg=error: unknown error occurred"); exit(); } $sql = "select avatar users username='$log_username' limit 1"; $query = mysqli_query($db_conx, $sql); $row = mysqli_fetch_row($query); $avatar = $row[0]; if($avatar != ""){ $picurl = "../user/$log_username/$avatar"; if (file_exists($picurl)) { unlink($picurl); } } $moveresult = move_uploaded_file($filetmploc, "../user/$log_username/$db_file_name"); if ($moveresult != true) { header("location: ../message.php?msg=error: file upload failed"); exit(); } include_once("../php_includes/image_resize.php"); $target_file = "../user/$log_username/$db_file_name"; $resized_file = "../user/$log_username/$db_file_name"; $wmax = 200; $hmax = 300; img_resize($target_file, $resized_file, $wmax, $hmax, $fileext); $sql = "update users set avatar='$db_file_name' username='$log_username' limit 1"; $query = mysqli_query($db_conx, $sql); mysqli_close($db_conx); header("location: ../user.php?u=$log_username"); exit(); } ?>

thanks in advance, help much appreciated!

insert image background instead of img tag, can position center.

<div class="avatar"></div> .avatar { width: 200px; height: 200px; background-image: url("avatar.png"); background-repeat: no-repeat; background-position: center center; border-radius: 50%; // if want circle }

php image

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -