2013-10-27 146 views
1

我正在嘗試調整圖像大小。它適用於單幅圖像。但對於多幅圖像,我必須使用循環逐個獲取圖像。所以我將類和它的函數保存在不同的文件中,以免得到類重新聲明的錯誤。所以我有3個文件一個是index.php,一個是class.php,另一個是functions.php,它們都在下面。在PHP中調整大小的圖像

我得到調用未定義的方法調整大小:: resizeImage()的錯誤

下面是我對指數代碼

<?php 


// *** Include the class 

$tmp = "image1.jpg,image2.jpg"; 

$single=explode(',',$tmp); 
foreach($single as $single){ 

// include_once("class.php"); 
include_once("class.php"); 

// *** 1) Initialise/load image 
$resizeObj = new resize('./images/'.$single.''); 

// *** 2) Resize image (options: exact, portrait, landscape, auto, crop) 
$resizeObj -> resizeImage(420, 290, 'auto'); 

// *** 3) Save image 
$resizeObj -> saveImage('1-'.$single.'', 100); 

} 

?>

而下面是我的課文件

Class resize 
    { 

     private $image; 
      private $width; 
      private $height; 
     private $imageResized; 
      } 

她和她E公司的職能文件

<?php 


     function __construct($fileName) 
     { 
      // *** Open up the file 
      $this->image = $this->openImage($fileName); 

      // *** Get width and height 
      $this->width = imagesx($this->image); 
      $this->height = imagesy($this->image); 
     } 

     ## -------------------------------------------------------- 

     private function openImage($file) 
     { 
      // *** Get extension 
      $extension = strtolower(strrchr($file, '.')); 

      switch($extension) 
      { 
       case '.jpg': 
       case '.jpeg': 
        $img = @imagecreatefromjpeg($file); 
        break; 
       case '.gif': 
        $img = @imagecreatefromgif($file); 
        break; 
       case '.png': 
        $img = @imagecreatefrompng($file); 
        break; 
       default: 
        $img = false; 
        break; 
      } 
      return $img; 
     } 

     ## -------------------------------------------------------- 

     public function resizeImage($newWidth, $newHeight, $option="auto") 
     { 
      // *** Get optimal width and height - based on $option 
      $optionArray = $this->getDimensions($newWidth, $newHeight, $option); 

      $optimalWidth = $optionArray['optimalWidth']; 
      $optimalHeight = $optionArray['optimalHeight']; 


      // *** Resample - create image canvas of x, y size 
      $this->imageResized = imagecreatetruecolor($optimalWidth, $optimalHeight); 
      imagecopyresampled($this->imageResized, $this->image, 0, 0, 0, 0, $optimalWidth, $optimalHeight, $this->width, $this->height); 


      // *** if option is 'crop', then crop too 
      if ($option == 'crop') { 
       $this->crop($optimalWidth, $optimalHeight, $newWidth, $newHeight); 
      } 
     } 

     ## -------------------------------------------------------- 

     private function getDimensions($newWidth, $newHeight, $option) 
     { 

      switch ($option) 
      { 
       case 'exact': 
        $optimalWidth = $newWidth; 
        $optimalHeight= $newHeight; 
        break; 
       case 'portrait': 
        $optimalWidth = $this->getSizeByFixedHeight($newHeight); 
        $optimalHeight= $newHeight; 
        break; 
       case 'landscape': 
        $optimalWidth = $newWidth; 
        $optimalHeight= $this->getSizeByFixedWidth($newWidth); 
        break; 
       case 'auto': 
        $optionArray = $this->getSizeByAuto($newWidth, $newHeight); 
        $optimalWidth = $optionArray['optimalWidth']; 
        $optimalHeight = $optionArray['optimalHeight']; 
        break; 
       case 'crop': 
        $optionArray = $this->getOptimalCrop($newWidth, $newHeight); 
        $optimalWidth = $optionArray['optimalWidth']; 
        $optimalHeight = $optionArray['optimalHeight']; 
        break; 
      } 
      return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight); 
     } 

     ## -------------------------------------------------------- 

     private function getSizeByFixedHeight($newHeight) 
     { 
      $ratio = $this->width/$this->height; 
      $newWidth = $newHeight * $ratio; 
      return $newWidth; 
     } 

     private function getSizeByFixedWidth($newWidth) 
     { 
      $ratio = $this->height/$this->width; 
      $newHeight = $newWidth * $ratio; 
      return $newHeight; 
     } 

     private function getSizeByAuto($newWidth, $newHeight) 
     { 
      if ($this->height < $this->width) 
      // *** Image to be resized is wider (landscape) 
      { 
       $optimalWidth = $newWidth; 
       $optimalHeight= $this->getSizeByFixedWidth($newWidth); 
      } 
      elseif ($this->height > $this->width) 
      // *** Image to be resized is taller (portrait) 
      { 
       $optimalWidth = $this->getSizeByFixedHeight($newHeight); 
       $optimalHeight= $newHeight; 
      } 
      else 
      // *** Image to be resizerd is a square 
      { 
       if ($newHeight < $newWidth) { 
        $optimalWidth = $newWidth; 
        $optimalHeight= $this->getSizeByFixedWidth($newWidth); 
       } else if ($newHeight > $newWidth) { 
        $optimalWidth = $this->getSizeByFixedHeight($newHeight); 
        $optimalHeight= $newHeight; 
       } else { 
        // *** Sqaure being resized to a square 
        $optimalWidth = $newWidth; 
        $optimalHeight= $newHeight; 
       } 
      } 

      return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight); 
     } 

     ## -------------------------------------------------------- 

     private function getOptimalCrop($newWidth, $newHeight) 
     { 

      $heightRatio = $this->height/$newHeight; 
      $widthRatio = $this->width/$newWidth; 

      if ($heightRatio < $widthRatio) { 
       $optimalRatio = $heightRatio; 
      } else { 
       $optimalRatio = $widthRatio; 
      } 

      $optimalHeight = $this->height/$optimalRatio; 
      $optimalWidth = $this->width/$optimalRatio; 

      return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight); 
     } 

     ## -------------------------------------------------------- 

     private function crop($optimalWidth, $optimalHeight, $newWidth, $newHeight) 
     { 
      // *** Find center - this will be used for the crop 
      $cropStartX = ($optimalWidth/2) - ($newWidth /2); 
      $cropStartY = ($optimalHeight/ 2) - ($newHeight/2); 

      $crop = $this->imageResized; 
      //imagedestroy($this->imageResized); 

      // *** Now crop from center to exact requested size 
      $this->imageResized = imagecreatetruecolor($newWidth , $newHeight); 
      imagecopyresampled($this->imageResized, $crop , 0, 0, $cropStartX, $cropStartY, $newWidth, $newHeight , $newWidth, $newHeight); 
     } 

     ## -------------------------------------------------------- 

     public function saveImage($savePath, $imageQuality="100") 
     { 
      // *** Get extension 
      $extension = strrchr($savePath, '.'); 
      $extension = strtolower($extension); 

      switch($extension) 
      { 
       case '.jpg': 
       case '.jpeg': 
        if (imagetypes() & IMG_JPG) { 
         imagejpeg($this->imageResized, $savePath, $imageQuality); 
        } 
        break; 

       case '.gif': 
        if (imagetypes() & IMG_GIF) { 
         imagegif($this->imageResized, $savePath); 
        } 
        break; 

       case '.png': 
        // *** Scale quality from 0-100 to 0-9 
        $scaleQuality = round(($imageQuality/100) * 9); 

        // *** Invert quality setting as 0 is best, not 9 
        $invertScaleQuality = 9 - $scaleQuality; 

        if (imagetypes() & IMG_PNG) { 
         imagepng($this->imageResized, $savePath, $invertScaleQuality); 
        } 
        break; 

       // ... etc 

       default: 
        // *** No extension - No save. 
        break; 
      } 

      imagedestroy($this->imageResized); 
     } 

?>

請幫助我。提前致謝。

+0

我很困惑你的功能文件。這些類方法是否在實際類之外?這是行不通的。 – Kontrollfreak

+0

嗨kontrollfreak, 嗯,我把它們放在與class文件相同的文件中但是然後我得到了我在class.php中使用的每個函數的常見警告。警告是:** functionname()期望參數1是資源,布爾給定** – user1960856

+0

在foreach之前使用include_once(「class.php」),它會解決你的問題嗎? – nut

回答

1

請激活php配置值「display_errors」爲true,然後您會看到每次循環時嘗試包括resize類。

包含您的課程和腳本的頂部,然後重試。

0

我認爲你必須把你的類文件

Class resize 
     { 

     private $image; 
     private $width; 
     private $height; 
     private $imageResized; 

     function __construct($fileName) 
     { 
      // *** Open up the file 
      $this->image = $this->openImage($fileName); 

      // *** Get width and height 
      $this->width = imagesx($this->image); 
      $this->height = imagesy($this->image); 
     } 

     // All other functions here ... 

     } 
+0

嗯,我做到了。但是,我在課堂上使用的每個功能都得到警告。php – user1960856

+0

我不喜歡那部分'$ single = explode(',',$ tmp); foreach($ single作爲$ single)'你可以改變它爲'$ arrSingle = explode(',',$ tmp); foreach($ arrSingle as $ single)',如果這沒有幫助請'print_r(explode(',',$ tmp))''。 – electroid

+0

嗨electroid我改變了你的建議,但它也沒有幫助。我也打印出這個數組,它有兩個tmp數組的值。 我得到相同的警告所有的功能 ** _ functionname()_期望參數1是資源,布爾給定** – user1960856

0

首先裏面所有的功能,我建議你OOP的一些基本的瞭解,PHP:PHP: Classes and Objects - Manual

其次,我討厭做所有的的工作,但在這裏它是:

<?php 

Class resize 
{ 
    private $image; 
    private $width; 
    private $height; 
    private $imageResized; 

    function __construct($fileName) 
    { 
     // *** Open up the file 
     $this->image = $this->openImage($fileName); 

     // *** Get width and height 
     $this->width = imagesx($this->image); 
     $this->height = imagesy($this->image); 
    } 

    ## -------------------------------------------------------- 

    private function openImage($file) 
    { 
     // *** Get extension 
     $extension = strtolower(strrchr($file, '.')); 

     switch($extension) 
     { 
      case '.jpg': 
      case '.jpeg': 
       $img = @imagecreatefromjpeg($file); 
       break; 
      case '.gif': 
       $img = @imagecreatefromgif($file); 
       break; 
      case '.png': 
       $img = @imagecreatefrompng($file); 
       break; 
      default: 
       $img = false; 
       break; 
     } 
     return $img; 
    } 

    ## -------------------------------------------------------- 

    public function resizeImage($newWidth, $newHeight, $option="auto") 
    { 
     // *** Get optimal width and height - based on $option 
     $optionArray = $this->getDimensions($newWidth, $newHeight, $option); 

     $optimalWidth = $optionArray['optimalWidth']; 
     $optimalHeight = $optionArray['optimalHeight']; 


     // *** Resample - create image canvas of x, y size 
     $this->imageResized = imagecreatetruecolor($optimalWidth, $optimalHeight); 
     imagecopyresampled($this->imageResized, $this->image, 0, 0, 0, 0, $optimalWidth, $optimalHeight, $this->width, $this->height); 


     // *** if option is 'crop', then crop too 
     if ($option == 'crop') { 
      $this->crop($optimalWidth, $optimalHeight, $newWidth, $newHeight); 
     } 
    } 

    ## -------------------------------------------------------- 

    private function getDimensions($newWidth, $newHeight, $option) 
    { 

     switch ($option) 
     { 
      case 'exact': 
       $optimalWidth = $newWidth; 
       $optimalHeight= $newHeight; 
       break; 
      case 'portrait': 
       $optimalWidth = $this->getSizeByFixedHeight($newHeight); 
       $optimalHeight= $newHeight; 
       break; 
      case 'landscape': 
       $optimalWidth = $newWidth; 
       $optimalHeight= $this->getSizeByFixedWidth($newWidth); 
       break; 
      case 'auto': 
       $optionArray = $this->getSizeByAuto($newWidth, $newHeight); 
       $optimalWidth = $optionArray['optimalWidth']; 
       $optimalHeight = $optionArray['optimalHeight']; 
       break; 
      case 'crop': 
       $optionArray = $this->getOptimalCrop($newWidth, $newHeight); 
       $optimalWidth = $optionArray['optimalWidth']; 
       $optimalHeight = $optionArray['optimalHeight']; 
       break; 
     } 
     return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight); 
    } 

    ## -------------------------------------------------------- 

    private function getSizeByFixedHeight($newHeight) 
    { 
     $ratio = $this->width/$this->height; 
     $newWidth = $newHeight * $ratio; 
     return $newWidth; 
    } 

    private function getSizeByFixedWidth($newWidth) 
    { 
     $ratio = $this->height/$this->width; 
     $newHeight = $newWidth * $ratio; 
     return $newHeight; 
    } 

    private function getSizeByAuto($newWidth, $newHeight) 
    { 
     if ($this->height < $this->width) 
     // *** Image to be resized is wider (landscape) 
     { 
      $optimalWidth = $newWidth; 
      $optimalHeight= $this->getSizeByFixedWidth($newWidth); 
     } 
     elseif ($this->height > $this->width) 
     // *** Image to be resized is taller (portrait) 
     { 
      $optimalWidth = $this->getSizeByFixedHeight($newHeight); 
      $optimalHeight= $newHeight; 
     } 
     else 
     // *** Image to be resizerd is a square 
     { 
      if ($newHeight < $newWidth) { 
       $optimalWidth = $newWidth; 
       $optimalHeight= $this->getSizeByFixedWidth($newWidth); 
      } else if ($newHeight > $newWidth) { 
       $optimalWidth = $this->getSizeByFixedHeight($newHeight); 
       $optimalHeight= $newHeight; 
      } else { 
       // *** Sqaure being resized to a square 
       $optimalWidth = $newWidth; 
       $optimalHeight= $newHeight; 
      } 
     } 

     return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight); 
    } 

    ## -------------------------------------------------------- 

    private function getOptimalCrop($newWidth, $newHeight) 
    { 

     $heightRatio = $this->height/$newHeight; 
     $widthRatio = $this->width/$newWidth; 

     if ($heightRatio < $widthRatio) { 
      $optimalRatio = $heightRatio; 
     } else { 
      $optimalRatio = $widthRatio; 
     } 

     $optimalHeight = $this->height/$optimalRatio; 
     $optimalWidth = $this->width/$optimalRatio; 

     return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight); 
    } 

    ## -------------------------------------------------------- 

    private function crop($optimalWidth, $optimalHeight, $newWidth, $newHeight) 
    { 
     // *** Find center - this will be used for the crop 
     $cropStartX = ($optimalWidth/2) - ($newWidth /2); 
     $cropStartY = ($optimalHeight/ 2) - ($newHeight/2); 

     $crop = $this->imageResized; 
     //imagedestroy($this->imageResized); 

     // *** Now crop from center to exact requested size 
     $this->imageResized = imagecreatetruecolor($newWidth , $newHeight); 
     imagecopyresampled($this->imageResized, $crop , 0, 0, $cropStartX, $cropStartY, $newWidth, $newHeight , $newWidth, $newHeight); 
    } 

    ## -------------------------------------------------------- 

    public function saveImage($savePath, $imageQuality="100") 
    { 
     // *** Get extension 
     $extension = strrchr($savePath, '.'); 
     $extension = strtolower($extension); 

     switch($extension) 
     { 
      case '.jpg': 
      case '.jpeg': 
       if (imagetypes() & IMG_JPG) { 
        imagejpeg($this->imageResized, $savePath, $imageQuality); 
       } 
       break; 

      case '.gif': 
       if (imagetypes() & IMG_GIF) { 
        imagegif($this->imageResized, $savePath); 
       } 
       break; 

      case '.png': 
       // *** Scale quality from 0-100 to 0-9 
       $scaleQuality = round(($imageQuality/100) * 9); 

       // *** Invert quality setting as 0 is best, not 9 
       $invertScaleQuality = 9 - $scaleQuality; 

       if (imagetypes() & IMG_PNG) { 
        imagepng($this->imageResized, $savePath, $invertScaleQuality); 
       } 
       break; 

      // ... etc 

      default: 
       // *** No extension - No save. 
       break; 
     } 

     imagedestroy($this->imageResized); 
    } 
} 

這個替換您class.php,刪除的functions.php並確保您的圖像是可訪問的,因爲你的代碼不會TH如果這些文件無法訪問,請排除任何錯誤。

這是一個快速和骯髒的修復。不要依賴它。

+0

嗨kontrollfreak,嗯,我保持在類文件相同的文件中的類n功能但然後我對於我在class.php中使用的每個函數都會收到常見的警告,警告是:imagesx()期望參數1是資源,布爾給定..不僅僅是imagesx(),而是每個其他圖像相關的函數都會給出相同的警告。而且,它的單幅圖像處理效果非常好。所以圖像可以訪問。請指導我關於此警告。它嚇壞了我。 – user1960856