2012-04-25 48 views
0

我有一個96x96的圖像,我需要將它分成36個16x16,我有一個腳本,下面給出的腳本可以在我的本地主機上正常工作,但不能在我的主機上工作。將照片劃分爲X和Y部分

function makeTiles($name, $imageFileName, $crop_width, $crop_height) 
    { 
    $dir = "/pic"; 
    $slicesDir = "/pic/16X16"; 

    // might be good to check if $slicesDir exists etc if not create it. 
    $ImgExt = split(".",$imageFileName); 
    $inputFile = $dir . $imageFileName; 

    $img = new Imagick($inputFile); 
    $imgHeight = $img->getImageHeight(); 
    $imgWidth = $img->getImageWidth(); 

    $cropWidthTimes = ceil($imgWidth/$crop_width); 
    $cropHeightTimes = ceil($imgHeight/$crop_height); 
    for($i = 0; $i < $cropWidthTimes; $i++) 
    { 
     for($j = 0; $j < $cropHeightTimes; $j++) 
     { 
      $img = new Imagick($inputFile); 
      $x = ($i * $crop_width); 
      $y = ($j * $crop_height); 
      $img->cropImage($crop_width, $crop_height, $x, $y); 
      $data = $img->getImageBlob(); 
      $newFileName = $slicesDir . $name . "_" . $x . "_" . $y . ".".$ImgExt[1]; 
      $result = file_put_contents ($newFileName, $data); 
     } 
    } 
} 

獲取致命錯誤

Fatal error: Class 'Imagick' not found in myfile.php line number 

我的主人說:

Imagick和圖像魔法都是一樣不幸的是,我們沒有 他們,因爲我們有這樣的二進制文件一個PHP模塊做ImageMagick二進制和 圖像魔法路徑是/ usr/local/bin。將此功能包含在您的 腳本中,並從您的末端檢查網站功能。

我不知道如何解決這個錯誤。

+0

我不知道,但也許一個包括與路徑的Imagick類的語句? – kjones 2012-04-25 05:29:10

回答

2

如果我理解正確的話,你將不得不使用exec()腳本調用imagick二進制:

exec('/usr/local/bin/convert '.$inputFile.' -crop 96x96+ox+oy '.$newFilename); 

oxoy應替換爲正確的偏移值。

這裏有幾個鏈接,可以幫助:

  1. How do I use Imagick binaries from php
  2. Imagick - Convert Command-line tool
+0

現在我的主機說''不可能在我們的服務器上運行腳本':(是否有任何其他方式將圖像分成塊而沒有'imagic' – 2012-04-25 05:48:41

+0

當然是的,您可以簡單地使用標準的GD函數, imagecreatefromjpg()','imagecopy()'等。查看GD和圖像函數:http://www.php.net/manual/en/ref.image.php – Cyclonecode 2012-04-25 05:51:10

+1

你應該嘗試與你的託管服務提供商,問他們如何從你的php腳本中使用'imagick'。第一條消息似乎表明它應該實際工作。 – Cyclonecode 2012-04-25 05:54:08