-1

I want to create thumbnail while uploading image, i have created upload script.

<?php
if (isset($_POST['upload'])) {
$target = "./img/".basename($_FILES['image']['name']);

$image = $_FILES['image']['name'];

$sql = "INSERT INTO `image`(`image`) VALUES ('$image')";
mysqli_query($connection, $sql);

if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
$smsg = "Image Uploaded Successfuly";
}else {
$fmsg = "An Error Occure During Upload";
}

}
?>

and resize image script:

function resize($newHeight, $targetFile, $originalFile) {

$info = getimagesize($originalFile);
$mime = $info['mime'];

switch ($mime) {
        case 'image/jpeg':
                $image_create_func = 'imagecreatefromjpeg';
                $image_save_func = 'imagejpeg';
                $new_image_ext = 'jpg';
                break;

        case 'image/png':
                $image_create_func = 'imagecreatefrompng';
                $image_save_func = 'imagepng';
                $new_image_ext = 'png';
                break;

        case 'image/gif':
                $image_create_func = 'imagecreatefromgif';
                $image_save_func = 'imagegif';
                $new_image_ext = 'gif';
                break;

        default: 
                throw new Exception('Unknown image type.');
}

$img = $image_create_func($originalFile);
list($width, $height) = getimagesize($originalFile);

$newWidth = ($width / $height) * $newHeight;
$tmp = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($tmp, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);

if (file_exists($targetFile)) {
        unlink($targetFile);
}
$image_save_func($tmp, "thumb/$targetFile.$new_image_ext");
}

i join both script like this

<?php
if (isset($_POST['upload'])) {
$target = "./img/".basename($_FILES['image']['name']);

$image = $_FILES['image']['name'];

$sql = "INSERT INTO `image`(`image`) VALUES ('$image')";
mysqli_query($connection, $sql);

if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
$smsg = "Image Uploaded Successfuly";

resize(320, '$target', '$target');     //resize function
}else {
$fmsg = "An Error Occure During Upload";
}

}
?>

and when i try to join both script then image upload but thumbnail not created error = No such file or directory.

Any one help me to join both script and create thumbnail while uploading image.

tkruse
  • 10,222
  • 7
  • 53
  • 80
Navneet
  • 1
  • 2

1 Answers1

-1

The single quotes in the following line's variables are not being parsed, the values are actually interpreted as $target instead of the intended folder/filename.xxx:

resize(320, '$target', '$target');

Either you remove them:

resize(320, $target, $target);

or use double quotes.

resize(320, "$target", "$target");

Consult http://php.net/manual/en/language.types.string.php on strings.

Use PHP's error reporting and make sure the paths are correct and that the folder(s) is (are) writeable with proper permissions.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • can the dv'er (just now) share their views on this? that would help. Otherwise, the dv is uncalled for; there's nothing wrong with this answer. – Funk Forty Niner Jul 19 '17 at 17:14