警告:imagecreatefromjpeg() 期望参数 1 为资源

Warning : imagecreatefromjpeg()expects parameter 1 to be resource

我的 reszie 图像(代码)有问题,当我 运行 本地主机中的这段代码工作正常时,但是当我在网站上实现时。代码给出警告。

<br /><b>Warning</b> : imagecreatefromjpeg([-1, [], 17, 1, 18, 1, 19, 23, true, [true, true],26,1,27,1,30,1,33]) expects parameter 1 to be resource, boolean given in <b>fungsi/f_upload_banner.php</b> on line <b>34</b><br />

这是我调整大小的代码

<?php

$target_dir = "../uploads/images/banner/";

$image1 =$_FILES['txtfile']['name'];
$filename1 = stripslashes($_FILES['txtfile']['name']);
$ext1 = substr($image1, strrpos($image1, '.')+1);
$idimg1 = md5(uniqid() . time() . $filename1) . "-1." . $ext1;
$target_file1 = $target_dir . basename($idimg1);

    //identify images file
    $realImages             = imagecreatefromjpeg($target_file1);
    $width                  = imageSX($realImages);
    $height                 = imageSY($realImages);

    //save for thumbs size
    $thumbWidth     = 150;
    $thumbHeight    = ($thumbWidth / $width) * $height;

    //change images size
    $thumbImage = imagecreatetruecolor($thumbWidth, $thumbHeight);
    imagecopyresampled($thumbImage, $realImages, 0,0,0,0, $thumbWidth, $thumbHeight, $width, $height);

    //save thumbnail images
    imagejpeg($thumbImage,$target_dir."thumb_".$idimg1);

    //remove images object from memory
    imagedestroy($realImages);
    imagedestroy($thumbImage);
    ?>

哪里错了?

您正在尝试使用原始文件名(经过处理后)作为 imagecreatefromjpeg() 的来源,而您应该使用上传过程分配的临时名称:$_FILES['txtfile']['tmp_name']

这样做:

$realImages = imagecreatefromjpeg($_FILES['txtfile']['tmp_name']);

您也没有将上传的文件移动到永久位置。当您的脚本终止时,临时版本将被删除,文件将丢失。

另请注意,您的代码未进行任何错误检查,因此如果上传失败您不会知道。请参阅 Handling File Upload

上的 PHP 部分