Jump to content

Help with image resize


stelthius

Recommended Posts

hello guys,

 

In my avatar system i make it resize the avatar, its resizing the avatar but its making the avatar thinner than its supposed leaving a black thickness on the left and right side of the image see screenshot for example,

 

here is my code i cant see what im doing wrong so if anyone can point it out id be greatfull, image is supposed to be resized to 100 x 100.

 

avatar.jpg

 

/* Avatar checking */
$field = "avatar"; //Use field name for avatar
$uploaded_dir = "/home/wownetwo/public_html/include/avatar/$subuser/";
$avatar_path = $uploaded_dir . $avatar_name;
$width = 100;
$height = 100;
$thumbsize = 100;

if(empty($avatar_name)){
$avatar_name = 'noavatar.png';
$avatar_path = "include/avatar/noavatar.png";
mkdir("/home/wownetwo/public_html/include/avatar/$subuser", 0777);
$destination = "include/avatar/$subuser/avatar.png";
if(!(copy($avatar_path, $destination))) {
echo "The specified file could not be copied. Please try again.", "\n";
}

} else {
if(!$avatar_tmpname){
$form->setError($field, "* Avatar does not exist");
} else {
$num_char=strlen($avatar_name);
if($num_char > 20){
$form->setError($field, "* Your Avatar name has $num_char characters, the limit is 20");
} else {
if($avatar_size > 102400) {
$form->setError($field, "* Your avatar is $avatar_size, the maximum upload size is 100kb");
} else if($avatar_type == "image/jpeg"){
mkdir("/home/wownetwo/public_html/include/avatar/$subuser", 0777);

/* Resize Image */
list($width_orig, $height_orig) = getimagesize($avatar_tmpname);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
/* Done Resize Image */

/* Resize Image */
$image_p = imagecreatetruecolor($thumbsize, $thumbsize);
$image = imagecreatefromjpeg($avatar_tmpname);
imagecopyresampled($image_p, $image, -($width/2) + ($thumbsize/2), -($height/2) + ($thumbsize/2), 0, 0, $width, $height, $width_orig, $height_orig);

imagejpeg($image_p, $avatar_tmpname, 100);
/* Done Resize Image */

move_uploaded_file($avatar_tmpname, "$avatar_path");
} else if($avatar_type == "image/pjpeg"){
mkdir("/home/wownetwo/public_html/include/avatar/$subuser", 0777);

/* Resize Image */
list($width_orig, $height_orig) = getimagesize($avatar_tmpname);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
/* Done Resize Image */

/* Resize Image */
$image_p = imagecreatetruecolor($thumbsize, $thumbsize);
$image = imagecreatefromjpeg($avatar_tmpname);
imagecopyresampled($image_p, $image, -($width/2) + ($thumbsize/2), -($height/2) + ($thumbsize/2), 0, 0, $width, $height, $width_orig, $height_orig);

imagejpeg($image_p, $avatar_tmpname, 100);
/* Done Resize Image */

move_uploaded_file($avatar_tmpname, "$avatar_path");
} else if($avatar_type == "image/png"){
mkdir("/home/wownetwo/public_html/include/avatar/$subuser", 0777);

/* Resize Image */
list($width_orig, $height_orig) = getimagesize($avatar_tmpname);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
/* Done Resize Image */

/* Resize Image */
$image_p = imagecreatetruecolor($thumbsize, $thumbsize);
$image = imagecreatefrompng($avatar_tmpname);
imagecopyresampled($image_p, $image, -($width/2) + ($thumbsize/2), -($height/2) + ($thumbsize/2), 0, 0, $width, $height, $width_orig, $height_orig);

imagepng($image_p, $avatar_tmpname, 100);
/* Done Resize Image */

move_uploaded_file($avatar_tmpname, "$avatar_path");
} else if($avatar_type == "image/gif"){
mkdir("/home/wownetwo/public_html/include/avatar/$subuser", 0777);

/* Resize Image */
list($width_orig, $height_orig) = getimagesize($avatar_tmpname);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
/* Done Resize Image */

/* Resize Image */
$image_p = imagecreatetruecolor($thumbsize, $thumbsize);
$image = imagecreatefromgif($avatar_tmpname);
imagecopyresampled($image_p, $image, -($width/2) + ($thumbsize/2), -($height/2) + ($thumbsize/2), 0, 0, $width, $height, $width_orig, $height_orig);

imagegif($image_p, $avatar_tmpname, 100);
/* Done Resize Image */

move_uploaded_file($avatar_tmpname, "$avatar_path");
} else {
$form->setError($field, "* $avatar_type is wrong extension");
}
}
}
}

 

 

Any help and or advice is great lappretiated, thanks rick

Link to comment
https://forums.phpfreaks.com/topic/147975-help-with-image-resize/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.