Jump to content

move image to another folder and write path to database


saunders1989

Recommended Posts

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>'; 

?> 

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.