【问题标题】:don't work auto resize uploaded image不能自动调整上传的图片大小
【发布时间】:2018-12-02 11:09:05
【问题描述】:

此方法允许上传一个php文件和图片,在你修改它以便创作图片后,它不再起作用并且错误出来了:

警告:imagecreatefromjpeg(C:\xampp\tmp\php8ED3.tmp): 无法打开流:C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php 220 行中没有这样的文件或目录

警告:getimagesize(C:\xampp\tmp\php8ED3.tmp): 无法打开流:C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php 221 行中没有这样的文件或目录

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

警告:imagecreatefromjpeg(C:\xampp\tmp\php8F24.tmp):无法打开流:C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php 中没有这样的文件或目录在第 220 行

警告:getimagesize(C:\xampp\tmp\php8F24.tmp): 无法打开流:第 221 行的 C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php 中没有这样的文件或目录

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

警告:imagecreatefromjpeg(C:\xampp\tmp\php8F2F.tmp): 无法打开流:C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php 220 行中没有这样的文件或目录

警告:getimagesize(C:\xampp\tmp\php8F2F.tmp): 无法打开流:第 221 行的 C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php 中没有这样的文件或目录

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

警告:imagecreatefromjpeg(C:\xampp\tmp\php8F44.tmp):无法打开流:第 220 行的 C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php 中没有这样的文件或目录

警告:getimagesize(C:\xampp\tmp\php8F44.tmp): 无法打开流:C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php 221 行中没有这样的文件或目录

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

警告:imagecreatefromjpeg(C:\xampp\tmp\php8F45.tmp):无法打开流:第 220 行的 C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php 中没有这样的文件或目录

警告:getimagesize(C:\xampp\tmp\php8F45.tmp): 无法打开流:第 221 行的 C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php 中没有这样的文件或目录

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

警告:imagecreatefromjpeg(C:\xampp\tmp\php8F50.tmp):无法打开流:C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php 中没有这样的文件或目录在第 220 行

警告:getimagesize(C:\xampp\tmp\php8F50.tmp):无法打开流:第 221 行的 C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php 中没有这样的文件或目录

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

警告:imagecreatefromjpeg(C:\xampp\tmp\php8FDD.tmp):无法打开流:第 220 行的 C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php 中没有这样的文件或目录

警告:getimagesize(C:\xampp\tmp\php8FDD.tmp): 无法打开流:第 221 行的 C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php 中没有这样的文件或目录

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

警告:imagecreatefromjpeg(C:\xampp\tmp\php8FE8.tmp):无法打开流:第 220 行的 C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php 中没有这样的文件或目录

警告:getimagesize(C:\xampp\tmp\php8FE8.tmp): 无法打开流:C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php 221 行中没有这样的文件或目录

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

警告:imagecreatefromjpeg(C:\xampp\tmp\php9011.tmp):无法打开流:C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php 中没有这样的文件或目录在第 220 行

警告:getimagesize(C:\xampp\tmp\php9011.tmp): 无法打开流:第 221 行的 C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php 中没有这样的文件或目录

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

警告:无法修改标头信息 - 标头已由 C:\xampp\htdocs\bconocimiento\controller\solucion 中的(输出开始于 C:\xampp\htdocs\bconocimiento\controller\solucion.controller.php:221)发送.controller.php 在第 236 行

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/png" || $_FILES["archivo"]["type"][$key] == "image/jpg" || !file_exists($target_path))
            {
                $result =move_uploaded_file($source, $target_path);
                // autoresize image
                $orig_image = imagecreatefromjpeg($source);
                $image_info = getimagesize($source); 
                $width_orig  = $image_info[0]; // current width as found in image file
                $height_orig = $image_info[1]; // current height as found in image file
                $width = 968; // new image width
                $height = 681; // new image height
                $destination_image = imagecreatetruecolor($width, $height);
                imagecopyresampled($destination_image, $orig_image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
                imagejpeg($destination_image, $target_path, 100);
            // autoresize image
            }


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

它保存在数据库名称,类型,描述和页面中,但是没有创建图像文件夹,其中创建了名称为页面的文件夹,保存图像的位置,我应该更改什么以便该方法有效再来一次?

【问题讨论】:

  • 请突出显示行211。我有一种感觉,它不在此代码中。否则它会随着另一条消息而死..
  • 天哪,您的编辑让我什至想知道您是否自己阅读了错误消息。为什么不从收到的第一条错误消息开始,跟踪它并找到它引导您的位置?
  • opendir() ... 为什么?你没有做任何需要这样做的事情

标签: php file-upload gd autoresize


【解决方案1】:

一旦你运行

$result =move_uploaded_file($source, $target_path);

文件不再位于$sourceC:\xampp\tmp\php8ED3.tmp

已移至$target_path 并从$source 中删除

所以在下面的所有代码中都使用$target_path

$orig_image = imagecreatefromjpeg($target_path);
$image_info = getimagesize($target_path);

【讨论】:

    猜你喜欢
    • 2019-10-09
    • 1970-01-01
    • 2011-08-25
    • 2014-08-26
    • 2011-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多