SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast redhead's Avatar
    Join Date
    Feb 2004
    Location
    Ontario
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Image Upload and Resize

    Hi Everyone

    I have a script here that will upload a image to the server.. what i want it to be able to do is resize it before it upload it so if i upload a image that is 500x500 it will be come 250x250, does that make since??

    Please help me i'm going crazy.

    Here is my upload Script
    PHP Code:
    <?php 
    //echo $imgfile_name;

    $dir="../testimages/";
    $ifile $dir $imgfile_name;

    $ext strrchr($imgfile_name,'.');

    echo 
    $ifile;

    if (isset(
    $submit))
    {
        
    $fileexists "n";

        while (
    $fileexists != "y")
        {   
         
    copy($imgfile,$ifile);
        
    $fileexists "y";
        }

        if (!
    is_uploaded_file ($imgfile))
        {
            echo 
    "<b>$imgfile_name</b> couldn't be copied !!";
        }
        
    }

    exec("mogrify -geometry 250x$tn_height! -format jpg $imgfile"); 


        echo 
    "<img src=../testimages/$imgfile_name>";
    ?>

  2. #2
    SitePoint Guru gavwvin's Avatar
    Join Date
    Nov 2004
    Location
    Cornwall, UK
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    By the way you can do this bit:

    $fileexists = "n";

    while ($fileexists != "y")
    {
    copy($imgfile,$ifile);
    $fileexists = "y";
    }

    if (!is_uploaded_file ($imgfile))
    {
    echo "<b>$imgfile_name</b> couldn't be copied !!";
    }

    much more easily by just using the function move_uploaded_file

    I'm not sure about using exec() to thumbnail an image, I always use the gd library and imagecopyresampled() to create a thumbnail. I can post the function if you still get no joy with this method.

  3. #3
    SitePoint Zealot ceefour's Avatar
    Join Date
    Feb 2005
    Location
    Bandung, Indonesia
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb

    Oh God. I think I won't be reading questions like this only once in my life.
    Well, this time it's better to have a code:

    PHP Code:
        /**
         * Uploads a photo for the specified user.
         *
         * This method does not delete the photo prior to uploading. It will throw a database error if the
         * uploaded photo already exists.
         *
         * @access public
         * @param string Username.
         * @param string File name.
         * @param string MIME type.
         */
        /*public*/ 
    function Upload($username$file$mime_type)
        {
            
    // sanity check
            
    if (!file_exists($file)) return CSF::raiseError("File '$file' does not exist.");
            
    // get size
            
    $stat stat($file);
            
    $size $stat['size'];
            
    // sanity check on size
            
    if ($size $this->MaxSize)
                return 
    Client::Retry("Your photo file size is too big (%d bytes). Maximum file size is %d bytes.",
                    
    $size$this->MaxSize);
            
    // get width & height
            
    $imgsize = @getimagesize($file);
            if (!
    $imgsize) return Client::Retry("Invalid photo file format.");
            if (
    $imgsize[2] != 2) return Client::Retry("We only accept files in JPEG format. Please upload a JPEG file.");
            
    $width $imgsize[0];
            
    $height $imgsize[1];
            
    // contents
            
    $contents file_get_contents($file);
            
    // thumbnail
            // calculate size first
            // calculate using t_height = ThumbHeight
            
    $t_height $this->ThumbHeight;
            
    $t_width round(($t_height $height) * $width);
            
    // if more than calculate using t_width = ThumbWidth
            
    if ($t_width $this->ThumbWidth) {
                
    $t_width $this->ThumbWidth;
                
    $t_height round(($t_width $width) * $height);
            }
            
    /* OLD: INCORRECT
            $ratio = $width / $height;
            $t_ratio = $this->ThumbWidth / $this->ThumbHeight;
            if ($ratio < $t_ratio) {
                $t_height = $this->ThumbHeight;
                $t_width = $this->ThumbWidth * ($ratio / $t_ratio);
            } else {
                $t_width = $this->ThumbWidth;
                $t_height = $this->ThumbHeight * ($ratio / $t_ratio);
            }*/
            // load orig file
            
    $orig = @imagecreatefromjpeg($file);
            if (!
    $orig) return CSF::raiseError("Invalid file format.");
            
    // create a new truecolor image
            
    $thumb imagecreatetruecolor($t_width$t_height);
            if (
    function_exists('imagecopyresampled')) {
                
    imagecopyresampled($thumb$orig0000$t_width$t_height$width$height);
            } else {
                
    imagecopyresized($thumb$orig0000$t_width$t_height$width$height);
            }
            
    ob_start();
            
    imagejpeg($thumb);
            
    $t_contents ob_get_contents();
            
    ob_end_clean();
            
    $t_size strlen($t_contents);
            
    $t_mime_type 'image/jpeg';
            include_once 
    'Util/DateTime.class.php';
            
    // insert into photo
            
    $db =& CSF::GetDBConn();
            
    $sql 'INSERT INTO photos (username, size, width, height, mime_type, contents, time_upload)
                VALUES (?, ?, ?, ?, ?, ?, ?)'
    ;
            
    $pq $db->prepare($sql);
            
    $params = array($username$size$width$height$mime_type$contentsDateTime::ToSQL());
            
    $res $db->executeParams($pqNULL$params);
            if (
    CSF::isError($res)) return CSF::raiseError("Cannot execute INSERT query.");
            
    $db->freePrepared($pq);
            
    // insert into photothumb
            
    $db =& CSF::GetDBConn();
            
    $sql 'INSERT INTO photothumbs (username, size, width, height, mime_type, contents, time_upload)
                VALUES (?, ?, ?, ?, ?, ?, ?)'
    ;
            
    $pq $db->prepare($sql);
            
    $params = array($username$t_size$t_width$t_height$t_mime_type$t_contentsDateTime::ToSQL());
            
    $res $db->executeParams($pqNULL$params);
            if (
    CSF::isError($res)) return CSF::raiseError("Cannot execute INSERT query.");
            
    $db->freePrepared($pq);
            return 
    TRUE;
        } 
    Hahahah... try that. That's an actual snippet of my photo uploading module of CSF ( http://dev.gauldong.net/csf and http://www.gauldong.net/mod/Info )
    I hope the code is quite well-structured and commented so you can learn from it easily and apply it to your own implementation. You can also see the "math" to compute the size of the thumbnail to a predefined fixed size and maintaining ratio... etc. etc.
    Last edited by ceefour; Mar 4, 2005 at 23:13. Reason: Added PHP tags (just know this thing)

  4. #4
    SitePoint Enthusiast redhead's Avatar
    Join Date
    Feb 2004
    Location
    Ontario
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi There

    I always use the gd library and imagecopyresampled() to create a thumbnail. I can post the function if you still get no joy with this method.
    My code that i have does work, i do not know anything about this gd libary. I did however, run this:

    PHP Code:
    <?php 
    phpinfo
    (); 
    ?>
    and found nothing regarding gb.. is this needed.. or is there another way to resize an image with some type of function..
    What are your thoughts?

  5. #5
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Without gd or imagemagik there isn't much you can do with images on upload.
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  6. #6
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    note: ceefour, use the proper code tags for posting code,
    [ p h p ] [ / p h p ]
    obviously without the spaces...

    it makes it alot easier to read.
    Cheers

    SpikeZ
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  7. #7
    SitePoint Enthusiast redhead's Avatar
    Join Date
    Feb 2004
    Location
    Ontario
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Thanks Man

    I will try and remember
    note: ceefour, use the proper code tags for posting code,
    I think i will look into the gd or imagemagik, guess the firt this is to see which one i can get installed on my server right? do you have any links that are good resources...

    If I may ask what is the differecne between gd and iamgemagik?? what do you think is most effective and easy to learn and understand..

  8. #8
    SitePoint Zealot ceefour's Avatar
    Join Date
    Feb 2005
    Location
    Bandung, Indonesia
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    gd is bundled with PHP since version... phew! well it has been there for quite a long time.
    Even the PHP source bundles it (it requires libjpeg externally though), but this shouldn't be your concern.

    Anyways, gd is an extension so it's not loaded by default. You should be able to change your PHP configuration to include gd or any other extension you're interested in. If you can't do that I suggest moving to a different webhoster. Really, I'm serious. Even worse if your webhoster forces safe_mode. :-P

    Even if you can't do so you can try dl('gd.so') or dl('php_gd.dll') (I'm not sure about the exact name in UNIX PHP) to see if you can load it dynamically. After that dump phpinfo() and you'll be able to see gd there, assuming the dl() call didn't give you an error, that is.

    gd is *VERY* extensively used in many servers which do any kind of image processing, even the relatively simple task of thumbnailing something. ImageMagick is less used because it's an "external" program/library and it's not bundled with PHP.

    Go http://www.php.net/Image


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
  •