Imagecreatefrompng(): is not a valid PNG file

Hi,

I am trying to create a mobile version of my already uploaded file using below code but getting this error

imagecreatefrompng(): is not a valid PNG file

Here is the code:

if (move_uploaded_file($_FILES['photo']['tmp_name'], $uploadedFile))
{
    $resource = imagecreatefrompng($uploadedFile);

    $small_file = imagescale($resource , 500, 400);
    $small_file = imagepng($small_file);

    $newSmallFile = "mobile-" . $gallery_uuid . '.png';
    $uploadedSmallFile = $uploaddir . $newSmallFile;

    echo $uploadedSmallFile;
    
    if (move_uploaded_file($small_file, $uploadedSmallFile))
    {
        
    }
    else
    {
        echo json_encode("error");
    }
}
else
{
    echo json_encode("error");
}

Kindly help…

Thanks,
Jassim

Are you 100% sure that the file is already a PNG?
Also, normally you would move the file then convert it.
Next, you create the file, then, just scale it. No need to reconvert it back to PNG if it already is one.
If you are uploading some other image type such as a JPG, that would not work. You should verify that the incoming file is a PNG first, then move it and convert it. Rename it during the move.

I am getting the following with getimagesize

IMAGE INTO START HERE ///// Array

(

[

0

] => 1600

[

1

] => 900

[

2

] => 2

[

3

] => width="1600" height="900"

[bits

] => 8

[channels

] => 3

[mime

] => image/jpeg

)

///// IMAGE INTO END HERE<br />

the original file is a JPG but I copying it as PNG to standarize it so the file on my FTP after moving is a PNG

So, it is not a PNG. So, move it as a PNG, meaning rename it. Then convert it to a PNG.
Quite often when you attempt to convert the temporary file, it fails. I have seen that many times!

Remember, when a user, or ADMIN, uploads a file, it goes to your temporary file folder. Then, in the move command it gets to it’s final resting place. Then, it becomes a real live file and should be easy to convert to a PNG. So, move, load it as a JPEG or whatever, save it as a PNG into the same file. Done…

Sponsor our Newsletter | Privacy Policy | Terms of Service