2014-03-03 128 views
0

我一直在尋找遍及互聯網尋找解決方案。我使用這個SimpleImage.php類來調整我在這裏找到的圖像的大小(http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php)。到目前爲止,一切正常。如果我嘗試重新調整圖像的大小以適應其高度或寬度,它正在完成其工作。但是,這隻適用於我試圖一次調整圖像大小的情況。假設我的圖像的原始尺寸是1400x1100,並且我想將其高度調整爲800.這將使我得到某些數字xx的結果。反之亦然。調整圖像的最大寬度和高度

我的目標是根據我傳遞的最大寬度和最大高度調整圖像大小。

事情是這樣的:

$image = new SimpleImage(); 
      $image->load($image_url); 
      $image->resizeToWidthHeight(MAX-WIDTH, MAX-HEIGHT); 
      $image->save('photo.png'); 

這裏是我的SimpleImage.php類

<?php 
    class SimpleImage { 
    var $image; 
    var $image_type; 

    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); 
     } 
    } 
    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); 
     } 
    } 

    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) { 
      imageAlphaBlending($this->image, true); 
      imageSaveAlpha($this->image, true); 
      imagepng($this->image); 
     } 
    } 

    function getWidth() { 
     return imagesx($this->image); 
    } 
    function getHeight() { 
     return imagesy($this->image); 
    } 
    function resizeToHeight($height) { 
     $ratio = $height/$this->getHeight(); 
     $width = $this->getWidth() * $ratio; 
     $this->resize($width,$height); 
    } 
    function resizeToWidth($width) { 
     $ratio = $width/$this->getWidth(); 
     $height = $this->getheight() * $ratio; 
     $this->resize($width,$height); 
    } 
    function scale($scale) { 
     $width = $this->getWidth() * $scale/100; 
     $height = $this->getheight() * $scale/100; 
     $this->resize($width,$height); 
    } 


    function resize($width,$height) { 

     // ADDED CODE IS HERE - NOT SURE WHY IT DOESN'T WORK FOR PNG 

     // Setup new image 
     $new_image = imagecreatetruecolor($width, $height); 
     // These parameters are required for handling PNG files. 
     imagealphablending($new_image, false); 
     imagesavealpha($new_image,true); 
     $transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127); 
     imagefilledrectangle($new_image, 0, 0, $width, $height, $transparent); 
     // Resize image 
     imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); 
     $this->image = $new_image; 
    } 
} 

?> 

回答

0

如果你的圖像變化的尺寸:4〜16:你從3去9,您需要在某處應用某種作物。

根據比例找出哪個維度最小,調整該特性的大小然後裁剪其他維度。


以我的經驗規模永遠是錯的。我建議你從圖書館中刪除它。調整大小+作物是要走的路。

你可以寫一個函數像cropTo($newX, $newY)

+0

你有任何其他的參考來幫助我嗎? – Bono

+0

我認爲這是一個很好的:http://stackoverflow.com/questions/1855996/crop-image-in-php – Halcyon

相關問題