Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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/6...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

    PHP Code:
                $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";
                }
                } 
    Last edited by shannont; 02-22-2012 at 01:49 PM.

  • #2
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

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

  • #3
    Regular Coder
    Join Date
    Apr 2004
    Posts
    298
    Thanks
    0
    Thanked 23 Times in 23 Posts
    might glean some info here...
    http://www.nstoia.com/sat/crop/
    and here...
    http://www.nstoia.com/sat/resize/


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •