Could someone please help me as ive been trying to figure out a way to do this for days. At the moment i have a website that you can upload an image to a folder and the image path is stored on the database. When you click on the gallery a thumbnail is produced on the fly using a script called imageResize. I have now found out that producing images on the fly is a no no and i am now looking into a way how to fix it. The way i think to fix it is by uploading the image and resizing it to a thumbnail and then upload to the image folder. Is it possible to run the imageResize script on file when it is uploading.
So like in the code it checks to see if the file doesn’t exist if it doesn’t exist run the script and upload the thumbnail image? Is that possible?
is this the right way about getting around creating thumbnails on the fly? or am i really not going in the right direction?
Here is the add_file.php file which is behind the upload form i have:
<?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))
{
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']);
//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>';
?>