2009-12-19 125 views
1

我正在使用目錄迭代器來遍歷目錄,並調整在該目錄中找到的圖像,我這樣做來自瀏覽器,因爲我沒有ssh訪問該服務器。 大多數圖片調整大小,但每10張圖片(或多或少)我得到jiberish數據。 我認爲這是一個圖片來源。在那個數據總是有一個字符串CREATOR: gd-jpeg v1.0所以我想知道這是什麼?我用@ sign禁用了任何錯誤輸出。php調整大小圖像腳本

編輯:

下面是代碼的,也是我禁用錯誤輸出的原因沒有任何錯誤,我認爲禁用錯誤輸出將禁用此jiberish數據,但數據顯示不管。

代碼:

<?php 
/* 
big = 350 
thumb = 90 
thumb2 = 70 
top = 215 
*/ 

set_time_limit(0); 
ini_set('memory_limit', '128M'); 
ini_set('display_errors', 'On'); 

class ResizeImages 
{ 
    private $dir = 'images/articles_backup_2009-12-19'; 
    private $imageType = array(
     '_big' => 'h:350', 
     '_thumb' => 'm:90', 
     '_thumb2' => 'h:70', 
     '_top' => 'h:215' 
    ); 

    public function __construct() 
    { 
     $this->deleteImages(); 
     $this->resizeImages(); 
    } 

    private function resizeImages() 
    { 
     $n = 0; 
     $dh = opendir($this->dir); 
     while (($file = readdir($dh)) !== false) 
     { 
      if(is_dir($this->dir."/".$file) && $file != '.' && $file != '..') 
      { 
       echo $this->dir."/".$file.'<br />'; 
       $deldir = opendir($this->dir."/".$file); 
       while (($filedel = readdir($deldir)) !== false) 
       { 
        if ($filedel != '.' && $filedel != '..' && $filedel != 'Thumbs.db') 
        { 
         $val = $this->resize($this->dir."/".$file."/".$filedel); 
         $n++; 
        } 
       } 
      } 
     } 
     closedir($dh); 
    } 

    private function resize($target) 
    { 
     $img = $target; 

     $origSize = getimagesize($img); 
     $origWidth = $origSize[0]; 
     $origHeight = $origSize[1]; 

     foreach($this->imageType as $key=>$value) 
     { 
      $attr = explode(':', $value); 

      if(strpos($attr[0], 'w') !== false) 
      { 
       $this->imageWidth = $attr[1]; 
       $this->imageHeight = false; 
      } 
      if(strpos($attr[0], 'h') !== false) 
      { 
       $this->imageHeight = $attr[1]; 
       $this->imageWidth = false; 
      } 

      $imageTmp = explode('.', $img); 
      if(count($imageTmp) == 2) $image_name_fin = $imageTmp[0].$key.'.'.$imageTmp[1]; 
      else if(count($imageTmp) == 4) $image_name_fin = $imageTmp[0].'.'.$imageTmp[1].$key.'.'.$imageTmp[2]; 

      if($this->imageWidth != false) 
      { 
       if($origWidth <= $this->imageWidth) 
       { 
        $resizeHeight = $origHeight; 
        $resizeWidth = $origWidth; 
       } 
       else 
       { 
        $resizeHeight = round($origHeight/($origWidth/$this->imageWidth)); 
        $resizeWidth = $this->imageWidth; 
       } 
      } 
      else if($this->imageHeight != false) 
      { 
       if($origHeight <= $this->imageHeight) 
       { 
        $resizeHeight = $origHeight; 
        $resizeWidth = $origWidth; 
       } 
       else 
       { 
        $resizeWidth = round($origWidth/($origHeight/$this->imageHeight)); 
        $resizeHeight = $this->imageHeight; 
       } 
      } 

      $im = ImageCreateFromJPEG ($img) or // Read JPEG Image 
      $im = ImageCreateFromPNG ($img) or // or PNG Image 
      $im = ImageCreateFromGIF ($img) or // or GIF Image 
      $im = false; // If image is not JPEG, PNG, or GIF 

      if (!$im) 
      { 
       $this->error = array(
        'error' => true, 
        'notice' => 'UPLOADUNSUCCESSFULL' 
       ); 
       return $this->error; 
      } 

      $thumb = ImageCreateTrueColor ($resizeWidth, $resizeHeight); 
      ImageCopyResampled ($thumb, $im, 0, 0, 0, 0, $resizeWidth, $resizeHeight, $origWidth, $origHeight); 
      ImageJPEG ($thumb, $image_name_fin, $this->imageQuality); 
      //echo $image_name_fin.'<br />'; 
     } 

     $this->error = array(
      'imageUrl' => $image_name, 
      'error' => false, 
      'notice' => 'IMAGEUPLOADED' 
     ); 
     return $this->error;    
    } 

    private function deleteImages() 
    { 
     $dh = opendir($this->dir); 
     while (($file = readdir($dh)) !== false) 
     { 
      if(is_dir($this->dir."/".$file)) 
      { 
       //echo $file.'<br />'; 
       $deldir = opendir($this->dir."/".$file); 
       while (($filedel = readdir($deldir)) !== false) 
       { 
        if(strpos($this->dir."/".$file."/".$filedel, '_big.') !== false || strpos($this->dir."/".$file."/".$filedel, '_thumb.') !== false || strpos($this->dir."/".$file."/".$filedel, '_thumb2.') !== false || strpos($this->dir."/".$file."/".$filedel, '_top.') !== false) 
        { 
         unlink($this->dir."/".$file."/".$filedel); 
        } 
       } 
      } 
     } 
     closedir($dh); 
    } 
} 

$batch = new ResizeImages; 


?> 

回答

2

在頂部加入這樣的:

error_reporting(E_ALL); 

,試着改變這樣的:

$im = ImageCreateFromJPEG ($img) or // Read JPEG Image 
$im = ImageCreateFromPNG ($img) or // or PNG Image 
$im = ImageCreateFromGIF ($img) or // or GIF Image 
$im = false; // If image is not JPEG, PNG, or GIF 

要這樣:

$im = ImageCreateFromString(file_get_contents($img)); 

有沒有幫助?還有損壞的圖像上有任何模式?他們都是同一類型嗎?

+0

Tnx,this error_reporting(E_ALL);似乎ini_set('display_errors','on');沒有做到這一點。 我有一些錯誤,我修好了,謝謝。 – dfilkovi 2009-12-19 16:27:25

1

好了,第一件事是刪除錯誤抑制,看看是否有任何錯誤。看到你的一些代碼也會有幫助。

編輯 好吧,爲時已晚,以解決您的問題,但這裏是您的代碼的另一個建議。所有這些「readdir,決定文件,構建路徑」的東西只是一個使用(和看)的痛苦。嘗試此替代:

class ImageFilterIterator extends FilterIterator 
{ 
    public function accept() 
    { 
     $ext = pathinfo($this->current()->getRealPath(), PATHINFO_EXTENSION); 
     return stripos('.gif|.jpg|.png', $ext); 
    } 
} 

$path = '.'; 
$images = new ImageFilterIterator(
       new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator($path))); 

Iterators來自SPL雖然他們有點令人費解,在首先使用,它們可以使開發更容易,一旦你瞭解他們。通過以上你現在可以遍歷$圖像並獲得.GIF,.JPG或結束從下面路徑中的所有目錄png格式,這樣所有文件名:

foreach($images as $image) { 
    echo $image; 
} 

事實上,$形象不只是一個字符串,但一個SplFileInfo對象,所以你也得到了一堆有用的其他方法。

+0

來源添加到帖子 – dfilkovi 2009-12-19 15:49:07