Image Upload then Resize

I’ve been searching all over the internet but I just can’t figure out how to get it working. Here’s my image upload script:

<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/bmp")
|| ($_FILES["file"]["type"] == "image/pdn")
|| ($_FILES["file"]["type"] == "image/psd")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 5000000))
 {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
       print "Successfully uploaded!<p>";   
    echo "File name: " . $_FILES["file"]["name"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kilobytes<br />";

$filename = $_FILES["file"]["name"];
$unique_id = uniqid (rand(), true);
echo $c;
echo "<br>";
$boo = "$unique_id-$filename";
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "i/" . $boo);

 print "<a href='"; 
      print "http://trollin.info/i/";
      print "$boo'>http://trollin.info/i/$boo</a>";
      print "</p>HTML: <br><TEXTAREA style='WIDTH: 297px; HEIGHT: 47px' rows=1 cols=35><img src='http://trollin.info/i/$boo'></textarea><p> ";
  
    print "<img src='http://trollin.info/i/";  
    print "$boo";
    print "'>";
      }
    }
else
  {
  echo "Invalid file";
  }
?>

http://trollin.info/

It works perfectly, except the image is displayed at full size. I need a maximum height of 200 and a maximum width of 350 and it to stay at the same aspect ratio. I’ve tried using CSS/HTML, but that won’t work with considering the aspect ratio and the height/width.

I need to use GD/ImageMagick I think somehow, but I don’t know how to get it working with my script

This might help you :slight_smile: I’m doing exactly what you’re looking for I think…



error_reporting(E_ALL);
ini_set("display_errors", 1); 

require("includes/actionPack.php");

handleDefaults($_POST['catid']);  // create default cat record if it doesnt exist

/*
// ImageMagick
//convert picture.jpg -thumbnail 120x120 picture-thumbnail.jpg

*/

$extensions = array("jpg","gif","jpeg","png");

$catid		= $_POST['catid'];

$pic 		= strtotime(date("Y-m-d h:i:s A")) . $_FILES['photo']['name'];

$pic		= str_replace(" ","_",$pic);

$tmpPic		= "tmp_".$pic;

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

$extPic 	= strtolower(trim(substr($pic, strrpos($pic,".")+1)));


if (in_array($extPic, $extensions)) {
	
	if (move_uploaded_file($tmpName,"../categoryphotos/$tmpPic")) {
		//echo "Move success";
	} else {
		//echo "move failure";
	}

	$fullDir = $adminDocRoot . "categoryphotos/$tmpPic";
	
	$newLocation = $adminDocRoot . "categoryphotos/$pic";
	
	
	// Create large resized
	shell_exec("convert $fullDir -thumbnail 440x390 $newLocation"); 

	$thumbLocation = $adminDocRoot . "categorythumbs/$pic";
	
	// Create thumbnail
	shell_exec("convert $fullDir -thumbnail 71x92 $thumbLocation");

	// Delete orig. photo
	unlink("../categoryphotos/$tmpPic");
	
	// Add to database...
	$sql = "INSERT INTO tblCustomCatImages VALUES ('','{$_POST['catid']}','$pic','$pic','0');";
	mysql_query($sql);
	
	header("location: form_manage_photos.php?catid={$_POST['catid']}");

} else {
	
	header("location: form_manage_photos.php?catid={$_POST['catid']}&err=1");
}


I’m using some includes here and other functions that you don’t see, but this should be enough to answer some of your questions. Basically what I’m doing here is uploading the orig. file to the web server, THEN I’m using ImageMagick to make two copies - a thumbnail and a smaller version of the original photo, then, I’m deleting the original. Hope this helps!