Uploading & thumbnails

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
therat
Forum Commoner
Posts: 62
Joined: Wed Oct 01, 2003 2:44 pm
Location: London

Uploading & thumbnails

Post by therat »

I have the following code that was put together from various scripts found on the net. The code uploads 2 files, one a jpg and one a zip file, and adds the info into a database. The jpg is resized, adds a border to it and is renamed along with the zip file to a random name and then the names entered into a db. The code does work but I would like to clean it up, creating a function or class from it. Any hints or tips welcome.

Code: Select all

<?php
error_reporting(E_ALL);

include('includes/funcs.php');
$skinz = my_conn();

$member_id  = $_POST['member_id'];
$cat_id = $_POST['cat_id'];
$sub_cat = $_POST['sub_cat'];
$image_desc = $_POST['image_desc'];
$image_name = $_POST['image_name'];

// Set the directory to upload files to
  $uploaddir_r = $_SERVER['DOCUMENT_ROOT'] . '/uploads/previews/';
  $uploaddir_z = $_SERVER['DOCUMENT_ROOT'] . '/uploads/archives/';

	// An array of allowed file types 
    $file_types = array(   
     'image/pjpeg' => 'jpg',
     'image/jpeg' => 'jpg',
     'image/x-png' => 'png',
	 'application/x-zip-compressed' => 'zip'
    );

   $imagefile = $uploaddir_r . $_FILES['imagefile']['name']; 
   $zipfile = $uploaddir_z . $_FILES['zipfile']['name']; 

//Preview image section 
    $filetype = $_FILES['imagefile']['type']; 
    $filename = $_FILES['imagefile']['name']; 
    $tempname = $_FILES['imagefile']['tmp_name']; 

   if (!move_uploaded_file($tempname, $imagefile)) { 
       print "ERROR: File is invalid"; 
       print_r($_FILES); 
   } 
	$new_filei = substr(sha1(rand(10, time())), 0,  . '.' . $file_types[$filetype]; 
	rename($imagefile, $uploaddir_r . $new_filei);

// Set a few variables for resize script
$image = $uploaddir_r . $new_filei;
$newimage = $image;
$image_quality = 80; 
$addborder = 1; 
$max_height = 225; 
$max_width = 300; 

// Main image resize code 
$src_img = ImageCreateFromJpeg($image); 
$orig_x = ImageSX($src_img); 
$orig_y = ImageSY($src_img); 

$new_y = $max_height; 
$new_x = $orig_x/($orig_y/$max_height);   

if ($new_x > $max_width) { 
    $new_x = $max_width; 
    $new_y = $orig_y/($orig_x/$max_width); 
} 

$dst_img = ImageCreateTrueColor($new_x,$new_y); 
ImageCopyResampled($dst_img, $src_img, 0, 0, 0, 0, $new_x, $new_y, $orig_x, $orig_y); 

if ($addborder == 1) { 
    // Add border 
    $black = ImageColorAllocate($dst_img, 0, 0, 0); 
    ImageSetThickness($dst_img, 1); 
    ImageLine($dst_img, 0, 0, $new_x, 0, $black); 
    ImageLine($dst_img, 0, 0, 0, $new_y, $black); 
    ImageLine($dst_img, $new_x-1, 0, $new_x-1, $new_y, $black); 
    ImageLine($dst_img, 0, $new_y-1, $new_x, $new_y-1, $black); 
} 
     
ImageJpeg($dst_img, $newimage, $image_quality); 
ImageDestroy($src_img); 
ImageDestroy($dst_img); 


//Zip file section
    $filetype = $_FILES['zipfile']['type']; 
    $filename = $_FILES['zipfile']['name']; 
    $tempname = $_FILES['zipfile']['tmp_name']; 

   if (!move_uploaded_file($tempname, $zipfile)) { 
       print "ERROR: File is invalid"; 
       print_r($_FILES); 
   } 
 	$new_filez = substr(sha1(rand(10, time())), 0,  . '.' . $file_types[$filetype]; 
	rename($zipfile, $uploaddir_z . $new_filez);

// Databse insertion stuff
    $zipmime = $_FILES['zipfile']['type'];
    $zipsize = $_FILES['zipfile']['size'];


$query = "INSERT INTO skinz_images (cat_id,subcat_id,user_id,status,image_name,image_description,image_date,image_thumb_file,image_download_url,image_mime,image_zsize) VALUES ('$cat_id','$sub_cat','$member_id','0','$image_name','$image_desc',now(),'uploads/previews/$new_filei','uploads/archives/$new_filez','$zipmime','$zipsize')";
$result = mysql_query($query, $skinz) or die(mysql_error());
mysql_close();

echo header ("Location: usercp.php");
?>
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

You can easily create a nice class from the code, just split it up into seperate stages. Uploading a zip if just the same as uploading a jpg or a gif or a png etc, so you can have a single class method to do this. Once you have the file uplaoded, then check to see if its an image and you need to resize it/add border etc - again place this in a function. Then have another class method, or a seperate class to add the details to the database.

Then you can use the class constructor to run the whole thing, pass to it the name of the files uploaded, have it loop through them - calling the check_upload() and move_upload() methods on each, then do a check to see if the add_border() and resize() methods should be called, then finally have it submit the data to the database.

I submitted a class a while back in the code snippets section that deals with all this, so check that out: viewtopic.php?t=17214
therat
Forum Commoner
Posts: 62
Joined: Wed Oct 01, 2003 2:44 pm
Location: London

Post by therat »

Thanks, that is a great help.
Post Reply