Support » Developing with WordPress » Database connection problem when uploading image

  • Resolved ashop59

    (@ashop59)


    Hello, yesterday I made this script that manipulates the $file in the filter. It seems to work fine because it achieves all its purpose, even loading the image in webp correctly.

    But the problem is that in each uploaded file I receive the email “There is a connection problem with the database.”

    Does anyone know what is wrong with this? Do not code anything other than this script to manipulate the files.

    ////
    Sorry it’s fixed now, it seemed to be a grammar error when I typed else instead of elseif.

    I can’t find a way to delete this question. If they inform me, I will do it since it is solved and it was not necessary to ask

    Thank you so much

    function max_image_size($file){
        
    $limit = 2048;
    $size = $file['size'] / 1000;
    $mbSize = round(($size / 1000),1);
    list($category,$type) = explode('/',$file['type']);
    $tmp_name = $file['tmp_name'];
    $img = getimagesize($tmp_name);
    $minimum = array('max_width'=>'3000','max_height'=>'3000');
    $width = $img[0];
    $height = $img[1];
    
    if ($size > $limit){
    return array("error"=>"El tamaño de tu archivo es de $mbSize MB y el máximo permitido es de 2 MB.");
     
    }elseif($width > $minimum['max_width']){
          
     return array("error"=>"El ancho de tu imagen es $width px y el máximo permitido es {$minimum['max_width']} px.");
     
    }elseif($height>$minimum['max_height']){
        
    return array("error"=>"La altura de tu imagen es $height px y el máximo permitido es {$minimum['max_height']}px."); 
    
    }elseif('image'!= $category || ! in_array($type,array('jpg','jpeg','png'))){
        
    return array("error"=>"Subiste un archivo $type y los formatos permitidos son .jpg, .jpeg, o .png.");   
      
        }elseif($type == 'png'){
     $image = imagecreatefrompng($tmp_name);
     }elseif($type == 'jpg' || $type == 'jpeg'){
     $image = imagecreatefromjpeg($tmp_name); 
     }
     
     
     if ( ! in_array($image,array('NULL',false)) ) {
                    imagewebp($image, $file['tmp_name'],60);
                    $file['name'] = pathinfo($file['name'],PATHINFO_FILENAME).'.webp';
                    $file['size'] = getimagesize($file['tmp_name']);
                    $file['type'] = 'image/webp';
                }
            
            return $file;
    
    } 
    
    }
    add_filter( 'wp_handle_upload_prefilter', 'max_image_size' );
     
     
     
    function restrict_mimes($mime_types){
        $mime_types = array(
            'jpeg' => 'image/jpeg', 
            'jpg' => 'image/jpg', 
            'jpe' => 'image/jpe',
            'png' => 'image/png',
            'webp' => 'image/webp'
        );
        
        
     return $mime_types;
    }
    
    add_filter('upload_mimes', 'restrict_mimes');
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    There is no way to delete posts. Don’t worry, one extra among millions of topics won’t make any difference 🙂 Marking it as resolved is good enough. Ideally, you’d add a reply explaining what happened. By adding a reply, your topic falls off of the “no replies” view that many members use to find those still needing help. But this reply serves that purpose, no further action is required.

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.