Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php图像切片,图像采用白色背景_Php_Image - Fatal编程技术网

php图像切片,图像采用白色背景

php图像切片,图像采用白色背景,php,image,Php,Image,我需要以水平方式对图像进行切片。为此,我使用了下面的代码 $filename = 'test.jpg'; $fileData = getimagesize($filename); $source_handle = ImageCreateFromJPEG($filename); $imageW = $fileData[0]; $imageH = $fileData[1]; /* Cropping Parameters */ $part1X = 0; $part1Y = 0; $part1W

我需要以水平方式对图像进行切片。为此,我使用了下面的代码

$filename = 'test.jpg';
$fileData = getimagesize($filename);

$source_handle = ImageCreateFromJPEG($filename);

$imageW = $fileData[0];
$imageH = $fileData[1];

/* Cropping Parameters */
$part1X = 0;
$part1Y = 0;
$part1W = $imageW;
$part1H = $imageH/2;

$part2X = 0;
$part2Y = $imageH/2;
$part2W = $imageW;
//$part2H = $imageH/2;
$part2H = $imageH;


$to_crop_array = array('x' =>$part1X , 'y' =>$part1Y, 'width' => $part1W, 'height'=> $part1H);
$imgCroped1 = imagecrop($source_handle, $to_crop_array);

$to_crop_array = array('x' =>$part2X , 'y' =>$part2Y, 'width' => $part2W, 'height'=> $part2H);
$imgCroped2 = imagecrop($source_handle, $to_crop_array);

/* Image Split */
imagejpeg($imgCroped1, '1a.jpg', 100);
imagejpeg($imgCroped2, '1b.jpg', 100);
通过此代码,图像被分为两部分。但有些图像是用黑色背景保存的。我需要把这个黑色背景换成白色

为此,我使用了下面的代码

// set background to white
$white = imagecolorallocate($imgCroped2, 255, 255, 255);
imagefill($imgCroped2, 0, 0, $white);    
imagejpeg($imgCroped2, 'xx.jpg', 100);
但白色背景不适用。如何更改此代码以对切片图像应用白色背景。请帮帮我