imagejpeg - 以JPEG未能打开流

问题描述 投票:2回答:1

我使用的功能resizeImage()来调整图像,但偶尔会失败。大多数时间是工作,但我有这样的某个时候警告说,它无法找到文件。应该是什么问题呢?如何解决这个问题?

思路:

  1. 权限文件
  2. 图像的错误路径
  3. 会议
  4. 写入权限DIR

用法:

<?php

session_start();

define('DS', DIRECTORY_SEPARATOR);
define('ROOT_DIR', realpath(dirname(__FILE__)) . DS);

$image = ROOT_DIR . 'cache' . DS . $_SESSION['file_name'];
resizeImage($image, 400, 400);

抛出错误:

Warning: imagejpeg(C:\wamp\www\mvc\cache\temp_1385972631.jpg): failed to open stream: Invalid argument in C:\wamp\www\mvc\application\helpers\image_helper.php on line 95

线#95:imagejpeg($newImage, $image, 100);

这里是resizeImage()函数:

<?php

function resizeImage($image, $max_width, $max_height) {

    $img = getimagesize($image);

    $mime = $img['mime'];
    $width = $img[0];
    $height = $img[1];

    if ($height > $width) {
        $scale = $max_height / $height;
    }
    elseif ($width > $height) {
        $scale = $max_width / $width;
    }
    else {
        $scale = 1;
    }

    $newImageWidth = ceil($width * $scale);
    $newImageHeight = ceil($height * $scale);
    $newImage = imagecreatetruecolor($newImageWidth, $newImageHeight);
    imagealphablending($newImage, false);
    imagesavealpha($newImage, true);
    switch ($mime) {
        case "image/gif":
            $source = imagecreatefromgif($image);
            break;
        case "image/pjpeg":
        case "image/jpeg":
        case "image/jpg":
            $source = imagecreatefromjpeg($image);
            break;
        case "image/png":
        case "image/x-png":
            $source = imagecreatefrompng($image);
            break;
    }
    imagecopyresampled($newImage, $source, 0, 0, 0, 0, $newImageWidth, $newImageHeight, $width, $height);

    switch ($mime) {
        case "image/gif":
            imagegif($newImage, $image);
            break;
        case "image/pjpeg":
        case "image/jpeg":
        case "image/jpg":
            imagejpeg($newImage, $image, 100); // line #95
            break;
        case "image/png":
        case "image/x-png":
            imagepng($newImage, $image);
            break;
    }

    chmod($image, 0777);
    return $image;
}

编辑:imagejpeg之前添加的var_dump($ newImage,$图像,100); //线#102

//var_dump($newImage);
resource(18) of type (gd)

//var_dump($image);
string(41) "C:\wamp\www\mvc\cache\temp_1385978306.jpg"

Warning: imagejpeg(C:\wamp\www\mvc\cache\temp_1385978306.jpg): failed to open stream: Invalid argument in C:\wamp\www\mvc\application\helpers\image_helper.php on line 102
php
1个回答
1
投票

我有同样的问题。这可能是因为你没有创建一个名为“缓存”的文件夹。 (似乎GD不创建目录,并假定目录之前创建的,并已所需的权限(例如0777)。)


推荐问答