I get two errors in image resize code

Hello,
I wrote the errors I got across the codes
// Warning: imagecreatefrompng(): ‘images/logo/1659626118.png’ is not a valid PNG file in
// Warning: imagecopyresampled() expects parameter 2 to be resource, bool given in
how can i fix these errors

function boyutla($resim, $tresim, $max_en, $max_boy){
              
    $arraylar = explode('.',$resim);
    $sonuc   = $arraylar[(count($arraylar) -1)];
    $sonuc = trim($sonuc);
    $sonuc = strtolower($sonuc);
    
    switch ($sonuc){
        case "jpeg": $islem=imagecreatefromjpeg($resim); break;
        case "JPEG": $islem=imagecreatefromjpeg($resim); break;
        case "jpg": $islem=imagecreatefromjpeg($resim); break;
        case "gif": $islem=imagecreatefromgif($resim); break;
        case "png": $islem=imagecreatefrompng($resim); break; // Warning: imagecreatefrompng(): 'images/firma_logo/1659626118.png' is not a valid PNG file in
        case "JPG": $islem=imagecreatefromjpeg($resim); break;
        case "GIF": $islem=imagecreatefromgif($resim); break;
        case "PNG": $islem=imagecreatefrompng($resim); break;
    }
    
    $boyut = getimagesize($resim);  
    $en    = $boyut[0];
    $boy   = $boyut[1];
     
    $x_oran = $max_en  / $en;  
    $y_oran = $max_boy / $boy;  
     
            if (($en <= $max_en) and ($boy <= $max_boy)){  
                $son_en  = $en;  
                $son_boy = $boy;  
            }  
            else if (($x_oran * $boy) < $max_boy){  
                $son_en  = $max_en;  
                $son_boy = ceil($x_oran * $boy);  
            }  
            else {  
                $son_en  = ceil($y_oran * $en);  
                $son_boy = $max_boy;  
            }  
    
    $yeni = ImageCreateTrueColor($son_en,$son_boy);
    imagealphablending($yeni, false);
    imagesavealpha($yeni, true);
    $transparent = imagecolorallocatealpha($yeni, 255, 255, 0, 127);
    //imagecolorallocatealpha($yeni, $son_en, $son_boy, $en, $boy);
    imagefilledrectangle($yeni, 0, 0, $son_en, $son_boy, $transparent);   
    imagecopyresampled($yeni,$islem,0,0,0,0,$son_en,$son_boy,$en,$boy); // Warning: imagecopyresampled() expects parameter 2 to be resource, bool given in
  
    switch ($sonuc){
        case "jpeg": imagejpeg($yeni,$tresim);  break;
        case "JPEG": imagejpeg($yeni,$tresim);  break;
        case "jpg": imagejpeg($yeni,$tresim);  break;
        case "gif": imagegif($yeni,$tresim); break;
        case "png": imagepng($yeni,$tresim); break;
        case "JPG": imagejpeg($yeni,$tresim);  break;
        case "GIF": imagegif($yeni,$tresim); break;
        case "PNG": imagepng($yeni,$tresim); break;            
    }
    return $resim; 
  }

where does the images/logo/1659626118.png value come from when you call this function? is it an image at all? is it a .png image? how do you know?

If he had uploaded a picture it would have been the name
The image path and name appear exactly in the error message
But the image is not uploaded

The .PNG image I’m trying to upload says it’s not real .PNG when I try it in another php code
Maybe the problem is here

Image upload and resize i need to change all the code
If you have a code you can suggest, I would be glad.

  1. How do I get the size of the uploaded file when the end of “$_FILES[“uploadimage”][“error”]” is 1?
    For $upload_max_filesize = (int)ini_get("upload_max_filesize")*1024576;
  2. Even if the file extension is an image extension, I want to check if it’s really an image
  3. Image should not add background when resizing
  4. Picture aspect ratio should be preserved. There should be no extension
  5. If the image is smaller than the given width and height values, it will not enlarge the image.

Your php file upload code needs to test if the file was successfully uploaded, the [‘error’] element will be set and it will be a zero, before using any of the uploaded file information. See your June 21st thread - Checking the image during image upload

If the [‘error’] element is a 1, there is no uploaded file, [‘tmp_name’] is empty and the [‘size’] is zero (see your print_r() output in that thread), because php won’t do anything with the bad uploaded file.

The upload_max_filesize setting is literally whatever is typed in as the php.ini setting. It can be either an integer or it can use k/K, m/M, or g/G notation. If you want to get the integer value, here’s a function -

// convert numbers using k/K,m/M,g/G notation to an integer
function return_bytes($val) {
    $val = trim($val);
    $last = strtolower($val[strlen($val)-1]);
    switch($last) {
        case 'g':
            $val *= 1024;
        case 'm':
            $val *= 1024;
        case 'k':
            $val *= 1024;
    }
    return $val;
}

Php has two ways of getting the mime type of a file - PHP: mime_content_type - Manual and PHP: finfo_file - Manual

In your resize function code, if $islem is a false value, it means that the imagecreatefrom… call failed. You should test for this and setup a message that the file could not be resized and skip running the rest of the code. Also, since that code is getting the lower-case of the file extension, you only need the lower-case values in the switch/case statements.

1 Like

If the maximum file upload size in php.ini is 2 MB, error 1 will be returned.
When Error 1 message occurs, I want to show message like below

The file size you are trying to upload is 7 MB
Maximum upload allowed 2 MB
as

php.ini => upload_max_filesize = 2M

I am trying to do something like below

if($_FILES["uploadimage"]["error"]=='0'){
// https://www.delftstack.com/howto/php/php-image-resize-on-upload/
// code on this site I will try to implement
}else{
echo "The file you are trying to upload is too large than allowed";
echo "File size you are trying to upload: ?";
echo "Allowed file size: 2 MB";
}

You can get the content length of the request, which will be in bytes, that’s the total size of all the post method form data. This will include the length of all $_POST and $_FILES data. This is in - $_SERVER['CONTENT_LENGTH']

1 Like

Thank you i solved the problem
First with [error] as you suggested, the file is too large I showed the message
Then I checked the file types as you suggested

    $nime_type_array = array(
        'png' => 'image/png',
        'jpe' => 'image/jpeg',
        'jpeg' => 'image/jpeg',
        'jpg' => 'image/jpeg',
        'gif' => 'image/gif',
        'bmp' => 'image/bmp'
    );
	
  if(in_array(mime_content_type($_FILES['uploadimage']['tmp_name']), $nime_type_array)){
  
  }else{
  
  }

I edited my codes as you suggested, now it’s ok
Thank you again

Thank you so much
It was exactly what I wanted
Ekran görüntüsü 2022-08-05 182016

Sponsor our Newsletter | Privacy Policy | Terms of Service