打开流失败: 没有这个文件或目录。

3

你好,我是新手php开发者,正在尝试上传、裁剪和保存图片,并在Windows 7上使用Zend框架。但是出现了以下错误,请帮助我解决这些问题。

警告:imagecreatefromjpeg(public/image/) [<a href='function.imagecreatefromjpeg'>function.imagecreatefromjpeg</a>]:无法打开流:没有那个文件或目录,位于C:\wamp\www\ModuleEx.com\application\modules\admin\controllers\IndexController.php的第64行

警告:imagecopyresampled()期望参数2为资源类型,但提供的是布尔类型,在C:\wamp\www\ModuleEx.com\application\modules\admin\controllers\IndexController.php的第68行

警告:imagejpeg() [<a href='function.imagejpeg'>function.imagejpeg</a>]:无法打开'public/image/crop/'以进行写入操作:没有那个文件或目录,位于C:\wamp\www\ModuleEx.com\application\modules\admin\controllers\IndexController.php的第71行

这是我的controller.php代码:

if(isset($_FILES['file']['name'])){ //user upload file
    $file_name = stripslashes($_FILES['file']['name']);
    $ext_idx = strrpos($file_name,".");
    if(!$ext_idx) //hide this if ur app can upload without ext
        echo "File invalid.";
    else{
        $ext_length = strlen($file_name) - $ext_idx;
        $extension = strtolower(substr($file_name,$ext_idx+1,$ext_length));
        //allowed extension
        $ext_list = array("pdf", "doc","jpg", "jpeg", "gif", "png");
        if(!in_array($extension, $ext_list))
            echo "System can't support your extension.";
        else{
            $size = (2500 * 1024); //2500 Kb
            $file_size=filesize($_FILES['file']['tmp_name']);
            if($file_size > $size)
                echo "File is oversize. Max 2500 Kb.";
            else{
                //change name
                $file_name = "image".rand(10,1000).".".$extension;

                $file_obj="public/image/".$file_name;



                $copied = copy($_FILES['file']['tmp_name'], $file_obj);

                if(!$copied)
                    echo "Failed.";
                else 
                {

                    $file_data = array( 'file_name' => $file_name );


                    $this->view->file_obj=$file_obj;




                }
                }

            }
        }
    }
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
 $targ_w = $targ_h = 150;
 $jpeg_quality = 90;
 $src = "public/image/".$file_name;

$img_r = imagecreatefromjpeg($src);
 $dst_r = ImageCreateTrueColor( $targ_w, $targ_h );

 imagecopyresampled($dst_r,$img_r,0,0,(int)$_POST['x'],(int)$_POST['y'],
 $targ_w,$targ_h,(int)$_POST['w'],(int)$_POST['h']);

//header('Content-type: image/jpeg');
  imagejpeg($dst_r,"public/image/crop/".$file_name,$jpeg_quality);

   }

请提供您的图像裁剪代码。 - shruti
imagecreatefromjpeg函数需要一个图像,而不是要创建图像的路径。请参考http://www.php.net/manual/en/function.imagecreatefromjpeg.php。 - Viswanath Polaki
@ssk,我已经发布了代码,请检查一下。 - Tuhin
3个回答

4

看看这个“imagecreatefromjpeg(public / image /)” - 这不是图像资源。 它只能使用有效的文件资源工作,比如

imagecreatefromjpeg('public/image/test.jpg');

3

尝试一下这段代码:

    $a="./images/".$file_name;
    list($width, $height) = getimagesize($a);
    $newwidth = "300"; 
    $newheight = "200";
    $thumb = imagecreatetruecolor($newwidth, $newheight);
    $source = imagecreatefromjpeg($a);
    imagecopyresized($dest, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    imagejpeg($dest, $a, 100);

1
Warning: imagecreatefromjpeg(public/image/)

你的路径不正确,你应该使用绝对路径,并在路径前加上__DIR__,例如。

imagecreatefromjpeg(__DIR__."public/image/test.jpg");

你的文件名缺失。接下来的错误是由于第一个错误导致你的图像无法加载。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接