Riski Febriansyah
Riski Febriansyah

Reputation: 437

Warning : imagecreatefromjpeg()expects parameter 1 to be resource

I have problem with my reszie image (code) when I run this code in localhost the code is work fine, but when I implement in website. the code give a warning.

<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 />

This is my code for resize

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

Where is wrong?

Upvotes: 1

Views: 5257

Answers (1)

user1864610
user1864610

Reputation:

You're trying to use the original file name (after mangling) as the source for imagecreatefromjpeg(), when you should be using the temporary name assigned by the upload process: $_FILES['txtfile']['tmp_name']

Do this:

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

You're also not moving the uploaded file to a permanent location. The temporary version will be deleted when your script terminates and the file will be lost.

Note also that your code is doing no error checking whatsoever, so if your upload fails you won't know about it. See the PHP section on Handling File Upload

Upvotes: 4

Related Questions