I am following this code to crop and resize images in php. However, when I use cutFromCenter function, it is automatically filling the image with black colour, I want to fill it with white.
public function cutFromCenter($width, $height) {
if ($width < $this->getWidth() && $width > $height) {
$this->resizeToWidth($width);
}
if ($height < $this->getHeight() && $width < $height) {
$this->resizeToHeight($height);
}
$x = ($this->getWidth() / 2) - ($width / 2);
$y = ($this->getHeight() / 2) - ($height / 2);
return $this->cut($x, $y, $width, $height);
}
usage:
$resizeimage->load('img/ori.jpg');
$resizeimage->cutFromCenter(320,250);
$resizeimage->save('img/new.jpg');
any way to make the function to fill the small images with white colour?
this is the output:

Ok so after about 20 mins fiddling i found your problem and fixed it i have also forked the file so you can download my version from GitHub link is below.
https://gist.github.com/barkermn01/a0ff9dd928ea78443259f8c055f608ac
public function cut($x, $y, $width, $height, $bgColor = null) {
$new_image = imagecreatetruecolor($width, $height);
imagecolortransparent($new_image, imagecolorallocate($new_image, 0, 0, 0));
imagealphablending($new_image, false);
imagesavealpha($new_image, false);
if($bgColor != null){
$rgb = explode(',', $bgColor);
$color = imagecolorallocate($new_image, $rgb[0], $rgb[1], $rgb[2]);
imagefill($new_image, 0, 0, $color);
}
ob_start ();
imagepng($this->image);
$imageStr = ob_get_clean();
$src_size = getimagesizefromstring($imageStr);
imagecopyresampled (
$new_image,
$this->image,
($width-$src_size[0])/2,
($height-$src_size[1])/2,
0,
0,
$src_size[0],
$src_size[1],
$src_size[0],
$src_size[1]);
$this->image = $new_image;
}
Explanation: So change the cut method to use image re-sample copy. Got the information of the source image though creating the image and then saving it into an buffer and using that buffer to get the size. added support for an RGB colour sting to be passed in case any one want the background a different colour.
public function cutFromCenter($width, $height, $bgColor = null) {
if ($width < $this->getWidth() && $width > $height) {
$this->resizeToWidth($width);
}
if ($height < $this->getHeight() && $width < $height) {
$this->resizeToHeight($height);
}
$x = ($this->getWidth() / 2) - ($width / 2);
$y = ($this->getHeight() / 2) - ($height / 2);
return $this->cut($x, $y, $width, $height, $bgColor);
}
Explanation Added support for the background colour RGB String to be passed though null it will default to GD's default.
$resizeimage->cutFromCenter(320,250, "255,255,255");
Allow an RGB colour string to be passed. in this case white.

It could be worth trying to fill the image created with imagecreatetruecolor with white before the original image is resized.
public function cut($x, $y, $width, $height) {
$new_image = imagecreatetruecolor($width, $height);
imagecolortransparent($new_image, imagecolorallocate($new_image, 0, 0, 0));
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
$image = imagecreatetruecolor(100, 100);
$white = imagecolorallocate($new_image, 255, 255, 255);
imagefill($new_image, 0, 0, $white);
imagecopy($new_image, $this->image, 0, 0, $x, $y, $width, $height);
$this->image = $new_image;
}
Have a look if this makes any difference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With