【问题标题】:don't auto resize uploaded png不要自动调整上传的png大小
【发布时间】:2018-12-02 12:05:16
【问题描述】:

guardar 方法允许上传 php 文件和图像(jpg 和 png),在您修改它以便图像自动调整大小后,使用 jpg 图像的类型效果很好,但是当我尝试上传 png 文件时它会不行。显示下一个错误:

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

警告: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 library reports unrecoverable error: in C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php on line 220

警告: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 library reports unrecoverable error: in C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php on line 220

警告: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 library reports unrecoverable error: in C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php on line 220

警告: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 library reports unrecoverable error: in C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php on line 220

警告: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 中的(输出开始于 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 大小的代码也执行了?

【问题讨论】:

    标签: php file-upload gd autoresize


    【解决方案1】:

    我确定是因为这条线:

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

    它评估为真,因为 $target_path 上还没有任何文件。所以这些条件总是会计算为真,因为|| 运算符。

    建议:

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

    类似:

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

    【讨论】:

      猜你喜欢
      • 2011-10-20
      • 2018-12-02
      • 1970-01-01
      • 2021-07-24
      • 1970-01-01
      • 1970-01-01
      • 2011-08-18
      • 2014-10-20
      • 2012-07-07
      相关资源
      最近更新 更多