Page 1 of 1

Image Resizing

Posted: Sun Dec 20, 2009 2:08 pm
by simonmlewis
Hi

Does anyone know a good script and method for resizing an image at the point of upload?
I have found a few that resize to the browser, but I want to do this for Avatars. So someone could upload a 500kb big picture, but it reduces the physical size to around 150px150px.

I find 'functions' but haven't a clue how to incorporate them into a PHP $row->photo type code.

Please help.

Re: Image Resizing

Posted: Sun Dec 20, 2009 8:22 pm
by daedalus__
simon programmers don't use 'good script' but i do know of a method.

using the gd library you can resize an image, specifically with the function imagecopyresampled()

http://php.net/manual/en/book.image.php

Re: Image Resizing

Posted: Sun Dec 20, 2009 11:47 pm
by pbs
Try below given function.

Code: Select all

 
function pictureResize($save_directory, $file_array, $max_width, $max_height, $postid)
{
    //--change filename to time - make it unique
    $temp_filename = strtolower($file_array['name']);           
    $ext = substr(strrchr($temp_filename, "."), 1);
    $temp_filename = $postid.strrchr($file_array['name'],".");
    
    //--check that it's a jpeg or gif
    if (preg_match('/^(gif|jpg|jpeg)$/',$ext)) 
    {
        // resize in proportion
        list($width_orig, $height_orig) = getimagesize($file_array['tmp_name']);
        if ($max_width && ($width_orig < $height_orig)) 
        {
            $max_width = ($max_height / $height_orig) * $width_orig;
        } 
        else 
        {
            $max_height = ($max_width / $width_orig) * $height_orig;
        }
        $image_p = imagecreatetruecolor($max_width, $max_height);
        //handle gifs and jpegs separately
        if($ext=='gif')
        {
            $image = imagecreatefromgif($file_array['tmp_name']);                           
            imagecopyresampled($image_p, $image, 0, 0, 0, 0, $max_width, $max_height, $width_orig, $height_orig);
            imagegif($image_p, $save_directory.$temp_filename, 80);
        }
        else
        {
            $image = imagecreatefromjpeg($file_array['tmp_name']);                           
            imagecopyresampled($image_p, $image, 0, 0, 0, 0, $max_width, $max_height, $width_orig, $height_orig);                           
            imagejpeg($image_p, $save_directory.$temp_filename, 80);
        }
        imagedestroy($image_p);
        imagedestroy($image);
        return $temp_filename;
    }
    else
    {
        $error_message.="<br> file is not a jpeg or gif picture <br>";
    }
}
 

Re: Image Resizing

Posted: Mon Dec 21, 2009 3:37 am
by simonmlewis
Thanks for both options. I will look at both.

PBS: I don't know how to "perform a function" from within a web page as I have never written functions in that way.

For example: when someone uploads an image, this is my script:

Code: Select all

if($update == "photoadd") 
  {
 
$pic=($_FILES['photo']['name']);
if ($pic != NULL) 
    {
 
$target = $_SERVER['DOCUMENT_ROOT']."/beta/images/avatars/";
srand(time());
$random = (rand()%99999999);
$pic=($_FILES['photo']['name']);
$newname="$random"."$pic";
$target = $target . $newname;
 
mysql_query("UPDATE users SET photo = '$newname' where id = '$cookieid'");
 
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
      {
echo "<div class='admincompletedbox'>
        <b>Your Avatar has been added.  <img src='images/iconimage.jpg' /></b></div>";
      }
  }
}
I don't have the knowledge to know how I place a function within that, so it only runs when $update" has "photoadd" in it.

Re: Image Resizing

Posted: Mon Dec 21, 2009 3:57 am
by simonmlewis
How would I incorporate this:
imagejpeg($image_p, null, 100);

Into my image upload code?
This appears to load the image to the browser. Also - when I used the code in this page (http://www.php.net/manual/en/function.i ... ampled.php), and places my image file string into the top variable, all it did was report the current URL to the browser and nothing else.

Very odd.