2010-05-26 120 views

回答

10
<?php 
$width = 100; 
$height = 100; 

$source = @imagecreatefromjpeg("source.jpg"); 
$source_width = imagesx($source); 
$source_height = imagesy($source); 

for($col = 0; $col < $source_width/$width; $col++) 
{ 
    for($row = 0; $row < $source_height/$height; $row++) 
    { 
     $fn = sprintf("img%02d_%02d.jpg", $col, $row); 

     echo("$fn\n"); 

     $im = @imagecreatetruecolor($width, $height); 
     imagecopyresized($im, $source, 0, 0, 
      $col * $width, $row * $height, $width, $height, 
      $width, $height); 
     imagejpeg($im, $fn); 
     imagedestroy($im); 
     } 
    } 
    ?> 

上面的代碼從源文件獲取輸入:「source.jpg」。它將文件分割成100x100像素並將文件命名爲img00_01.jpg等等。您可以通過更改$ height和$ width參數來更改所得圖像的高度和寬度。

+0

如何以及在哪裏看到這些分割圖像? – DRAJI 2014-07-09 07:22:29

0

閱讀關於PHP GD library。你將需要如下方法:imagecreatefromjpeg()(或其他,取決於你的源文件),imagecreatetruecolor(),imagecopy(),imagejpeg()。