/    Sign up×
Community /Pin to ProfileBookmark

image upload & resize: how to make the it centered and square?

I have an image upload script that resizes the uploaded image to 150×150 pixels. That’s great if the image is square, but if someone uploads an image with let’s say 640×200 pixels, it doesn’t look pretty.

So I basically need it to automatically create a squared based on the center of the image. If the image is wider it should crop off the left and right sides. If the image is higher, it should crop off the top and bottom.

I found a code modification online, here to be exact:
[url]http://stackoverflow.com/questions/6500206/upload-resize-and-crop-center-of-image-with-php[/url]

I’m not great with PHP and I’ve been at this for a few hours now, trying to combine my code below with the option above. If anyone could help me that would be great ?

[code=php]
$target_path = “avatars/”;
$image = $_FILES[‘uploadedfile’][‘name’];
$target_path = $target_path . basename( $_FILES[‘uploadedfile’][‘name’]);
$_POST[“userpic”]=$_FILES[‘uploadedfile’][‘name’];
if($_FILES[‘uploadedfile’][‘tmp_name’]!=””) {
$imagetype=explode(“.”,$_POST[“userpic”]);
if($imagetype[1]==”jpg” || $imagetype[1]==”JPG” || $imagetype[1]==”gif” || $imagetype[1]==”GIF”)
{
$target_path = “avatars/”;
$thaid=$_POST[“user_id”];
$target_path = $target_path .$thaid.”.”.$imagetype[1];
$target_path2 =$thaid.”.”.$imagetype[1];
move_uploaded_file($_FILES[‘uploadedfile’][‘tmp_name’], $target_path);
$_POST[“userpic”]=$target_path2;
$n_width=$setts[‘avatar_width’];
$n_height=$setts[‘avatar_height’];
$tsrc=$target_path;
$add=$target_path;
if($imagetype[1]==”jpg” || $imagetype[1]==”JPG”)
{
$im=imagecreatefromjpeg($add);
$width=imagesx($im);
$height=imagesy($im);
$newimage=imagecreatetruecolor($n_width,$n_height);
imagecopyresized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
imagejpeg($newimage,$tsrc);
}
if($imagetype[1]==”gif” || $imagetype[1]==”GIF”)
{
$im=imagecreatefromgif($add);
$width=imagesx($im);
$height=imagesy($im);
$newimage=imagecreatetruecolor($n_width,$n_height);
imagecopyresized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
imagegif($newimage,$tsrc);
}
}
else
{
$_POST[“userpic”]=”noimage.jpg”;
}
}
[/code]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@shannontauthorFeb 22.2012 — After a few hours of trying, I got it working somewhat, but there's still something wrong. The image thumbnail is now giving me a black space on the right instead of being centered. Any ideas? ?

[code=php]
$target_path = "avatars/";
$image = $_FILES['uploadedfile']['name'];
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
$_POST["userpic"]=$_FILES['uploadedfile']['name'];
if($_FILES['uploadedfile']['tmp_name']!="") {
$imagetype=explode(".",$_POST["userpic"]);

if($imagetype[1]=="jpg" || $imagetype[1]=="JPG" || $imagetype[1]=="gif" || $imagetype[1]=="GIF")
{
$target_path = "avatars/";
$thaid=$_POST["user_id"];
$target_path = $target_path .$thaid.".".$imagetype[1];
$target_path2 =$thaid.".".$imagetype[1];
move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path);
$_POST["userpic"]=$target_path2;
$n_width=$setts['avatar_width'];
$n_height=$setts['avatar_height'];
$tsrc=$target_path;
$add=$target_path;

list ($ow, $oh) = getimagesize ($tsrc);
if ($ow > $oh) {
$off_w = ($ow-$oh)/2;
$off_h = 0;
$ow = $oh;
} elseif ($oh > $ow) {
$off_w = 0;
$off_h = ($oh-$ow)/2;
$oh = $ow;
} else {
$off_w = 0;
$off_h = 0;
}

if($imagetype[1]=="jpg" || $imagetype[1]=="JPG")
{
$im=imagecreatefromjpeg($add);
$width=imagesx($im);
$height=imagesy($im);
$newimage=imagecreatetruecolor($n_width,$n_height);
imagecopyresized($newimage,$im,0,0,$off_w, $off_h,$n_width,$n_height,$width,$height);
imagejpeg($newimage,$tsrc);
}
if($imagetype[1]=="gif" || $imagetype[1]=="GIF")
{
$im=imagecreatefromgif($add);
$width=imagesx($im);
$height=imagesy($im);
$newimage=imagecreatetruecolor($n_width,$n_height);
imagecopyresized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
imagegif($newimage,$tsrc);
}
}
else
{
$_POST["userpic"]="noimage.jpg";
}
}
[/code]
Copy linkTweet thisAlerts:
@NogDogFeb 23.2012 — For whatever it's worth, here's the function I used here:
[code=php]
/**
* Resize image to specific dimension, cropping as needed
* @return resource Resized image resource, or boolean false on failure
* @param string $imgFile Path to image to be resized
* @param int $width
* @param int $height
* @param string $error Error message
*/
function resize($imgFile, $width, $height, &$error = null)
{
$attrs = @getimagesize($imgFile);
if($attrs == false)
{
$error = "Uploaded file does not appear to be an image.";
return false;
}
if($attrs[0] * $attrs[1] > 3000000)
{
$error = "Max pixels allowed is 3,000,000. Your {$attrs[0]} x " .
"{$attrs[1]} image has " . $attrs[0] * $attrs[1] . " pixels.";
return false;
}
$ratio = (($attrs[0] / $attrs[1]) < ($width / $height)) ?
$width / $attrs[0] : $height / $attrs[1];
$x = max(0, round($attrs[0] / 2 - ($width / 2) / $ratio));
$y = max(0, round($attrs[1] / 2 - ($height / 2) / $ratio));
switch($attrs[2]) {
case IMAGETYPE_JPEG:
$src = @imagecreatefromjpeg($imgFile);
break;
case IMAGETYPE_PNG:
$src = @imagecreatefrompng($imgFile);
break;
case IMAGETYPE_GIF:
$src = @imagecreatefromgif($imgFile);
break;
default:
$error = "File is not a JPEG, PNG, or GIF image";
return false;
}
if($src == false)
{
$error = "An error occurred processing the file, it may have been corrupted.";
return false;
}
$resized = imagecreatetruecolor($width, $height);
$result = imagecopyresampled($resized, $src, 0, 0, $x, $y, $width, $height,
round($width / $ratio, 0), round($height / $ratio));
if($result == false)
{
$error = "Error trying to resize and crop image.";
return false;
}
else
{
return $resized;
}
}
[/code]
×

Success!

Help @shannont spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...