不要自动调整上传的png

时间:2018-06-23 03:12:43

标签: php file-upload gd autoresize

guardar方法允许在修改后上传php文件和图像(jpg和png),以便图像自动调整大小,以jpg图像类型工作正常,但是当我尝试上传png文件时,确实可以不行。显示下一个错误:

警告:imagecreatefromjpeg():gd-jpeg:JPEG库报告了不可恢复的错误:在第220行的C:\ xampp \ htdocs \ bconocimiento \ controller \ solucion.controller.php中

警告:imagecreatefromjpeg():'view / hardware / imagenes / Mouse.php / conectores-usb.png'在C:\ xampp \ htdocs \ bconocimiento \ controller \ solucion.controller.php中不是有效的JPEG文件第220行

警告:imagecopyresampled()期望参数2为资源,在第250行的C:\ xampp \ htdocs \ bconocimiento \ controller \ solucion.controller.php中给出布尔值

警告:imagecreatefrompng():'view / hardware / imagenes / Mouse.php / conectores-usb.png'在C:\ xampp \ htdocs \ bconocimiento \ controller \ solucion.controller.php中不是有效的PNG文件258行

警告:imagecopyresampled()期望参数2为资源,在第288行的C:\ xampp \ htdocs \ bconocimiento \ controller \ solucion.controller.php中给出布尔值

警告:imagecreatefromjpeg():gd-jpeg:JPEG库报告了不可恢复的错误:在第220行的C:\ xampp \ htdocs \ bconocimiento \ controller \ solucion.controller.php中

警告:imagecreatefromjpeg():'view / hardware / imagenes / Mouse.php / Conector-pc.png'在C:\ xampp \ htdocs \ bconocimiento \ controller \ solucion.controller.php中不是有效的JPEG文件第220行

警告:imagecopyresampled()期望参数2为资源,在第250行的C:\ xampp \ htdocs \ bconocimiento \ controller \ solucion.controller.php中给出布尔值

警告:imagecreatefrompng():'view / hardware / imagenes / Mouse.php / Conector-pc.png'在C:\ xampp \ htdocs \ bconocimiento \ controller \ solucion.controller.php中不是有效的PNG文件258行

警告:imagecopyresampled()期望参数2为资源,在第288行的C:\ xampp \ htdocs \ bconocimiento \ controller \ solucion.controller.php中给出布尔值

警告:imagecreatefromjpeg():gd-jpeg:JPEG库报告了不可恢复的错误:在第220行的C:\ xampp \ htdocs \ bconocimiento \ controller \ solucion.controller.php中

警告:imagecreatefromjpeg():'view / hardware / imagenes / Mouse.php / Mouse-usb.png'在C:\ xampp \ htdocs \ bconocimiento \ controller \ solucion.controller.php中不是有效的JPEG文件第220行

警告:imagecopyresampled()期望参数2为资源,在第250行的C:\ xampp \ htdocs \ bconocimiento \ controller \ solucion.controller.php中给出布尔值

警告:imagecreatefrompng():'view / hardware / imagenes / Mouse.php / Mouse-usb.png'在C:\ xampp \ htdocs \ bconocimiento \ controller \ solucion.controller.php中不是有效的PNG文件258行

警告:imagecopyresampled()期望参数2为资源,在第288行的C:\ xampp \ htdocs \ bconocimiento \ controller \ solucion.controller.php中给出布尔值

警告:imagecreatefromjpeg():gd-jpeg:JPEG库报告了不可恢复的错误:在第220行的C:\ xampp \ htdocs \ bconocimiento \ controller \ solucion.controller.php中

警告:imagecreatefromjpeg():'view / hardware / imagenes / Mouse.php / Tipo-conector-mouse.png'在C:\ xampp \ htdocs \ bconocimiento \ controller \ solucion.controller中不是有效的JPEG文件。第220行的PHP

警告:imagecopyresampled()期望参数2为资源,在第250行的C:\ xampp \ htdocs \ bconocimiento \ controller \ solucion.controller.php中给出布尔值

警告:imagecreatefrompng():'view / hardware / imagenes / Mouse.php / Tipo-conector-mouse.png'在C:\ xampp \ htdocs \ bconocimiento \ controller \ solucion.controller中不是有效的PNG文件。第258行上的php

警告:imagecopyresampled()期望参数2为资源,在第288行的C:\ xampp \ htdocs \ bconocimiento \ controller \ solucion.controller.php中给出布尔值

警告:imagecreatefromjpeg():gd-jpeg:JPEG库报告了不可恢复的错误:在第220行的C:\ xampp \ htdocs \ bconocimiento \ controller \ solucion.controller.php中

警告:imagecreatefromjpeg():'view / hardware / imagenes / Mouse.php / Tipos.png'在第220行的C:\ xampp \ htdocs \ bconocimiento \ controller \ solucion.controller.php中不是有效的JPEG文件。

警告:imagecopyresampled()期望参数2为资源,在第250行的C:\ xampp \ htdocs \ bconocimiento \ controller \ solucion.controller.php中给出布尔值

警告:imagecreatefrompng():'view / hardware / imagenes / Mouse.php / Tipos.png'不是C:\ xampp \ htdocs \ bconocimiento \ controller \ solucion.controller.php中第258行的有效PNG文件

警告:imagecopyresampled()期望参数2为资源,在第288行的C:\ xampp \ htdocs \ bconocimiento \ controller \ solucion.controller.php中给出布尔值

警告:无法修改标头信息-已发送的标头(输出从C:\ xampp \ htdocs \ bconocimiento \ controller \ solucion中的C:\ xampp \ htdocs \ bconocimiento \ controller \ solucion.controller.php:288开始) .controller.php,第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");
}

为什么未检测到png文件类型,为什么我上传的所有png都检测到它们无效,为什么还执行了自动调整jpg大小的代码?

1 个答案:

答案 0 :(得分:0)

我确定是因为这一行:

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

它评估为true,因为$ target_path上还没有任何文件。因此,这些条件将始终为true,因为||运算符。

建议:

首先在外部if上检查!file_exists($target_path)),然后在内部if中检查图像类型。

类似:

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

  }
  if(//png){

  }
}