Kenneth .J
Kenneth .J

Reputation: 1433

imagecreatefromjpeg not giving me a resource?

I am trying to code a script for file resizing.In the script, I use getimagesize() to get the mime information from the image and then test it's value using a switch statement, assigning different values to variables in different cases.

However after running the script, I'm getting the error

PHP Warning:  imagecopyresampled() expects parameter 2 to be resource, string given in ..........

Why exactly am I getting this error when all I'm doing is passing it the variable from createimagefromjpeg?

My Code extract:

list($width,$height,$type)=getimagesize($_FILES["BusinessCreateFileUpload"]["tmp_name"]);
        //checks the mime type and gets the extension
        //Declares the $uploadedfile,$src and $ext variables.
        $uploadedfile='';
        $src='';
        $ext='';
        switch($type)
        {
            case"image/jpeg":
                $uploadedfile=$_FILES["BusinessCreateFileUpload"]["tmp_name"];
                $src=imagecreatefromjpeg($uploadedfile);
                $ext=".jpg";
                break;
            case"image/png":
                $uploadedfile=$_FILES["BusinessCreateFileUpload"]["tmp_name"];
                $src=imagecreatefrompng($uploadedfile);
                $ext=".png";
                break;
            default:
                echo"An error has occurred.Please follow the <a href='http://localhost/Kenced/Business/edititem.php'>link to try again</a>";
        }
        //Creates thumbnail
        $newwidth=100;
        $newheight=100;
        $thumbimg=imagecreatetruecolor($newwidth, $newheight);
        imagecopyresampled($thumbimg,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
        //Creates displayimage
        $newwidth1=250;
        $newheight1=250;
        $displayimg=imagecreatetruecolor($newwidth1, $newheight1);
        imagecopyresampled($displayimg,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);

I'm editing bits and pieces of a script I found on the internet as I'm still rather new to programming so I might have inadvertently screwed something up, though I can't seem to figure out where exactly did I go wrong.

Upvotes: 1

Views: 416

Answers (2)

Stoic
Stoic

Reputation: 10754

Your code sets a default value of $src to an empty string. The following lines are then accessing this empty string of $src, which is why it is giving the error:

imagecopyresampled($thumbimg,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
imagecopyresampled($displayimg,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);

As per your code, this would occur when the switch case for $type is assuming a default value, and therefore your code must be outputting the following line as well:

An error has occurred.Please follow the link to try again


Regarding OP's comment (the answer seemed long, so including it here):

  • First, make your script use die instead of echo in your default case of the switch statement. So, if the value of $type is not as desired, then the script will give an error and stop.

  • Secondly, the function getimagesize returns mimetype as an INT value, which is what your variable $type will be set as. Instead, you are comparing your $type variable to strings which is wrong, and would always make use of the default case. Therefore, change your switch statement and compare the values to INT values.

  • Also, make sure your case statements and echo statements have a space after keywords, i.e. after case and echo


Considering the above, you can replace your switch statement with this:

$src = 0;
switch($type)
    {
        case 2:
            $uploadedfile=$_FILES["BusinessCreateFileUpload"]["tmp_name"];
            $src=imagecreatefromjpeg($uploadedfile);
            $ext=".jpg";
            break;
        case 3:
            $uploadedfile=$_FILES["BusinessCreateFileUpload"]["tmp_name"];
            $src=imagecreatefrompng($uploadedfile);
            $ext=".png";
            break;
        default:
            die "An error has occurred.Please follow the <a href='http://localhost/Kenced/Business/edititem.php'>link to try again</a>";
    }

Upvotes: 2

Paulo H.
Paulo H.

Reputation: 1258

The getimagesize function returns in 3th parameter the type using a int value, not a string mimetype.

Check the docs in php.net.

1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, 7 = TIFF(intel byte order), 8 = TIFF(motorola byte order), 9 = JPC, 10 = JP2, 11 = JPX, 12 = JB2, 13 = SWC, 14 = IFF, 15 = WBMP, 16 = XBM

var_dump the $type variable.

Upvotes: 0

Related Questions