views:

26

answers:

2

Does anyone know of a PHP routine where I can take an original image and split it in half to create two new images A and B?

See below:

alt text

Thanks

A: 

Read about PHP GD library. You will need methods like: imagecreatefromjpeg() (or other, depends on your source file), imagecreatetruecolor(), imagecopy(), imagejpeg().

Piotr Pankowski
+3  A: 
<?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 );
        }
    } 
    ?>

The code above takes input from a source file : "source.jpg". It splits the file into 100x100 pixels and names the files img00_01.jpg and so on.... You can change the height, width of the resulting image by changing the $height and $width parameters..

Vimard