Resize image in PHP

前端 未结 13 1658
长情又很酷
长情又很酷 2020-11-22 04:18

I\'m wanting to write some PHP code which automatically resizes any image uploaded via a form to 147x147px, but I have no idea how to go about it (I\'m a relative PHP novice

13条回答
  •  鱼传尺愫
    2020-11-22 04:43

    ZF cake:

    load($_FILES[$html_element_name]['tmp_name']);
        $this->resize($new_img_width, $new_img_height);
        $this->save($target_file);
        return $target_file; 
        //return name of saved file in case you want to store it in you database or show confirmation message to user
    
    
    
      public function load($filename) {
    
          $image_info = getimagesize($filename);
          $this->image_type = $image_info[2];
          if( $this->image_type == IMAGETYPE_JPEG ) {
    
             $this->image = imagecreatefromjpeg($filename);
          } elseif( $this->image_type == IMAGETYPE_GIF ) {
    
             $this->image = imagecreatefromgif($filename);
          } elseif( $this->image_type == IMAGETYPE_PNG ) {
    
             $this->image = imagecreatefrompng($filename);
          }
       }
      public function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
    
          if( $image_type == IMAGETYPE_JPEG ) {
             imagejpeg($this->image,$filename,$compression);
          } elseif( $image_type == IMAGETYPE_GIF ) {
    
             imagegif($this->image,$filename);
          } elseif( $image_type == IMAGETYPE_PNG ) {
    
             imagepng($this->image,$filename);
          }
          if( $permissions != null) {
    
             chmod($filename,$permissions);
          }
       }
      public function output($image_type=IMAGETYPE_JPEG) {
    
          if( $image_type == IMAGETYPE_JPEG ) {
             imagejpeg($this->image);
          } elseif( $image_type == IMAGETYPE_GIF ) {
    
             imagegif($this->image);
          } elseif( $image_type == IMAGETYPE_PNG ) {
    
             imagepng($this->image);
          }
       }
      public function getWidth() {
    
          return imagesx($this->image);
       }
      public function getHeight() {
    
          return imagesy($this->image);
       }
      public function resizeToHeight($height) {
    
          $ratio = $height / $this->getHeight();
          $width = $this->getWidth() * $ratio;
          $this->resize($width,$height);
       }
    
      public function resizeToWidth($width) {
          $ratio = $width / $this->getWidth();
          $height = $this->getheight() * $ratio;
          $this->resize($width,$height);
       }
    
      public function scale($scale) {
          $width = $this->getWidth() * $scale/100;
          $height = $this->getheight() * $scale/100;
          $this->resize($width,$height);
       }
    
      public function resize($width,$height) {
          $new_image = imagecreatetruecolor($width, $height);
          imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
          $this->image = $new_image;
       }
    
      public function savepicAction() {
        ini_set('display_errors', 1);
        ini_set('display_startup_errors', 1);
        error_reporting(E_ALL);
    
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender();
        $this->_response->setHeader('Access-Control-Allow-Origin', '*');
    
        $this->db = Application_Model_Db::db_load();        
        $ouser = $_POST['ousername'];
    
    
          $fdata = 'empty';
          if (isset($_FILES['picture']) && $_FILES['picture']['size'] > 0) {
            $file_size = $_FILES['picture']['size'];
            $tmpName  = $_FILES['picture']['tmp_name'];  
    
            //Determine filetype
            switch ($_FILES['picture']['type']) {
                case 'image/jpeg': $ext = "jpg"; break;
                case 'image/png': $ext = "png"; break;
                case 'image/jpg': $ext = "jpg"; break;
                case 'image/bmp': $ext = "bmp"; break;
                case 'image/gif': $ext = "gif"; break;
                default: $ext = ''; break;
            }
    
            if($ext) {
              //if($file_size<400000) {  
                $img = $this->store_uploaded_image('picture', 90,82);
                //$fp      = fopen($tmpName, 'r');
                $fp = fopen($img, 'r');
                $fdata = fread($fp, filesize($tmpName));        
                $fdata = base64_encode($fdata);
                fclose($fp);
    
              //}
            }
    
          }
    
          if($fdata=='empty'){
    
          }
          else {
            $this->db->update('users', 
              array(
                'picture' => $fdata,             
              ), 
              array('username=?' => $ouser ));        
          }
    
    
    
      }  
    

提交回复
热议问题