PHP image resize (thumbnail)

Hi Experts!

I'm looking for a PHP resize script to fit this code:

The upload function in action:

image_upload("user-", "file1", 1, "redirect länk ska vara här", $current_id, 0);

The script im using here is a simple image uploader that saves the image to the FTP. I want to make a thumbnail for this uploaded image.

I've tried several code snippets but I cant get it to work properly since I'm not a guru at PHP.

Thanks for your help!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
function image_upload($spectype, $specfile, $num, $redirectlink, $current_id, $regup){
	 $current_inserted_id = $current_id;
$register_or_upload = "";
	case 0:
		$register_or_upload = "uppladdad";
	case 1:
		$register_or_upload = "uppdaterad";
  // Kollar vilken mapp denna PHP fil befinner sig i
$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
// HÒ¤r anges vilken mapp de uppladdade bilderna ska hamna i
//$uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . 'uploaded_files/';
$uploadsDirectory = '../../pic/images/';
// HÒ¤r anges sÒ¶kvÒ¤gen till uppladdningsformulÒ¤ret
$uploadForm = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'upload_form.php';
// HÒ¤r anges sÒ¶kvÒ¤gen man skickas till nÒ¤r bilden uppladdats korrekt
$uploadSuccess = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'upload_success.php';
// HÒ¤r lagras filnamnet av bilden som laddas upp i <input> i formulÒ¤ret
$fieldname = $specfile;
// MÒ¶jliga uppladdningsfel
$errors = array(1 => 'bilden du valde &#228;r f&#246;r stor',
                2 => 'bilden du valde &#228;r f&#246;r stor',
                3 => 'bilden laddades inte upp',
                4 => 'ingen bild blev vald');
// check the upload form was actually submitted else print the form
    or error('the upload form is neaded', "");
// check for PHP's built-in uploading errors
($_FILES[$fieldname]['error'] == 0)
    or error($errors[$_FILES[$fieldname]['error']], $register_or_upload);
// check that the file we are working on really was the subject of an HTTP upload
    or error('not an HTTP upload', $register_or_upload);
// Kollar om den uppladdade filen Ò¤r en bild, annars returnera false
    or error('only image uploads are allowed', $register_or_upload);
// GÒ¶r ett unikt filnamn fÒ¶r filen och kollar sÒ¥ det inte Ò¤r upptaget
// Om det Ò¤r upptaget, fÒ¶rsÒ¶ker den till den hittar ett ledigt
// Exempel: filnamn3.jpg
//$query = "SELECT * FROM cats";
//$result = mysql_query($query);
$parts = explode('.',$_FILES[$fieldname]['name']);
$ext = array_pop($parts);
$ext = strtolower($ext);
$filename = implode('.',$parts);
$filename = $current_inserted_id;
$db_filename = $spectype.$filename.'-'.$num;
$db_uploadfilename = $spectype.$filename.'-'.$num.'.'.$ext;
while(file_exists($uploadFilename = $uploadsDirectory.$spectype.$filename.'-'.$num.'.'.$ext))
  $db_filename = $spectype.$filename.'-'.$num;
  $db_uploadfilename = $spectype.$filename.'-'.$num.'.'.$ext;
// now let's move the file to its final location and allocate the new filename to it
@move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename)
    or error('receiving directory insuffiecient permission', $register_or_upload);
	case "article-":
		$query = "INSERT INTO pic (PicName, PicFileName, ArticleID) VALUES ('$db_filename', '$db_uploadfilename', '$current_inserted_id')";
		$result = mysql_query($query);
	case "cat-":
		$query = "INSERT INTO pic (PicName, PicFileName, CatID) VALUES ('$db_filename', '$db_uploadfilename', '$current_inserted_id')";
		$result = mysql_query($query);
	case "user-":
		$query = "INSERT INTO pic (PicName, PicFileName, UserID) VALUES ('$db_filename', '$db_uploadfilename', '$current_inserted_id')";
		$result = mysql_query($query);
function error($error, $register_or_upload){
	echo "<br /><br /><span class=\"register-info\">Men bilden blev inte ".$register_or_upload." f&#246;r att: ".$error."</span>";
} // end error handler

Open in new window

Who is Participating?
szewkamConnect With a Mentor Commented:
Below you've got some function to resizing picture.
Works only for .jpg.
Use this function just after the line "@move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename)
    or error('receiving directory insuffiecient permission', $register_or_upload);"
    * source - jpeg picture
    * max_x - max width of miniature 
    * max_y - max height of miniature
    * save_image - name and relative path fo saving miniature
function resizeImage($source, $max_x, $max_y, $save_image, $jpeg_quality = 100) {   
    $img_src = imagecreatefromjpeg($source); 
    $image_x = imagesx($img_src); 
    $image_y = imagesy($img_src); 
    if($image_x > $image_y) /*Landscape*/ { 
      $ratio_x = ($image_x > $max_x) ? $max_x/$image_x : 1; 
      $ratio_y = $ratio_x; 
      $move = 'y'; 
    else /*Portrait*/ { 
      $ratio_y = ($image_y > $max_y) ? $max_y/$image_y : 1; 
      $ratio_x = $ratio_y; 
      $move = 'x'; 
    $new_x = $image_x*$ratio_x; 
    $new_y = $image_y*$ratio_y; 
    $move_x = ($move == "x") ? ($max_x-$new_x)/2 : 0; 
    $move_y = ($move == "y") ? ($max_y-$new_y)/2 : 0; 
    $new_img = imagecreatetruecolor($max_x, $max_y); 
    //beckground color for miniature. Adjust it to your needing
    $background = imagecolorallocate($new_img, 171, 255, 161); 
    imagefill($new_img, 0, 0, $background); 
    imagecopyresampled($new_img, $img_src, $move_x, $move_y, 0, 0, $new_x, $new_y, $image_x, $image_y); 
    imagejpeg($new_img, $save_image, 100); 

Open in new window

GodDoesntExistAuthor Commented:
Thank you so much!

I tink then I'll have to restrict my image uploader also to just work with JPEG. Do you know how I can do that?
szewkamConnect With a Mentor Commented:
it's very simple since in your function you have extracted extension into variable ;). You need to add code from snippet before line: while(file_exists($uploadFilename = $uploadsDirectory.$spectype.$filename.'-'.$num.'.'.$ext))
I use your error function ;)

if ($ext == "jpg" || $ext == "jpeg")
  error("wrong file extension. Allowed: jpg.", $register_or_upload);

Open in new window

GodDoesntExistAuthor Commented:
Thank you! =D

Great help!

Your help is unvaluable!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.