2014-01-07 49 views
0

我試圖在裁剪到一個目錄後保存裁剪後的圖像,但是當我點擊修剪按鈕時什麼也沒有發生。我從2天內陷入困境,請別人幫我解決。 我得到這些錯誤修剪後不保存到目錄中的圖像

Warning:imagecreatefromjpeg(public/image /)[function.imagecreatefromjpeg]:無法打開流:C:\ wamp \ www \ ModuleEx.com \ application \ modules中沒有這樣的文件或目錄\ admin \ controllers \ IndexController.php on line 64

警告:imagecreatefromjpeg(public/image /)[function.imagecreatefromjpeg]:無法打開流:C:\ wamp \ www \ ModuleEx中沒有這樣的文件或目錄。 com \ application \ modules \ admin \ controllers \ IndexController.php on line 64

Warning:imagejpeg()[function.imagejpeg]:無法打開'public/image/crop'進行寫入:權限被拒絕在C:\ WAMP \ WWW \ ModuleEx .COM \應用\模塊\管理員\控制器\ IndexController.php上線71

這是我的控制器代碼

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 = 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; 




       } 
       } 

      } 
     } 
    } 

這是我的PHTML代碼。

if ($_SERVER['REQUEST_METHOD'] == 'POST') 
{ 
$targ_w = $targ_h = 150; 
$jpeg_quality = 90; 

$src = 'file_obj'; 
$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,'file_obj',$jpeg_quality); 

    } 


    ?> 




<html> 
    <head> 

    <script src="public/js/jquery.min.js"></script> 
    <script src="public/js/jquery.Jcrop.js"></script> 
    <script src="public/js/jquery.color.js"></script> 
    <script type="text/javascript"> 


    $(function(){ 

    $('#cropbox').Jcrop({ 
aspectRatio: 1, 
onSelect: updateCoords 
}); 

    }); 

    function updateCoords(c) 
    { 
    $('#x').val(c.x); 
$('#y').val(c.y); 
$('#w').val(c.w); 
$('#h').val(c.h); 
}; 

function checkCoords() 
    { 
if (parseInt($('#w').val())) return true; 
alert('Please select a crop region then press submit.'); 
return false; 
}; 

    </script> 


    <style type="text/css"> 
    #target { 
    background-color: #ccc; 
    width: 500px; 
    height: 330px; 
    font-size: 24px; 
    display: block; 
    } 


    </style> 


<?=$this->headLink()->appendStylesheet('/public/css/demos.css');?> 
<?=$this->headLink()->appendStylesheet('/public/css/main.css');?> 
<?=$this->headLink()->appendStylesheet('/public/css/jquery.Jcrop.min.css');?> 
</head> 

<body> 
    <form id="file_form" method="POST" enctype="multipart/form-data" action="" onsubmit=""> 
    <input type="file" name="file" /> 

    <input type="submit" name="submit" value="Upload"/><br/></br> 
    <img id="cropbox" src="<?php echo $this->file_obj?>" alt="Image" style="display: block; visibility: visible; width: 602px; height: 400px; border: medium none; opacity: 0.5;"/><br/> 

     <input type="hidden" id="x" name="x" /> 
     <input type="hidden" id="y" name="y" /> 
     <input type="hidden" id="w" name="w" /> 
     <input type="hidden" id="h" name="h" /> 
     <input type="submit" value="Crop Image" class="btn btn-large btn-inverse" /> 
    </form> 


</body> 
</html> 

回答

0

也許這會有所幫助。 *特別是你的文件夾(公共/圖像)或其路徑存儲?它是否與您的IndexController.php位於相同的目錄?

這是我的代碼保存在一個文件夾中的圖像: $ path =「uploads /」;

$actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext; 
$tmp = $_FILES['photoimg']['tmp_name']; 
if(move_uploaded_file($tmp, $path.$actual_image_name)) 
{ 
    mysql_query("UPDATE lms_users SET userPic='$actual_image_name' WHERE userId='$_SESSION[email]'");      
    echo "<img src='uploads/".$actual_image_name."' class='preview'>"; 
} 
else 
echo "failed"; 

希望這會有所幫助。

+0

這是圖像目錄路徑C:\ WAMP \ WWW \ ModuleEx.com \ PUBLIC \圖像 – Tuhin

+0

,這是控制路徑C:\ WAMP \ WWW \ ModuleEx.com \應用\ modules \ admin \ controllers – Tuhin

+0

嘗試製作一個公共/圖像文件夾以及您的IndexController.php目錄。您的系統/程序將在此代碼中搜索public/image以及indexController.php: $ file_obj =「public/image /".$ file_name; – Aljie

2

試試這個:

$names = $_FILES["img"]["name"]; 
    $tmp = $_FILES["img"]["tmp_name"]; 
    $upload_dir = './images'; 
    $move=move_uploaded_file($tmp, "$upload_dir/$names"); 
    $a="./images/".$names; 
    list($width, $height) = getimagesize($a); 
    $newwidth = "120"; 
    $newheight = "100"; 
    $thumb = imagecreatetruecolor($newwidth, $newheight); 
    if($_FILES["img"]["type"]=="image/jpeg"){ 
     $source = imagecreatefromjpeg($a); 
    imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 
    imagejpeg($thumb, $a, 100); 
    } 
+0

感謝這個代碼@ssk,但我仍然沒有得到這樣的文件或目錄錯誤@此行列表($ width,$ height)= getimagesize($ a); – Tuhin

+0

那麼問題是隻與您的圖像路徑...您的文件已成功移動到公用文件夾?檢查$ move .. if($ move){echo「file moved」;} else {echo「file not moved」;} – shruti

+0

是上傳文件成功移動到/ public/image文件夾後,但選擇了剪切區域後當我點擊裁剪按鈕然後裁剪圖像不保存到該目錄。 – Tuhin