0

I'm trying to preserve the transparent colors of a source image to a destination image, when a user crops an image. I think that black colors appears instead of transparent colors only if the image is of type gif or png. What have I done wrong?

I also only want to save the image in jpg-format

public function crop2ThumbNail($top, $left, $height, $width){

    $this->imageResized = imagecreatetruecolor(100, 100);
    $type = strtolower(substr(strrchr($this->image,"."),1));
    if($type == "gif" or $type == "png"){
        imagecolortransparent($this->imageResized,imagecolorallocatealpha($this->imageResized, 0, 0, 0, 127));
        imagealphablending($this->imageResized, false);
        imagesavealpha($this->imageResized, true);
    }
    imagecopyresampled($this->imageResized, $this->image , 0, 0, $left, $top, 100, 100 , $width, $height);

public function saveImage($savePath, $imageQuality="75"){
    if (imagetypes() & IMG_JPG){
         imagejpeg($this->imageResized, $savePath, $imageQuality);
    }else{
             //Alert wrong format
    }
}
einstein
  • 13,389
  • 27
  • 80
  • 110

1 Answers1

0

php GD create a transparent png image

You can probably find more if you search for it.

This sadly isn't as easy or straight forward as you'd expect.

Community
  • 1
  • 1
Halcyon
  • 57,230
  • 10
  • 89
  • 128