ives rodriguez
ives rodriguez

Reputation: 7

don't auto resize uploaded png

The guardar method allows uploading a php file and images(jpg and png), after you modify it so that the images are auto resize,with the type of jpg image works well,but when I try to upload png files it does not work. show the next errors:

Warning: imagecreatefromjpeg(): gd-jpeg: JPEG library reports unrecoverable error: in C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php on line 220

Warning: imagecreatefromjpeg(): 'view/hardware/imagenes/Mouse.php/conectores-usb.png' is not a valid JPEG file in C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php on line 220

Warning: imagecopyresampled() expects parameter 2 to be resource, boolean given in C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php on line 250

Warning: imagecreatefrompng(): 'view/hardware/imagenes/Mouse.php/conectores-usb.png' is not a valid PNG file in C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php on line 258

Warning: imagecopyresampled() expects parameter 2 to be resource, boolean given in C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php on line 288

Warning: imagecreatefromjpeg(): gd-jpeg: JPEG library reports unrecoverable error: in C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php on line 220

Warning: imagecreatefromjpeg(): 'view/hardware/imagenes/Mouse.php/Conector-pc.png' is not a valid JPEG file in C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php on line 220

Warning: imagecopyresampled() expects parameter 2 to be resource, boolean given in C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php on line 250

Warning: imagecreatefrompng(): 'view/hardware/imagenes/Mouse.php/Conector-pc.png' is not a valid PNG file in C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php on line 258

Warning: imagecopyresampled() expects parameter 2 to be resource, boolean given in C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php on line 288

Warning: imagecreatefromjpeg(): gd-jpeg: JPEG library reports unrecoverable error: in C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php on line 220

Warning: imagecreatefromjpeg(): 'view/hardware/imagenes/Mouse.php/Mouse-usb.png' is not a valid JPEG file in C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php on line 220

Warning: imagecopyresampled() expects parameter 2 to be resource, boolean given in C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php on line 250

Warning: imagecreatefrompng(): 'view/hardware/imagenes/Mouse.php/Mouse-usb.png' is not a valid PNG file in C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php on line 258

Warning: imagecopyresampled() expects parameter 2 to be resource, boolean given in C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php on line 288

Warning: imagecreatefromjpeg(): gd-jpeg: JPEG library reports unrecoverable error: in C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php on line 220

Warning: imagecreatefromjpeg(): 'view/hardware/imagenes/Mouse.php/Tipo-conector-mouse.png' is not a valid JPEG file in C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php on line 220

Warning: imagecopyresampled() expects parameter 2 to be resource, boolean given in C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php on line 250

Warning: imagecreatefrompng(): 'view/hardware/imagenes/Mouse.php/Tipo-conector-mouse.png' is not a valid PNG file in C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php on line 258

Warning: imagecopyresampled() expects parameter 2 to be resource, boolean given in C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php on line 288

Warning: imagecreatefromjpeg(): gd-jpeg: JPEG library reports unrecoverable error: in C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php on line 220

Warning: imagecreatefromjpeg(): 'view/hardware/imagenes/Mouse.php/Tipos.png' is not a valid JPEG file in C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php on line 220

Warning: imagecopyresampled() expects parameter 2 to be resource, boolean given in C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php on line 250

Warning: imagecreatefrompng(): 'view/hardware/imagenes/Mouse.php/Tipos.png' is not a valid PNG file in C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php on line 258

Warning: imagecopyresampled() expects parameter 2 to be resource, boolean given in C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php on line 288

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php:288) in C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php on line 299

public function Guardar()
{


    $solucion = new solucion();
    $tamaño = $_FILES['Pagina']['size'];
    $tipo = $_FILES['Pagina']['type'];

    $ruta = "";
    if ($_REQUEST['Tipo'] == "software")
    {
        $ruta = "view/software/";
    }
    if ($_REQUEST['Tipo'] == "hardware")
    {
        $ruta = "view/hardware/";
    }
    if ($_REQUEST['Tipo'] == "red")
    {
        $ruta = "view/red/";
    }


    opendir($ruta);
    $archivo_name = $_FILES['Pagina']['name'];
    $extension = explode(".", $archivo_name);
    $num = count($extension) - 1;
    if ($extension[$num] == "php" || $extension[$num] == "html")
    {

        $destino = $ruta . $_FILES['Pagina']['name'];
        copy($_FILES['Pagina']['tmp_name'], $destino);
        $solucion->id = $_REQUEST['id'];

        $solucion->Nombre = $_REQUEST['Nombre'];
        $solucion->Tipo = $_REQUEST['Tipo'];
        $solucion->Descripcion = $_REQUEST['Descripcion'];

        $solucion->Pagina = $_FILES['Pagina']['name'];

        if ($solucion->id > 0)
        {
            $this->model->Actualizar($solucion, $_REQUEST['id']);
        }
        else
        {
            $this->model->Registrar($solucion);
        }
    }
    $nuevacarpeta = $solucion->Pagina;
    $directorio = "";
    $ubicaion = $solucion->Tipo;
    if ($ubicaion == "software")
    {
        $directorio = "view/software/imagenes/" . $nuevacarpeta . "/";
    }
    if ($ubicaion == "hardware")
    {
        $directorio = "view/hardware/imagenes/" . $nuevacarpeta . "/";
    }
    if ($ubicaion == "red")
    {
        $directorio = "view/red/imagenes/" . $nuevacarpeta . "/";
    }


    foreach ($_FILES["archivo"]['tmp_name'] as $key => $tmp_name)
    {

        if ($_FILES["archivo"]["name"][$key])
        {
            $filename = $_FILES["archivo"]["name"][$key];
            $source = $_FILES["archivo"]["tmp_name"][$key];




            if (!file_exists($directorio))
            {
                mkdir($directorio, 0777) or die("No se puede crear el directorio de extracción");
            }

            $dir = opendir($directorio);
            $target_path = $directorio . $filename;

            if ($_FILES["archivo"]["type"][$key] == "image/jpeg" || $_FILES["archivo"]["type"][$key] == "image/jpg" || !file_exists($target_path))
            {
                $result =move_uploaded_file($source, $target_path);
                $orig_image = imagecreatefromjpeg($target_path);
                $image_info = getimagesize($target_path); 
                $width_orig  = $image_info[0]; // current width as found in image file
                $height_orig = $image_info[1]; // current height as found in image file
                $ratio = $width_orig / $height_orig;
                $width = $height = min($image_info, max($width_orig, $height_orig));

                if ($ratio < 1) {
                    $width = $height * $ratio;
                } else {
                    $height = $width / $ratio;
                }


                $srcWidth = $width_orig;
                $srcHeight = $height_orig;
                $srcX = $srcY = 0;

                $width = $height = min($width_orig, $height_orig, $image_info);

                if ($ratio < 1) {
                    $srcX = 0;
                    $srcY = ($height_orig / 2) - ($width_orig / 2);
                    $srcWidth = $srcHeight = $width_orig;
                } else {
                    $srcY = 0;
                    $srcX = ($width_orig / 2) - ($height_orig / 2);
                    $srcWidth = $srcHeight = $height_orig;
                }
                $destination_image = imagecreatetruecolor($width, $height);
                imagecopyresampled($destination_image, $orig_image, 0, 0, $srcX, $srcY, $width, $height, $srcWidth, $srcHeight);

                imagejpeg($destination_image, $target_path, 100);
            }

            if ( $_FILES["archivo"]["type"][$key] == "image/png" || !file_exists($target_path))
            {
                $result = move_uploaded_file($source, $target_path);
                $orig_image = imagecreatefrompng($target_path);
                $image_info = getimagesize($target_path);
                $width_orig = $image_info[0]; // current width as found in image file
                $height_orig = $image_info[1]; // current height as found in image file
                $ratio = $width_orig / $height_orig;
                $width = $height = min($image_info, max($width_orig, $height_orig));

                if ($ratio < 1) {
                    $width = $height * $ratio;
                } else {
                    $height = $width / $ratio;
                }


                $srcWidth = $width_orig;
                $srcHeight = $height_orig;
                $srcX = $srcY = 0;

                $width = $height = min($width_orig, $height_orig, $image_info);

                if ($ratio < 1) {
                    $srcX = 0;
                    $srcY = ($height_orig / 2) - ($width_orig / 2);
                    $srcWidth = $srcHeight = $width_orig;
                } else {
                    $srcY = 0;
                    $srcX = ($width_orig / 2) - ($height_orig / 2);
                    $srcWidth = $srcHeight = $height_orig;
                }
                $destination_image = imagecreatetruecolor($width, $height);
                imagecopyresampled($destination_image, $orig_image, 0, 0,  $srcX, $srcY, $width, $height, $srcWidth, $srcHeight);

                imagepng($destination_image, $target_path, 9);
            }




            closedir($dir);
        }
    }
    header("Location: index.php");
}

Why is not the png file type detected, why do all the pngs I upload detect them as not valid, why is the code to auto-resize the jpg also executed?

Upvotes: 0

Views: 65

Answers (1)

Charis
Charis

Reputation: 419

I am sure its because of this line:

if ($_FILES["archivo"]["type"][$key] == "image/jpeg" || $_FILES["archivo"]["type"][$key] == "image/jpg" || !file_exists($target_path))

it evaluate to true because there isn't any file on $target_path yet. So those condition will alway evaluate to true, because || operator.

Suggestion:

check !file_exists($target_path)) first on an outer if then check image type in inner if.

something like:

if(!file_exists($target_path))){
  if(//jpeg){

  }
  if(//png){

  }
}

Upvotes: 0

Related Questions