Results 1 to 3 of 3

Thread: Need Help with image Resize

  1. #1
    Join Date
    Aug 2009
    Location
    Florida
    Posts
    23
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Need Help with image Resize

    I have a script that works for my website but, I'm not sure how to resize the image. I have tried to figure this out for some time now, but I'm just not having any luck. I would like the image to be resized to 100X100... I have posted the code below and please keep in mind that I'm very new to php... Thanks in advance for any help!

    PHP Code:
    <?

    include_once "accesscontrol.php";

        
    $bdir="../member_image/";


    if (isset(
    $submitok) && $_FILES[userfile][size] > '0')
    {
       if (empty(
    $image_name))
       {
        echo 
    "<center><span class=TNA>You left blank some of the required fields. </span></center>";
       }
        else
        {  
            
    $image_id time()."_".$_FILES[userfile][name];
          
    copy($_FILES[userfile][tmp_name], "../member_image/$image_id");    

                       
       
         
    $q2 "update members_info set
            
            image_name = \"
    $_POST[image_name]\",
            image_id = \"
    $image_id\"

            where uname = '
    $_SESSION[uname]' ";


            
    $r2 mysql_query($q2) or die(mysql_error());
                    
                     
    header("location:s_upload.php");
                    exit();

            }

    }

    unset(
    $submitok);
    unset(
    $userfile);



    ?>
    Last edited by Snookerman; 08-26-2009 at 07:08 AM.

  2. #2
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    You'd need to use the GD library for that. Be sure you have GD enabled/installed on your server, otherwise this will not work. Additionally, be sure that "accesscontrol.php" doesn't call any headers before hand, otherwise, the image resizing functinos for the imagejpeg header will not work either, and you'll return errors.

    Code:

    PHP Code:
    <?php

    include_once "accesscontrol.php";


    $bdir="../member_image/";


    if (isset(
    $submitok) && $_FILES[userfile][size] > '0')
    {
    if (empty(
    $image_name))
    {
    echo 
    "<center><span class=TNA>You left blank some of the required fields. </span></center>";
    }
    else

    $image_id time()."_".$_FILES[userfile][name];
    copy($_FILES[userfile][tmp_name], "../member_image/$image_id"); 

    $filename $_FILES[userfile][tmp_name];
    $newname "thumb".$filename;

    header('Content-type: image/jpeg');

    list(
    $width$height) = getimagesize($filename);
    $newwidth 100;
    $newheight 100;

    $thumb imagecreatetruecolor($newwidth$newheight);
    $source imagecreatefromjpeg($filename);

    imagecopyresized($thumb$source0000$newwidth$newheight$width$height);
    imagejpeg($thumb$newname);
    imagedestroy($thumb);

    unlink($filename);
    rename($newname$filename); 


    $q2 "update members_info set

    image_name = \"
    $_POST[image_name]\",
    image_id = \"
    $image_id\"

    where uname = '
    $_SESSION[uname]' ";


    $r2 mysql_query($q2) or die(mysql_error());

    header("location:s_upload.php");
    exit();

    }

    }

    unset(
    $submitok);
    unset(
    $userfile);



    ?>
    HTH
    - Josh

  3. #3
    Join Date
    Mar 2009
    Location
    NJ, USA
    Posts
    32
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    where you display the code adjust the image source.

    <img src="image.jpg" width="X" height="X">

    Why include this line?
    $bdir="../member_image/";

    For one of my sites the image is called like this:

    Code:
    //If image display is confirmed
    if ($_SESSION['imageconfirm'] == '1') 
    {
    //display image with given size
    echo '<img width="175" height="175" src="' . $_SESSION['image'] . '">'; 
    }
    I think this is the line your looking for:
    echo '<img width="175" height="175" src="' . $_SESSION['image'] . '">';

    There are nicer ways to do this that are beyond me. Ways to avoid image distortion by scaling the size of the image within certain limits. We'll need a better programmer then myself for that however.

    Best of luck.

Bookmarks

Posting Permissions

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