php - white background for cutFromCenter function -


i following this code crop , resize images in php. however, when use cutfromcenter function, automatically filling image black colour, want fill 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 make function fill small images white colour?

this output:

example

ok after 20 mins fiddling found problem , fixed have forked file can download version github link below.

https://gist.github.com/barkermn01/a0ff9dd928ea78443259f8c055f608ac

what have done this.

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: change cut method use image re-sample copy. got information of source image though creating image , saving buffer , using buffer size. added support rgb colour sting passed in case 1 want background 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 background colour rgb string passed though null default gd's default.

usage changed to:

$resizeimage->cutfromcenter(320,250, "255,255,255"); 

allow rgb colour string passed. in case white.

examples:

original image resamapled image cut centre


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -