【问题标题】:Opencart Upload Image From Customer PageOpencart 从客户页面上传图片
【发布时间】:2017-02-09 13:41:41
【问题描述】:

我想问如何从 Opencart 客户网站上传图片(例如个人资料图片)并调整其大小?我的代码可以上传图片,但它不能调整我的图片大小。如果我有 5MB 的图像文件,我想将文件缩小到 200KB 左右。我已经尝试过PHP函数imagejpeg,但它不起作用,它不会上传图像。这是我的代码:

//Check the post
if((!empty($_FILES)) && ($_FILES['photo']['size'] > 0) && ($_FILES['photo']['error'] == 0)){
//check extension
   $fileType = strtolower(pathinfo(basename($this->request->files['photo']['name']), PATHINFO_EXTENSION));
   if($fileType != "jpeg" && $fileType != "jpg" && $fileType != "png" && $fileType != "bmp"){
      echo "<script>
       alert('Extension allowed : jpeg, jpg, png, dan bmp. Your extension: ".$fileType."');
       window.location.href='javascript: window.history.go(-1)';
     </script>";
      die();
   }
  //check image size
   if($this->request->files['photo']['size'] > 5000000){
      echo "<script>
       alert('File too big, max. 5000000 (5MB). Your file: ".$this->request->files['photo']['size']."');
       window.location.href='javascript: window.history.go(-1)';
     </script>";
      die();
   }
   $fileName = $this->request->files['photo']['name'];
   $newimg = date('YmdHis', time());
   $arr = explode(".", $fileName);
   $extension = strtolower(array_pop($arr));
   $foto = $newimg.".".$extension;
   $uploads_dir = 'image/data/profile/';
   if (is_uploaded_file($this->request->files['photo']['tmp_name'])) {
   //i want to shrink image size before this upload
      move_uploaded_file($this->request->files['photo']['tmp_name'], $uploads_dir.$foto);
   }
}
else{
   echo "<script>
       alert('Please choose a photo');
       window.location.href='javascript: window.history.go(-1)';
     </script>";
      die();
   $foto = '';
}

【问题讨论】:

    标签: php image opencart


    【解决方案1】:

    您可以尝试以下代码 sn-p 在图片上传期间减小图片大小 -

    <?php
        function reduce_image_size($source_url, $destination_url, $quality) {
    
            $info = getimagesize($source_url);
    
            if ($info['mime'] == 'image/jpeg')
                $image = imagecreatefromjpeg($source_url); 
            else if ($info['mime'] == 'image/gif')
                $image = imagecreatefromgif($source_url); 
            else if ($info['mime'] == 'image/png')
                $image = imagecreatefrompng($source_url);
    
            imagejpeg($image, $destination_url, $quality);
            return $destination_url;
        }
    
        if($_POST) {
            if ($_FILES["file"]["error"] > 0) {
                $output = $_FILES["file"]["error"];
            } else if (($_FILES["file"]["type"] == "image/gif") ||
                    ($_FILES["file"]["type"] == "image/jpeg") ||
                    ($_FILES["file"]["type"] == "image/png") ||
                    ($_FILES["file"]["type"] == "image/pjpeg")) {
    
                $destination = 'reduced.jpg';
                $filename = reduce_image_size($_FILES["file"]["tmp_name"], $destination, 80);
                $output = 'Done.';
            } else {
                $output = "Uploaded image should be jpg or gif or png.";
            }
    
            echo $output . " <a href=''>Reload</a>"; die;
        }
    ?>
    <html>
        <head>
            <title>Reduce Image Size with PHP</title>
        </head>
        <body>
    <form action="" name="img_compress" id="img_compress" method="post" enctype="multipart/form-data">
                   Upload: <input type="file" name="file" id="file"/>
                   <input type="submit" name="submit" id="submit" class="submit btn-success"/>
       </form>
         </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 2015-09-06
      • 1970-01-01
      • 2016-01-25
      • 2011-08-16
      • 1970-01-01
      • 1970-01-01
      • 2011-08-02
      • 1970-01-01
      • 2011-12-12
      相关资源
      最近更新 更多