...

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



shannont
02-22-2012, 01:44 PM
I have an image upload script that resizes the uploaded image to 150x150 pixels. That's great if the image is square, but if someone uploads an image with let's say 640x200 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:
http://stackoverflow.com/questions/6500206/upload-resize-and-crop-center-of-image-with-php

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 :)



$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";
}
}

shannont
02-22-2012, 11:09 PM
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? :(



$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";
}
}

litebearer
02-23-2012, 12:03 AM
might glean some info here...
http://www.nstoia.com/sat/crop/
and here...
http://www.nstoia.com/sat/resize/



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum