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

时间:2015-02-18 04:27:31

标签: php image-resizing

我在reszie映像(代码)上遇到问题,当我在localhost中运行此代码时,代码工作正常,但是当我在网站中实现时。代码发出警告。

<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);
    ?>

哪里错了?

1 个答案:

答案 0 :(得分:2)

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

这样做:

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

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

另请注意,您的代码不会进行任何错误检查,因此如果您的上传失败,您将无法了解它。请参阅Handling File Upload

上的PHP部分