Zend - The PHP Company




Graphics

Add Code


ImageCrop  

Type: code fragment
Added by: DuckObalaNET
Entered: 06/04/2001
Last modified: 04/12/2000
Rating: - (fewer than 3 votes)
Views: 7206
copy a part of an image to another one and saves it to the disk


<?
//
//    image crop fuction ..... Duck@obala.net
//
// $cropX = source starting X 
// $cropY = source starting Y
// $cropW = crom weigth
// $cropH = crom heigh
// $source = source filename
// $desctination = desctination filename
// $type = image type

function im_crop($cropX,$cropY,$cropH,$cropW,$source,$destination,$type='jpeg') {
    
    switch (
$type) {
    case 
'wbmp'$sim ImageCreateFromWBMP($source); break;
        case 
'gif'$sim ImageCreateFromGIF($source); break;
    case 
'png'$sim ImageCreateFromPNG($source); break;
        default: 
$sim ImageCreateFromJPEG($source); break;
    }
    
    
$dim imagecreate($cropW$cropH); 
    

    for ( 
$i=$cropY$i<($cropY+$cropH); $i++ ) {
        for ( 
$j=$cropX$j<($cropX+$cropW); $j++ ) {
            
$color imagecolorsforindex($simimagecolorat($sim$j$i));
            
$index ImageColorAllocate($dim$color['red'], $color['green'], $color['blue']); 
            
imagesetpixel($dim$j-$cropX$i-$cropY$index);
        }
    }
    
imagedestroy($sim);

    
    switch (
$type) {
    case 
'wbmp'ImageWBMP($dim$destination); break;
        case 
'gif'ImageGIF($dim$destination); break;
    case 
'png'ImagePNG($dim$destination); break;
        default: 
ImageJPEG($dim$destination); break;
    }
    
imagedestroy($dim);
    
    
}
?>


Usage Example




Rate This Script





Search



This Category All Categories