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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.