Jump to content

Saving php Thumbnails


Accurax

Recommended Posts

I'm having diffculty getting my head around this, and could do witha  prod in the right direction.

 

1) I can upload a file to my server & save it in a picture dir.

2) At the same time i can create a thumbnail of this image and display it on the screen.

 

 

But how do i go about saving this new thumbnail image to a seperate directory, at the same time as saving the origional image?

 

heres my entire upload script:

 

<?php
session_start();
include("passwords/andstuff");
include("function.inc"); //this is the open_file() function

if ( $_SESSION['login'] != "true" )
{
	header("location: hacker.php");
}
else
{
	$connection=mysql_connect($host, $user, $passwd)
	or die ("Could not connect !");
	$db = mysql_select_db($database, $connection)
	or die ("Could not connect to Database");


//--------------------------------------------

   
   if (($_FILES['filename']['type']!="image/jpeg") && ($_FILES['filename']['type']!="image/pjpeg") && ($_FILES['filename']['type']!="image/gif")) {
      
      echo "Sorry you must upload only files of the type .jpg .jpeg or .gif, Click <a href='../pictureupload.php'>Here</a> to try again";
   
   } else {

      $query = "SELECT picture1 FROM pics WHERE user_name='$username'";
      $exist = mysql_query($query) or die ("could not Select Picture.");

      preg_match('/\.\w{3,4}$/', $_FILES['filename']['name'], $matches);
      $new_filename = $_SESSION['username']."1".$matches[0];

      $filepath = "pics/full_size/".$new_filename;


if (file_exists($filepath)) {
          unlink("pics/full_size/".$new_filename);
	  $username = $_SESSION['username'];

      }
  
      $source = "pics/full_size/";
      move_uploaded_file($_FILES['filename']['tmp_name'], "../../$source".$new_filename);

      $query = "UPDATE pics SET picture1 = '$filepath' WHERE user_name='{$_SESSION['username']}'";
      $result = mysql_query($query) or die ("could not add picture.");
   }
}


//------------------

//create and save the thumbnail version of this picture.
// Load image
$truepath = "../../".$filepath;
$image = open_image($truepath);

if ($image === false) { 
die ('Unable to open image'); 
}


// Get original width and height
$width = imagesx($image);
$height = imagesy($image);

// New width and height
$new_width = 100;
$new_height = 100;

// Resample
$image_resized = imagecreatetruecolor($new_width, $new_height);
imagecopyresized($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// Display resized image
/*I dont want to display in the complete version... just save this file to a new directory*/

header('Content-type: image/jpeg');
imagejpeg($image_resized);
die();


?>

 

 

Im sorry to keep bothering you guys with this today, but im struggleing to understand how i approach saving this thumbnail to my "thumbs" directory.... Thanks in advance for any advice / kicks up the bum :)

Link to comment
https://forums.phpfreaks.com/topic/40180-saving-php-thumbnails/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.