2014-01-07 162 views
2

嗨,我是新来的PHP和我正在上传图像和裁剪,并保存在Windows 7上zend上传和裁剪图像,但它给了我这些错误。请帮我解决这些错误。无法打开流:没有这样的文件或目录

Warning: imagecreatefromjpeg(public/image/) [<a href='function.imagecreatefromjpeg'>function.imagecreatefromjpeg</a>]: failed to open stream: No such file or directory in C:\wamp\www\ModuleEx.com\application\modules\admin\controllers\IndexController.php on line 64

Warning: imagecopyresampled() expects parameter 2 to be resource, boolean given in C:\wamp\www\ModuleEx.com\application\modules\admin\controllers\IndexController.php on line 68

Warning: imagejpeg() [<a href='function.imagejpeg'>function.imagejpeg</a>]: Unable to open 'public/image/crop/' for writing: No such file or directory in C:\wamp\www\ModuleEx.com\application\modules\admin\controllers\IndexController.php on line 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); 

    } 
+0

也请发表您的图像裁剪代码。 – shruti

+0

imagecreatefromjpeg预计图像不是必须创建图像的路径。遵循http://www.php.net/manual/en/function.imagecreatefromjpeg.php –

+0

@ssk我已经发布的代码,请检查它 – Tuhin

回答

3

看看这个“imagecreatefromjpeg(public/image /)” - 这不是一个图像资源。它只有一个有效的文件资源就像

imagecreatefromjpeg('public/image/test.jpg'); 
1
Warning: imagecreatefromjpeg(public/image/) 

您的路径是不正确的,你应该使用绝对路径与__DIR__例如。

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

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

+0

但我已经在index.php文件中定义的根路径 – Tuhin

2

试试这个代码:

$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); 
相关问题