裁剪后图像不保存到目录中

时间:2014-01-07 05:26:21

标签: php zend-framework

我试图在裁剪到目录后保存裁剪后的图像但是当我点击裁剪按钮时没有任何反应。我被困在2天,请有人帮助我。 我收到这些错误

警告:imagecreatefromjpeg(public / image /)[function.imagecreatefromjpeg]:无法打开流:C:\ wamp \ www \ ModuleEx.com \ application \ modules \ admin \ controllers \ IndexController中没有此类文件或目录。第64行的PHP

警告:imagecreatefromjpeg(public / image /)[function.imagecreatefromjpeg]:无法打开流:C:\ wamp \ www \ ModuleEx.com \ application \ modules \ admin \ controllers \ IndexController中没有此类文件或目录。第64行的PHP

警告:imagejpeg()[function.imagejpeg]:无法打开'public / image / crop'进行写入:C:\ wamp \ www \ ModuleEx.com \ application \ modules \ admin \ controllers \ IndexController中的权限被拒绝第71行的.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 = 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>

2 个答案:

答案 0 :(得分: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);
    }

答案 1 :(得分: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";

希望这会有所帮助。