Image Resizing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Image Resizing

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Re: Image Resizing

Post 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
pbs
Forum Contributor
Posts: 230
Joined: Fri Nov 07, 2008 5:31 am
Location: Nashik, India
Contact:

Re: Image Resizing

Post 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>";
    }
}
 
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Image Resizing

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Image Resizing

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply