...

View Full Version : move image to another folder and write path to database



saunders1989
02-18-2010, 04:32 PM
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>';

?>

Fumigator
02-18-2010, 06:16 PM
Try the rename() function.

http://us2.php.net/manual/en/function.rename.php

saunders1989
02-18-2010, 06:37 PM
how would that help? i want to keep the thumbnail the same name and at the end of it there is _tmb which gets added already. i just need that to be moved to a folder and written where the path is on the database

mOrloff
02-18-2010, 07:04 PM
Check out the link from Fumigator.
He knows his stuff.

(The point is that you can "rename" it into whichever dir you desire. )

saunders1989
02-18-2010, 07:11 PM
i did this instead as it works just as good. thanks for the link. everything im doing is a learning curve.


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

// to...

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


my only problem is i cant seem to display my thumbnail images:

this is how i create them on the fly:


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 this is what i thought it would be to display the thumbnail images:


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?

Courtney
02-21-2010, 06:42 AM
rename() is the way to go. It will allow you to specify the old name and the new name and also a new location.

I believe it is something like:

rename (\images\filea.aaa, \images\images\filea.aaa);

puzzled
10-26-2010, 12:41 PM
sorry if this thread has long ben inactive but i seem to have a similar problem and i'm using rename function..

i'm trying to make a function in order to move images to another folder, i'm using rename function but i have problems on getting it to work.
this is what the error says..

Warning: rename(wp-content/gallery/shirts-gallery/thumbs/thumbs_1288084826.jpg,wp-content/gallery/shirts-gallery/1288084826.jpg) [function.rename]: No error in E:\Xampp\xampp\htdocs\wordpress2\wp-content\plugins\nextgen-gallery\view\gallery.php on line 83

can anyone please help? I don't understand the error :(

Fumigator
10-26-2010, 02:02 PM
It looks like maybe you're renaming a file to the same name which is throwing a warning but the. Wordpress is intercepting the warning to say it's not an error. (maybe :p)

puzzled
10-29-2010, 02:56 AM
Yeah, it is a wordpress problem. I tried it out of wordpress and it works! But my problem is that i need to get it to work in wordpress.. anyone know how? Thanks. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum