首页 文章

不要自动调整上传的png

提问于
浏览
0

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'不是第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 / conectores-usb.png'在第258行的C:\ xampp \ htdocs \ bconocimiento \ controller \ solucion.controller.php中不是有效的PNG文件

警告: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'在第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 / Conector-pc.png'在第258行的C:\ xampp \ htdocs \ bconocimiento \ controller \ solucion.controller.php中不是有效的PNG文件

警告: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'不是第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 / Mouse-usb.png'在第258行的C:\ xampp \ htdocs \ bconocimiento \ controller \ solucion.controller.php中不是有效的PNG文件

警告: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.php中有效的JPEG文件220

警告: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.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 / 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'在第258行的C:\ xampp \ htdocs \ bconocimiento \ controller \ solucion.controller.php中不是有效的PNG文件

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

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

    我确信这是因为这一行:

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

    它评估为true,因为$ target_path上还没有任何文件 . 因此,这些条件总是会评估为真,因为 || 运算符 .

    建议:

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

    就像是:

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

相关问题