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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?
function image_upload($spectype, $specfile, $num, $redirectlink, $current_id, $regup){
	 $current_inserted_id = $current_id;
 
$register_or_upload = "";
switch($regup){
	case 0:
		$register_or_upload = "uppladdad";
		break;
	case 1:
		$register_or_upload = "uppdaterad";
		break;
}
 
  // 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
isset($_POST['insert'])
    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
@is_uploaded_file($_FILES[$fieldname]['tmp_name'])
    or error('not an HTTP upload', $register_or_upload);
    
// Kollar om den uppladdade filen Ò¤r en bild, annars returnera false
@getimagesize($_FILES[$fieldname]['tmp_name'])
    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))
{
  //$num++;
  $db_filename = $spectype.$filename.'-'.$num;
  $db_uploadfilename = $spectype.$filename.'-'.$num.'.'.$ext;
  break;
}
 
// 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);
 
switch($spectype){
	case "article-":
		$query = "INSERT INTO pic (PicName, PicFileName, ArticleID) VALUES ('$db_filename', '$db_uploadfilename', '$current_inserted_id')";
		$result = mysql_query($query);
		break;
	case "cat-":
		$query = "INSERT INTO pic (PicName, PicFileName, CatID) VALUES ('$db_filename', '$db_uploadfilename', '$current_inserted_id')";
		$result = mysql_query($query);
		break;
	case "user-":
		$query = "INSERT INTO pic (PicName, PicFileName, UserID) VALUES ('$db_filename', '$db_uploadfilename', '$current_inserted_id')";
		$result = mysql_query($query);
		break;
}
}
 
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>";
	exit;
} // end error handler
 
?>

Open in new window

LVL 3
GodDoesntExistAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

szewkamCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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?
0
szewkamCommented:
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

0
GodDoesntExistAuthor Commented:
Thank you! =D

Great help!

Your help is unvaluable!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.