Hi,

I recently bought a baby album script. Unfortunately, the pictures aren't resized on uploading and are absolutlely massive!

I've tried for what seems like days now to add in code to resize a picture on upload but to no avail!

Most of the code I've see on resizing seems to be pretty much the same, something like this...

// temporary file created by PHP 
$uploadedfile = $_FILES['photo']['tmp_name'];

// resize
$src = imagecreatefromjpeg($uploadedfile);

// Capture the original size of the uploaded image
list($width,$height)=getimagesize($uploadedfile);

// resize the image 

$newwidth=350;
$newheight=($height/$width)*400;
$tmp=imagecreatetruecolor($newwidth,$newheight);

// resizing, copying from the original image into the $tmp image
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); 

// now write the resized image to disk. 
$filename = "images/". $_FILES['uploadfile']['name'];
imagejpeg($tmp,$filename,100);

imagedestroy($src);
imagedestroy($tmp);

This is the code that I'm trying to fit it into:

require_once '../include/config.php';
require_once $_config['site_path'] . '/include/user_function.php';



if ($submit) {
    $_var += $_POST;
    $source = $_FILES['photo']['name'];
    $ext =  get_file_extension($_FILES['photo']['tmp_name']);

if ($ext != 'err') {
   $temp2 = 1;
}    
$check_max =  check_max_photos();





$_form->isEmpty($source, 'Enter Photo File');
$_form->isEmpty($temp2, 'Enter A Valid Image File(JPG,GIF,PNG)');
$_form->isEmpty($title, 'Enter Photo Title');
$_form->isEmpty($description, 'Enter Photo Description');
$_form->isEmpty($category_id, 'Please Add A Category.');
$_form->isEmpty($check_max, 'Your number of photos Exceeds the limits.');

if ($_form->isError()) {

    $_var['category'] = get_option(array(name => 'category', value=> 'category_id', option => 'category_name'),"where user_id ='$user_id'"); 
    $_var['category'] = str_replace("value=\"$category_id\"","value=\"$category_id\" selected",$_var['category']);
    $_var['error'] = "<font color='red'>" . join("<BR>",$_form->getErrorList()) . "</font>";
    $_var['main_content'] = do_template('user_upload_photo.html');
}
else {
    $db->query("INSERT INTO photos SET category_id = '$category_id', user_id = '$user_id', title = '$title', description = '$description'");
    $photo_id = $db->insert_id(); 
    $uploadpath = $_config['site_path'] . '/photos/';



    $source = $_FILES['photo']['tmp_name'];

    $file_name = "{$user_id}_" . "{$category_id}_" . "$photo_id" . ".$ext";


    $dest = $uploadpath . $file_name;

    if (move_uploaded_file($source, $dest ) ) { 

    } else { 
         echo 'File could not be stored.<BR>'; 
    }  

    $db->query("UPDATE photos SET file_name = '$file_name' WHERE photo_id = '$photo_id'");

    $_var['main_content'] = "Your Photo is Uploaded";
    refresh('upload_photo.php');

}
}
else {
    $_var['category'] = get_option(array(name => 'category', value=> 'category_id', option => 'category_name'),"where user_id ='$user_id'");
    $_var['main_content'] = do_template('user_upload_photo.html');
}

echo do_template('main.html');
echo do_template('footer.html');

I know it's never ideal to try and slot code together like this!

The 'name' tag in the html form is photo so that's ok, and I can see that one problem is the file storage; the new code specifies the wrong place.

I've taken this line out and tried placing the snippet in various places, calling it as a function from the included files listed at the top of the main code block, again at different places in the script.

Can anyone point me the right direction please before I go completely bananas!!??

Many thanks

Luds

    I'd suggest putting your resizing code into a function and perhaps storing it in user_function.php, which appears to part of the gallery which you've bought. I've changed a few things to fit.

    function resizeImage( $uploadedfile )
    {
      // resize
      $src = imagecreatefromjpeg($uploadedfile);
    
      // Capture the original size of the uploaded image
      list($width,$height)=getimagesize($uploadedfile);
    
      // resize the image
    
      $newwidth=350;
      $newheight=($height/$width)*400;
      $tmp=imagecreatetruecolor($newwidth,$newheight);
    
      // resizing, copying from the original image into the $tmp image
      imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
    
      // now write the resized image to disk.
      imagejpeg($tmp,$uploadedfile,100);
    
      imagedestroy($src);
      imagedestroy($tmp);
    }
    

    Then you can modify the other snippet which you've posted with just one line

    // Existing code at line 48
    if (move_uploaded_file($source, $dest ) ) {
    // At line 49, the new line
    resizeImage( $dest );
    

    One improvement is that you would then only resize the file once it's been moved to it's permanent home. So any validation which the script does will have been already done (we hope).

    I do feel a bit sorry for you if you paid for this code. It's not exactly brilliant, and some of the SQL queries are cause for concern. Of course I can't tell for sure without seeing the rest of the code.

    There are plenty of free PHP photo galleries around which have this sort of functionality already built in. Coppermine is one I've seen in action.

    Good luck!

      Shrike....

      What can I say???

      BRILLIANT!!!!

      Worked first time 🙂

      You're right about the code too...the presentation and html ain't wonderful either and I forsee many (happy??) hours trying to get this looking and working how I want it!!

      However, that's not to detract from my problem that you solved.

      many thanks for you time and help.

      all the best
      Luds

        Write a Reply...