duangangpin078794 2017-08-12 10:47
浏览 47
已采纳

上传和创建缩略图错误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;}

  • 写回答

1条回答 默认 最新

  • douyi9597 2017-08-13 09:29
    关注

    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");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?