4 Replies - 1776 Views - Last Post: 20 February 2010 - 02:24 PM Rate Topic: -----

#1 saunders1989  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 16-January 10

move thumbnail image to a different folder and write the path of it in

Posted 18 February 2010 - 09:27 AM

when i upload a image it gets sent to my folder called images a script is run called thumb save and creates a thumbnail of that original file but places it in the same folder. what i need it to do is get the thumbnail and move it to a folder called thumbnails or something similiar and then write the path of the thumbnail in the database where it is stored.

the actual large image has its path stored in the database already. but i dont know how to move the thumbnail to the folder and create the image path in the database. ive tried adding my own target path and changing a few things but kept coming up with errors. so not to sure where to go from there. the image path would be just an extra row in my images table so it would have the same gallery_id as the large image and then i can call it in my loop to display them. please help!

thanks for listning

here is the thumbnail script:

<?php

// $image, $maxHeight, $maxWidth

$pinfo = pathinfo($image);

$tmb_name = $pinfo['dirname'].'/'.$pinfo['filename'].'_tmb.'.$pinfo['extension'];

if(!isset($maxWidth)){

    $maxWidth = 100;

}

if(!isset($maxHeight)){

    $maxHeight = 150;

}

switch(strtolower(substr($image, -3))) {

case "jpg" :

    $fileType = "jpeg";

    $imageCreateFunction = "imagecreatefromjpeg";

    $imageOutputFunction = "imagejpeg";

    break;

case "jpeg" :

    $fileType = "jpeg";

    $imageCreateFunction = "imagecreatefromjpeg";

    $imageOutputFunction = "imagejpeg";

    break;

case "png" :

    $fileType = "png";

    $imageCreateFunction = "imagecreatefrompng";

    $imageOutputFunction = "imagepng";

    break;

case "gif" :

    $fileType = "gif";

    $imageCreateFunction = "imagecreatefromgif";

    $imageOutputFunction = "imagegif";

    break;

}

$size = GetImageSize($image);

$originalWidth = $size[0];

$originalHeight = $size[1];

$x_ratio = $maxWidth / $originalWidth;

$y_ratio = $maxHeight / $originalHeight;

// check that the new width and height aren't bigger than the original values.

// the new values are higher than the original, don't resize or we'll lose quality

if (($originalWidth <= $maxWidth) && ($originalHeight <= $maxHeight)) {

    $newWidth = $originalWidth;

    $newHeight = $originalHeight;

} else if (($x_ratio * $originalHeight) < $maxHeight) {

    $newHeight = ceil($x_ratio * $originalHeight);

    $newWidth = $maxWidth;

} else {

    $newWidth = ceil($y_ratio * $originalWidth);

    $newHeight = $maxHeight;

}

$src = $imageCreateFunction($image);

$dst = imagecreatetruecolor($newWidth, $newHeight);

// Resample

imagecopyresampled($dst, $src, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight);

// Save image

$imageOutputFunction($dst, $tmb_name);

ImageDestroy($src);

ImageDestroy($dst);

?> 


it gets the path for the image from this file:

<?php

 $max_size=5*1024*1024;

 // Check if a file has been uploaded

if(isset($_FILES['uploaded_file']) && preg_match("/image\/jpeg|image\/jpg/i",$_FILES['uploaded_file']['type']) && $_FILES['uploaded_file']['size']<= $max_size)

{

     // Make sure the file was sent without errors

     if($_FILES['uploaded_file']['error'] == 0)

     {

        $target_path = "images/";

        $target_path = $target_path . basename( $_FILES['uploaded_file']['name']);     

        if(!file_exists($target_path)){

    if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target_path))

        {

            $image = $target_path;

            $maxHeight = 50; $maxWidth = 50;

            include 'thumb_save.php'; // Save a thumb of uploaded pic 

            echo "The file ".  basename($_FILES['uploaded_file']['name']). " has been uploaded";

            $dbLink = new mysqli('localhost', 'root', '', 'gallery');

             if(mysqli_connect_errno()) {

            die("MySQL connection failed: ". mysqli_connect_error());

                                        }             

              // Gather all required data

              $name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']);

              $mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']);

              $size = intval($_FILES['uploaded_file']['size']);

              $image_path = $dbLink->real_escape_string($target_path);

              $gallery_type = $dbLink->real_escape_string($_POST['gallery_type']);

             $desc = $dbLink->real_escape_string($_POST['desc']);

             $image_path = $dbLink->real_escape_string($target_path);

             //query to insert the data i had gathered into the database

             $query = "INSERT INTO `images` (`name`, `size`, `created`, `image_path`, `gallery_type_id`, `desc` )

             VALUES ('{$name}', {$size}, NOW(), '{$image_path}', '{$gallery_type}', '{$desc}')";

             //executes the query

             $dbLink->query($query);

        }

    }

    else 

         {

            echo 'A file with the same name exists please change the file name and try again';

          }

}

  else

          {

      echo 'A file was not sent';

        }

}

else

          {

      echo 'The file is too large';

        }

 // Echo a link back to the main page

 echo '<p>Click <a href="member-index.php">here</a> to go back</p>'; 

 ?> 


Is This A Good Question/Topic? 0
  • +

Replies To: move thumbnail image to a different folder and write the path of it in

#2 vernak  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 24
  • Joined: 11-February 10

Re: move thumbnail image to a different folder and write the path of it in

Posted 18 February 2010 - 11:00 AM

Ok so you're trying to save the thumbnail in a different folder. From looking at your code, you are just saving the thumbnail in the same folder. You will actually have to have a different folder/file path specified when you're creating a thumbnail image. If that folder name doesn't exist you might need to make it dynamically or just using whatever you use to connect to your server. Basically, you have to change the directory when you are creating it. I'm sure there is a way to move a file, but if you put it in the correct directory first, then you will never need to move it.

I think this would work:

$tmb_name = $pinfo['dirname'].'/'.$pinfo['filename'].'_tmb.'.$pinfo['extension'];

// to...

$tmb_name = $pinfo['dirname'].'/(whateverfolderyouwant)/'.$pinfo['filename'].'_tmb.'.$pinfo['extension'];




As for inserting the file path into the database, you can do two different things. You can take the $tmb_name that I showed above and just insert it into the database, associated with the image of course. You will need to add another column to your table, but that is not too hard.

You can also just take care of it in your code and not use the database. If you name the place where the thumbnails are being stored consistently, then you can just add a directory to the image path and go from there.
EX: If I have different galleries and I put all the thumbs in basically the same place, here is what it would look like:
$image_path = 'gallery1/image1.jpg';
$thumb_path = 'gallery1/thumbs/image1.jpg';
$image_path = 'gallery2/image1.jpg';
$thumb_path = 'gallery2/thumbs/image1.jpg';
$image_path = 'gallery3/image1.jpg';
$thumb_path = 'gallery3/thumbs/image1.jpg';
// etc...



If that doesn't help then I'll give you an example...
Was This Post Helpful? 1
  • +
  • -

#3 saunders1989  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 16-January 10

Re: move thumbnail image to a different folder and write the path of it in

Posted 18 February 2010 - 11:50 AM

moving the thumbnail to that folder worked a treat :) thank you. writing the path of it hasnt worked unfortunatly.

the top line is how i do the image path for my large image and the line below is what i tried for the thumbnail

 $image_path = $dbLink->real_escape_string($target_path);
       $tmb_name = $dbLink->real_escape_string($tmb_name);


unfortunatly the thumbnail line didnt work.

this is my query i use aswell:

$query = "INSERT INTO `images` (`name`, `size`, `created`, `image_path`, `gallery_type_id`, `desc`, `thumbnail_path` )
             VALUES ('{$name}', {$size}, NOW(), '{$image_path}', '{$gallery_type}', '{$desc}', '{$tmb_name}')";


EDIT:

im very very very thick!!! i miss spelt the word thumbnail and that is the reason it didnt write it in the database i put thumnail and that would explain my issue. thanks again for your help!

This post has been edited by saunders1989: 18 February 2010 - 11:53 AM

Was This Post Helpful? 0
  • +
  • -

#4 saunders1989  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 16-January 10

Re: move thumbnail image to a different folder and write the path of it in

Posted 18 February 2010 - 12:02 PM

sorry to be a pain but can i ask another question:

when im trying to display the thumbnails i get a error. when creating the thumbnails on the fly i used this code:

while ($row = $result->fetch_assoc()) {

echo "<a href=\"image.php?id=" . $row['id'] . "\">";
echo "<img src=\"imageResize.php?imageFilename=" . $row['name'] . "&maxHeight=90&maxWidth=150\" />\n";
echo "</a>\n";

}


but now i have the thumbnails in a folder nd the image path stored in the database i thought it would be this:

while ($row = $result->fetch_assoc()) {

echo "<a href=\"image.php?id=" . $row['id'] . "\">";
echo "<img src=\"" . $row['thumbnail_path'] " />\n";
echo "</a>\n";

}  


but i was wrong and got errors. any ideas?

thanks again for you help
Was This Post Helpful? 0
  • +
  • -

#5 vernak  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 24
  • Joined: 11-February 10

Re: move thumbnail image to a different folder and write the path of it in

Posted 20 February 2010 - 02:24 PM

can you provide the errors that you are getting? From what it looks like, you should be in good shape. It might be something along the lines of the path not being the correct one. For example you may need '../' in front of it to go up a directory depending on where the page is located. Also, I have not had much experience with specifying a file path as the <img /> src. I have no clue what that does if you would like to fill me in.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1