ive tried turning a script into a function but im not sure if ive done it correctly. could someone advise me if ive gone wrong please.
<?php
function thumbnail($image, $maxWidth, $maxHeight) {
$pinfo = pathinfo($image);
$tmb_name = $pinfo['dirname'].'/thumbs/'.$pinfo['filename'].'_tmb.'.$pinfo['extension'];
$imageviewed = $pinfo['dirname'].'/viewed/'.$pinfo['filename'].'_viewed.'.$pinfo['extension'];
switch($pinfo['extension']) :
case "jpg" :
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;
endswitch;
list($originalWidth, $originalHeight) = getimagesize($image);
$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, $imageviewed);
imagedestroy($src);
imagedestroy($dst);
return true;
}
?>
and when i call it in my upload file is this correct aswell?
<?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))
{
function thumbnail($maxheight,$maxwidth,$image)
{
$maxheight=150;
$maxwidth=100;
$image = $target_path;
}
function thumbnail($maxheight,$maxwidth,$image)
{
$maxheight=550;
$maxwidth=600;
$image = $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']);
$image_path = $dbLink->real_escape_string($target_path);
$tmb_name = $dbLink->real_escape_string($tmb_name);
//query to insert the data i had gathered into the database
$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}')";
//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>';
?>