Sharo Kantir
Sharo Kantir

Reputation: 83

uploading and creating thumbnail error php

I'm making my frist cms for a gallery I have. I want to be able to upload an image to a folder but at the same time upload the thumbnail for the same image into another folder. So I tried using GD for the thumbnail part but I am getting the following error;

Warning: move_uploaded_file() expects parameter 1 to be string, resource given in C:\xampp\htdocs\liana\admin_panel\functions\gallery_upload.php on line 49

But it is a string, it is the path of the file.

gallery.php;

<form action="functions/gallery_upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="file" >
<button type="submit" name="submit"> UPLOAD </button>
</form>

functions/gallery_upload.php

<?php
//code for image upload, working normally
if (isset($_POST['submit'])){
    $file = $_FILES['file'];
    $fileName = $_FILES['file']['name'];
    $fileTmpName = $_FILES['file']['tmp_name'];
    $fileSize = $_FILES['file']['size'];
    $fileError = $_FILES['file']['error'];
    $fileType = $_FILES['file']['type'];
    $fileExt = explode('.', $fileName);
    $fileActualExt = strtolower(end($fileExt));
    $allowed = array('jpg', 'jpeg', 'png');

    if (in_array($fileActualExt, $allowed)) {
        if ($fileError === 0) { //if there are no errors
            if ($fileSize < 500000) {
                $fileNameNew = uniqid('', true);    
                str_replace(".", "", $fileNameNew);
                $fileNameNew = $fileNameNew.".".$fileActualExt; //change img name based on time
                $fileDestination = '../../gallery/data1/images/'.$fileNameNew;
                move_uploaded_file($fileTmpName, $fileDestination);
        } else {echo 'too big';}} else {echo 'error';}} else {echo 'filetype not allowed';}}


// here I am trying to make the thumbnail

$thumb = resize_image($fileDestination, 200, 200); //I get said error in this line 
move_uploaded_file($thumb,'../../gallery/data1/tooltips/'.$fileNameNew);


// copy pasted function that resizes the image
function resize_image($file, $w, $h, $crop=FALSE) {
    list($width, $height) = getimagesize($file);
    $r = $width / $height;
    if ($crop) {
        if ($width > $height) {
            $width = ceil($width-($width*abs($r-$w/$h)));
        } else {
            $height = ceil($height-($height*abs($r-$w/$h)));
        }
        $newwidth = $w;
        $newheight = $h;
    } else {
        if ($w/$h > $r) {
            $newwidth = $h*$r;
            $newheight = $h;
        } else {
            $newheight = $w/$r;
            $newwidth = $w;}}
    $src = imagecreatefromjpeg($file);
    $dst = imagecreatetruecolor($newwidth, $newheight);
    imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

return $dst;}

Upvotes: 0

Views: 90

Answers (1)

Sharo Kantir
Sharo Kantir

Reputation: 83

I just used a plugin calle 'wideimage'. I strogly reccomend it, you have the same result with just 4 lines of code..

include "path/WideImage.php";
$thumb = WideImage::load($fileDestination);
$resized = $thumb->resize(100, 100);
$resized->saveToFile("path/". $fileNameNew .".jpg");

Upvotes: 1

Related Questions