Jump to content

Recommended Posts

Ok, my code below is not working for generating thumbnail images. The script uploads the files, creates the 'thumbs' directory, but the thumbnails are not created. Where did I go wrong?

 

<?php mkdir("../ebay/$ebay_task/thumbs", 0777); ?>

<?php
$path1= "$ebay_task/".$HTTP_POST_FILES['ufile']['name'][0];
$path2= "$ebay_task/".$HTTP_POST_FILES['ufile']['name'][1];
$path3= "$ebay_task/".$HTTP_POST_FILES['ufile']['name'][2];

copy($HTTP_POST_FILES['ufile']['tmp_name'][0], $path1);
copy($HTTP_POST_FILES['ufile']['tmp_name'][1], $path2);
copy($HTTP_POST_FILES['ufile']['tmp_name'][2], $path3);

echo "File Name :".$HTTP_POST_FILES['ufile']['name'][0]."<BR/>";
echo "File Size :".$HTTP_POST_FILES['ufile']['size'][0]."<BR/>";
echo "File Type :".$HTTP_POST_FILES['ufile']['type'][0]."<BR/>";
echo "<img src=\"$path1\" width=\"150\" height=\"150\">";
echo "<P>";

echo "File Name :".$HTTP_POST_FILES['ufile']['name'][1]."<BR/>";
echo "File Size :".$HTTP_POST_FILES['ufile']['size'][1]."<BR/>";
echo "File Type :".$HTTP_POST_FILES['ufile']['type'][1]."<BR/>";
echo "<img src=\"$path2\" width=\"150\" height=\"150\">";
echo "<P>";

echo "File Name :".$HTTP_POST_FILES['ufile']['name'][2]."<BR/>";
echo "File Size :".$HTTP_POST_FILES['ufile']['size'][2]."<BR/>";
echo "File Type :".$HTTP_POST_FILES['ufile']['type'][2]."<BR/>";
echo "<img src=\"$path3\" width=\"150\" height=\"150\">";

///////////////////////////////////////////////////////

// Use this code to display the error or success.

$filesize1=$HTTP_POST_FILES['ufile']['size'][0];
$filesize2=$HTTP_POST_FILES['ufile']['size'][1];
$filesize3=$HTTP_POST_FILES['ufile']['size'][2];

if($filesize1 && $filesize2 && $filesize3 != 0)
{
echo "Images Uploaded Successfully";
}

else {
echo "ERROR.....";
}

//////////////////////////////////////////////

// What files that have a problem? (if found)

if($filesize1==0) {
echo "There're something error in your first file";
echo "<BR />";
}

if($filesize2==0) {
echo "There're something error in your second file";
echo "<BR />";
}

if($filesize3==0) {
echo "There're something error in your third file";
echo "<BR />";
}

?>
<?php
function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth )
{
  // open the directory
  $dir = opendir( $pathToImages );

  // loop through it, looking for any/all JPG files:
  while (false !== ($fname = readdir( $dir ))) {
    // parse path for the extension
    $info = pathinfo($pathToImages . $fname);
    // continue only if this is a JPEG image
    if ( strtolower($info['extension']) == 'jpg' )
    {
      echo "Creating thumbnail for {$fname} <br />";

      // load image and get image size
      $img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
      $width = imagesx( $img );
      $height = imagesy( $img );

      // calculate thumbnail size
      $new_width = $thumbWidth;
      $new_height = floor( $height * ( $thumbWidth / $width ) );

      // create a new temporary image
      $tmp_img = imagecreatetruecolor( $new_width, $new_height );

      // copy and resize old image into new image
      imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

      // save thumbnail into a file
      imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
    }
  }
  // close the directory
  closedir( $dir );
}
createThumbs('/$ebay_task','$ebay_task/thumbs',100);
?>

Thanks, but I want to keep the original image and create the thumbnail in a new directory. I don't want to resize the image, and I can't draw the image dynamically I need to generate it at the same time of upload on the same page. Any other ideas? I appreciate the help.

 

SK

Yes, thats exactly what i am trying to do. it doesn't necessarily 'have' to be in another directory, I can even use it appended with a tn_ or something on the filename in the same directory. Any idea?

 

Thanks a ton,

SK

Well I don't want to go messing with your code bu the script I wrote will work fine with a little fix

 

<?php
$image_path = "C:/Inetpub/wwwroot/phpforum/images/";       //Absolute path to where files are uploaded
$thumb_path = "C:/Inetpub/wwwroot/phpforum/images/thumbs/";   //Absolute path to where thumbs are to be stored if you want this
$size_limit = "yes";                                          //do you want a size limit yes or no.
$limit_size = "2097152";                                       //How big do you want size limit to be in bytes
$limit_ext = "yes";                                           //do you want to limit the extensions of files uploaded
$ext_count = "8";                                             //total number of extensions in array below
$extensions = array(".jpg", ".jpeg", ".png", ".gif", ".doc", ".pdf", ".txt", ".xls");   //List extensions you want files uploaded to be
$im_array = array("image/x-png", "image/pjpeg", "image/gif", "image/jpeg");             //List of image types make sure image type extension is in above array

function resampleimage($maxsize, $sourcefile, $destination, $filetype, $quality){
  // CHECK TO SEE IF THE IMAGE EXISTS FIRST
  if(file_exists($sourcefile)){
  // FIRST WE GET THE CURRENT IMAGE SIZE
  $g_is=getimagesize($sourcefile);
    /********* CALCULATE THE WIDTH AND THE HEIGHT ***************/
    // CHECK TO SEE IF THE WIDTH AND HEIGHT ARE ALREADY SMALLER THAN THE MAX SIZE
    if($g_is[0] <= $maxsize && $g_is[1] <= $maxsize){
    // LEAVE WIDTH AND HEIGHT ALONE IF IMAGE IS SMALLER THAN MAXSIZE
    $new_width=$g_is[0];
    $new_height=$g_is[1];
    } else {
    // GET VALUE TO CALCULATE WIDTH AND HEIGHT
    $w_adjust = ($maxsize / $g_is[0]);
    $h_adjust = ($maxsize / $g_is[1]);
      // CHECK TO WHICH DIMENSION REQUIRES THE SMALLER ADJUSTMENT
      if($w_adjust <= $h_adjust){
      // CALCULATE WIDTH AND HEIGHT IF THE WIDTH VALUE IS SMALLER
      $new_width=($g_is[0]*$w_adjust);
      $new_height=($g_is[1]*$w_adjust);
      } else {
      // CALCULATE WIDTH AND HEIGHT IF THE HEIGHT VALUE IS SMALLER
      $new_width=($g_is[0]*$h_adjust);
      $new_height=($g_is[1]*$h_adjust);
      }
    }
//SWITCHES THE IMAGE CREATE FUNCTION BASED ON FILE EXTENSION
switch($filetype) {
	case 'image/pjpeg':
		$img_src = imagecreatefromjpeg($sourcefile);
		break;
	case 'image/jpeg':
		$img_src = imagecreatefromjpeg($sourcefile);
		break;
	case 'image/x-png':
		$img_src = imagecreatefrompng($sourcefile);
		break;
	case 'image/gif':
		$img_src = imagecreatefromgif($sourcefile);
		break;
	default:
		echo("Error Invalid Image Type");
		die;
		break;
}
  // CREATE THE TRUE COLOR IMAGE WITH NE WIDTH AND HEIGHT
  $img_dst=imagecreatetruecolor($new_width,$new_height);
  // RESAMPLE THE IMAGE TO NEW WIDTH AND HEIGHT
  imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $new_width, $new_height, $g_is[0], $g_is[1]);
  // OUTPUT THE IMAGE AS A JPEG.
  // THIS CAN BE CHANGED IF YOU WANT TRANSPARENCY OR PREFER ANOTHER FORMAT. MAKE SURE YOU CHANGE HEADER ABOVE.
  imagejpeg($img_dst, $destination, $quality);
  // DESTROY THE NEW IMAGE
  imagedestroy($img_dst);
  return true;
  } else {
  return false;
  }
}

if(!isset($_POST['submit'])){
$extens = '';

        if (($extensions == "") or ($extensions == " ") or ($ext_count == "0") or ($ext_count == "") or ($limit_ext != "yes") or ($limit_ext == "")) {
           $extens = "any extension";
        } else {
        $ext_count2 = $ext_count+1;
        for($counter=0; $counter<$ext_count; $counter++) {
            $extens .= "  $extensions[$counter]";
        }
        }
        if (($limit_size == "") or ($size_limit != "yes")) {
            $limit_size = "any size";
        } else {
            $limit_size .= " bytes";
            $mb_size = ($limit_size/1048576);
        }
        $pichead = "<li><font size=\"2\" color=660000>File extension must be $extens<b>";
        $pichead .="</b></font>
        <li><font size=\"2\" color=660000>Maximum file size is $limit_size ($mb_size MB)</font></li>
        <li><font size=\"2\" color=660000>No spaces in the filename</font></li>";
?>
<html>
<head>
<title>HTML Form for uploading image to server</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<html>
<title>Upload Files</title>
<body>
<p><? echo $pichead; ?></p>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<p>Files:<br />
<?php
$files = 3;   // Change this line for the number of upload you want to allow
for($i=1; $i<=$files; $i++){
echo "File $i <input type=\"file\" name=\"files[]\" /><br />\n";
}
?>
<input type="submit" name=submit value="Send" />
</p>
</form>
<?php
} else {
$i=0;
$quality = "75";    // change the quality here

  foreach ($_FILES["files"]["error"] as $key => $error) {
  $file_name =  $_FILES["files"]['name'][$i]; // can call this anything you like this will take the original name
  $file =  $_FILES["files"]['tmp_name'][$i];
  $file_size = $_FILES["files"]['size'][$i];
  $file_type = $_FILES["files"]['type'][$i];
  $endresult = "<font size=\"4\" color=990000>$file_name uploaded successfully</font>";

    if ($file_name == "") {
    $pic = $i+1;
    $endresult = "<font size=\"4\" color=990000>Pic#$pic Not selected</font>";
    }else{
      if(file_exists("$file_path/$file_name") || file_exists("$image_path/$file_name") || file_exists("$thumb_path/$file_name")) {
      $endresult = "<font size=\"4\" color=990000>File Already Existed in your home directory</font>";
      } else {
        if (($size_limit == "yes") && ($limit_size < $file_size)) {
        $endresult = "<font size=\"4\" color=990000>File was to big</font>";
        } else {
        $ext = strrchr($file_name,'.');
          if (($limit_ext == "yes") && (!in_array($ext,$extensions))) {
          $endresult = "<font size=\"4\" color=990000>File is wrong type</font>";
          }else{
            move_uploaded_file($file, $image_path.$file_name);  // Move the file to the folder just as it is
            // Save thumb image with max width/height of 200
            resampleimage("200", $file, $thumb_path.$file_name, $file_type, $quality); // Comment this line out if you don't want to create a thumbnail, change the 200 to the size you want
          }
        }
      }
    }
  $i++;
  echo $endresult."<br>";
  }
}
?>
</body>
</html>

 

Ray

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.