php resize image after upload and crop it to the center
i have a user profile in php and i want to give users the choice of changing their profile picture. But when they submit their new picture via $_POST i want the picture to be resized to:
height:110px | width:relevant to the height (if the width is bigger than height)
width:110px | height:relevant to the width (if the height is bigger than width)
when the resize is done, i want to crop the picture so it becomes 110px x 110px but i want it to be centered.
For example if the user uploads a picture with 110px width and 200px height (dimensions after the resize) the new image after crop will be 110x110 cropped by 90px from right. What i want is to cropped 45px from left and another 45px from right so it will be centered
the function will accept .png
, .gif
and .jpg
images and will save the new image only in jpg format no matter what the initial format was.
I searched a lot to create such a function and i found an answer but any time i atempt to change some minor detail everything stop working properly.
My code so far:
<?php
$userfile_name = $_FILES["sgnIMG"]["name"];
$userfile_tmp = $_FILES["sgnIMG"]["tmp_name"];
$userfile_size = $_FILES["sgnIMG"]["size"];
$filename = basename($_FILES["sgnIMG"]["name"]);
$file_ext = substr($filename, strrpos($filename, ".") + 1);
$large_image_location = $target_path . 开发者_运维知识库$filename;
$ext = '';
if ($file_ext == 'jpg') {
$ext = 1;
} else if ($file_ext == 'gif') {
$ext = 2;
} else if ($file_ext == 'png') {
$ext = 3;
} else {
$ext = 0;
}
$target = $target_path . basename($_FILES["sgnIMG"]["name"]);
if (move_uploaded_file($userfile_tmp, $target)) {
$newImg = resize110($target, $ext);
if (isset($_POST['imupd']) && ($_POST['imupd'] == 'up')) {
$sql = "UPDATE users SET avatar='" . str_replace('im/users/', '', $newImg) . "' WHERE id=" . $_SESSION['sesID'] . "";
$result = mysql_query($sql);
if ($result) {
echo '<img src="' . $newImg . '" width="110" title="' . $file_ext . '"/>';
} else {
echo '<img src="im/avatars/px.png" width="110" title="' . $file_ext . '"/>';
}
}
} else {
}
function getHeight($image)
{
$sizes = getimagesize($image);
$height = $sizes[1];
return $height;
}
function getWidth($image)
{
$sizes = getimagesize($image);
$width = $sizes[0];
return $width;
}
function resize110($image, $ext)
{
chmod($image, 0777);
$oldHeight = getHeight($image);
$oldWidth = getWidth($image);
if ($oldHeight < $oldWidth) {
$newImageHeight = 110;
$newImageWidth = ceil((110 * $oldWidth) / $oldHeight);
imagecopyresampled($newImage, $source, -ceil(($newImageWidth - 110) / 2), 0, 0, 0, $newImageWidth, $newImageHeight, $oldWidth, $oldHeight);
} else {
$newImageHeight = ceil((110 * $oldHeight) / $oldWidth);
$newImageWidth = 110;
imagecopyresampled($newImage, $source, 0, -ceil(($newImageHeight - 110) / 2), 0, 0, $newImageWidth, $newImageHeight, $oldWidth, $oldHeight);
}
$newImage = imagecreatetruecolor(110, 110);
chmod($image, 0777);
return $image;
switch ($ext) {
case 1;
$source = imagecreatefromjpeg($image);
break;
case 2;
$source = imagecreatefromgif($image);
break;
case 3;
$source = imagecreatefrompng($image);
break;
}
imagejpeg($newImage, $image, 90);
return $image;
}
I looked around a lot and combined different parts of code i found. So this script will take a jpg,gif of png image, resize it to 110px width if width is greater of 110px height if height is greater. The ascpect ratio will remain so the remaining pixels will be divided by 2 will be used to center the image.
for a different size just change 110 everywhere.
==================================================================================
<?php
// pfpic -> the name of the <input type="file" name="pfpic"/> where user chooses file
$target_path = "im/users/"; // the directory to store the uploaded and then resampled image
$userfile_name = $_FILES["pfpic"]["name"]; // the name that the image file will have once uploaded
$userfile_tmp = $_FILES["pfpic"]["tmp_name"]; // the temporary name the server uses to store the file
$userfile_size = $_FILES["pfpic"]["size"]; // the size of the file that we want to upload
$filename = basename($_FILES["pfpic"]["name"]); // the full name of the file
$file_ext = substr($filename, strrpos($filename, ".") + 1); // the file extension
$large_image_location = $target_path.$filename; // the full path to the file
$ext='';
if($file_ext=='jpg')
{
$ext=1;
}
else if ($file_ext=='gif')
{
$ext=2;
}
else if ($file_ext=='png')
{
$ext=3;
}
else
{
$ext=0;
}
$target = $target_path.basename(sha1($_SESSION['sesID']).'.'.'jpg');
if($ext!=0)
{
if(move_uploaded_file($userfile_tmp,$target))
{
$newImg=resize110($target,$ext);
echo '<img src="'.$newImg.'"/>';
}
else
{
echo 'the file could not be uploaded, please try again';
}
}
else
{
echo 'this file extension is not accepted, please use "jpg", "gif" or "png" file formats';
}
function getHeight($image)
{
$sizes = getimagesize($image);
$height = $sizes[1];
return $height;
}
function getWidth($image)
{
$sizes = getimagesize($image);
$width = $sizes[0];
return $width;
}
function resize110($image,$ext)
{
chmod($image, 0777);
$oldHeight=getHeight($image);
$oldWidth=getWidth($image);
switch ($ext)
{
case 1;
$source = imagecreatefromjpeg($image);
break;
case 2;
$source = imagecreatefromgif($image);
break;
case 3;
$source = imagecreatefrompng($image);
break;
}
$newImage = imagecreatetruecolor(110,110);
$bgcolor = imagecolorallocate($newImage, 255, 255, 255);
imagefill($newImage, 0, 0, $bgcolor); // use this if you want to have a white background instead of black
// we check tha width and height and then we crop the image to the center
if($oldHeight<$oldWidth)
{
$newImageHeight = 110;
$newImageWidth = ceil((110*$oldWidth)/$oldHeight);
imagecopyresampled($newImage,$source,-ceil(($newImageWidth-110)/2),0,0,0,$newImageWidth,$newImageHeight,$oldWidth,$oldHeight);
}
else
{
$newImageHeight = ceil((110*$oldHeight)/$oldWidth);
$newImageWidth = 110;
imagecopyresampled($newImage,$source,0,-ceil(($newImageHeight-110)/2),0,0,$newImageWidth,$newImageHeight,$oldWidth,$oldHeight);
}
//we save the image as jpg resized to 110x110 px and cropped to the center. the old image will be replaced
imagejpeg($newImage,$image,90);
return $image;
}
?>
精彩评论