Jump to content


Photo

Image Resize and Rename


  • Please log in to reply
1 reply to this topic

#1 Flukey

Flukey
  • Members
  • PipPipPip
  • Advanced Member
  • 39 posts

Posted 13 October 2006 - 04:14 PM

Hi,

I've done some code which uploads a picture, resizes it to a ratio value. However, i can't work out how to create the new image name and then move it into a directory.

Heres the code:


        function open_image ($file) {
    # JPEG:
    $im = @imagecreatefromjpeg($file);
    if ($im !== false) { return $im; }

    # GIF:
    $im = @imagecreatefromgif($file);
    if ($im !== false) { return $im; }

    # PNG:
    $im = @imagecreatefrompng($file);
    if ($im !== false) { return $im; }

    # GD File:
    $im = @imagecreatefromgd($file);
    if ($im !== false) { return $im; }

    # GD2 File:
    $im = @imagecreatefromgd2($file);
    if ($im !== false) { return $im; }

    # WBMP:
    $im = @imagecreatefromwbmp($file);
    if ($im !== false) { return $im; }

    # XBM:
    $im = @imagecreatefromxbm($file);
    if ($im !== false) { return $im; }

    # XPM:
    $im = @imagecreatefromxpm($file);
    if ($im !== false) { return $im; }

    # Try and load from string:
    $im = @imagecreatefromstring(file_get_contents($file));
    if ($im !== false) { return $im; }

    return false;
    }
     if($_POST['upload_picture']) {  
        // No image?
        if (empty($_FILES['image']) OR $_FILES['image']['error'] != UPLOAD_ERR_OK) {
            echo ('<strong>Invalid image uploaded.  Please go back and try again.</strong>');
        }

        $imagepath = $_FILES['image']['tmp_name'];
        $file_type = $_FILES['image']['type'];

        // Load image
        $image = open_image($imagepath);

        if (!$image) {
            die ('<strong>You uploaded an invalid image. Please go back and try again.</strong>');
        }

        // Get original width and height
        $width = imagesx($image);
        $height = imagesy($image);

        //Calculate new width and height
    if($width > 200)  {
            $new_width = floatval(200);
            $new_height = $height * ($new_width/$width);
        }
    elseif($height > 200) {
            $new_height = floatval(200);
            $new_width = $width * ($new_height/$height);
    }

        // Resample
        $image_resized = imagecreatetruecolor($new_width, $new_height);
        $image =imagecopyresampled($image_resized, $new_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
   
        // Create a hash of the title for the picture name
        
        $hash_title = md5($int_title);
        $user_id = $_SESSION['user_id'];
        $image_title = $hash_title . "_" . $user_id . ".jpg";
        $filepath = "e:\XXXX\X\XXXX\user\htdocs\article\images";  
        $webpath = "http://www.XXXX.com/article/images/";
        $image_filepath = $filepath . $image_title;
        $image_webpath = $webpath . $image_title;

     
       ;
        $imagecreated = true;
        $next_step = 2;
 

    }   

Any help would be hugely appreciated, thanks.

Jamie

#2 fezzik

fezzik
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 13 October 2006 - 04:43 PM

Jamie,

Here is some code that I use for a photogallery site:

//--------------- end: config.php --------------------
//path where to store images
$path_img = "../uploads/";

$extlimit = "yes"; //Do you want to limit the extensions of files uploaded (yes/no)

//allowed Extensions
$limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");

//check if folders are Writable or not
//please CHOMD them 777
if (!is_writeable($path_img)){
    die ("Error: The directory <b>($path_img)</b> is NOT writable");
}
// --------------- end: config.php --------------------


if ( $_SERVER['REQUEST_METHOD'] == "POST" ) {
  // get the post variables
  foreach( $_POST as $key=>$value ) {
    if ( $value ) {
      $$key=htmlentities( trim($value), ENT_QUOTES );
    } else {
      $$key="";
    } //end else loop
  } //end foreach loop
  
  $file_type = $_FILES['imgfile']['type'];
  $file_name = $_FILES['imgfile']['name'];
  $file_size = $_FILES['imgfile']['size'];
  $file_tmp = $_FILES['imgfile']['tmp_name'];

  //check if you have selected a file.
  if(!is_uploaded_file($file_tmp)){
    echo "Error: Please select a file to upload!.";
    exit(); //exit the script and don't do anything else.
  }
  
  //check file extension
  $ext = strrchr($file_name,'.');
  $ext = strtolower($ext);
  if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
    echo "Wrong file extension.";
    exit();
  }

  //get the file extension.
  $getExt = explode ('.', $file_name);
  $file_ext = $getExt[count($getExt)-1];

  //create a random file name
  $rand_name = md5(time());
  $rand_name= rand(0,999999999)."_".rand(0,999999999);
	 
  //upload the image
  move_uploaded_file ($file_tmp, "$path_img/$rand_name.$file_ext");

} //end POST loop

At a quick glance it looks like you haven't included a way to copy the file to a directory in your script. I think the following lines might help:

//save the image to a writable directory
imagejpeg($image_resized,"$path_img/$rand_name.$file_ext");

Here is the link for more information on imagejpeg: http://us3.php.net/imagejpeg

Let me know if any of this helps.

Cheers,

Fezzik




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users