如何保持透明的颜色上的图像裁剪在php?(只能保存为jpg文件)


How to preserve transparent colors on image cropping in php?(Saving only in jpg files)

当用户裁剪图像时,我试图将源图像的透明颜色保留到目标图像。我认为只有当图像类型为gif或png时,才会出现黑色而不是透明色。我做错了什么?

我也只想保存图片为jpg格式

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
    }
}

php GD创建透明png图像

如果你搜索,你可能会找到更多。

遗憾的是,这并不像您期望的那样简单或直接。