我试图在PHP中调整透明背景的png,我在网上找到的代码示例对我不起作用.这是我正在使用的代码,建议将不胜感激!
$this->image = imagecreatefrompng($filename);
imagesavealpha($this->image, true);
$newImage = imagecreatetruecolor($width, $height);
// Make a new transparent image and turn off alpha blending to keep the alpha channel
$background = imagecolorallocatealpha($newImage, 255, 255, 255, 127);
imagecolortransparent($newImage, $background);
imagealphablending($newImage, false);
imagesavealpha($newImage, true);
imagecopyresampled($newImage, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $newImage;
imagepng($this->image,$filename);
Run Code Online (Sandbox Code Playgroud)
更新
"不工作"我的意思是当我调整pngs时,背景颜色会变为黑色.
我需要一个脚本,在提供的图像上形成圆角透明角.我找到了一个,它除了一件事之外它的效果很好:应用的角落看起来不平滑.imageantialias()由于PHP在Debian上运行并且重新编译它不是一个选项,因此抛出致命错误.
我发现使这些角落看起来平滑的技巧是调整图像大小imagecopyresampled(),如下所示:
但问题出现了:结果图像的角落(在步骤5之后)是平滑的,但不是透明的.当在步骤4之后发送输出图像时(即在减小它的尺寸之前) - 一切都应该如此.
这是负责使角落四舍五入的代码的一部分:
// $dest = image resource
$q=10;
// making everything 10x bigger
$new_width=$width*$q;
$new_height=$height*$q;
$radius=$radius*$q;
$magnified=imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($magnified, $dest, 0,0, 0,0, $new_width,$new_height, ($new_width/$q),($new_height/$q));
// picking the unique colour
$found = false;
while($found == false) {
$r = rand(0, 255);
$g = rand(0, 255);
$b = rand(0, 255);
if(imagecolorexact($magnified, $r, $g, $b) != (-1)) {
$found = true;
}
}
$colorcode … 在我的项目中我只是做图像水印或图像结合它的工作正常和代码.
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<?php
if(isset($_POST['submit']))
{
// Give the Complete Path of the folder where you want to save the image
$folder="uploads/";
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], "$folder".$_FILES["fileToUpload"]["name"]);
$file='uploads/'.$_FILES["fileToUpload"]["name"];
$uploadimage=$folder.$_FILES["fileToUpload"]["name"];
$newname= time();
$ext = pathinfo($_FILES["fileToUpload"]["name"], PATHINFO_EXTENSION);
// Set the thumbnail name
$thumbnail = $folder.$newname.".".$ext;
$imgname=$newname.".".$ext;
// Load the mian image
if ($ext=="png" || $ext=="PNG") {
$source = imagecreatefrompng($uploadimage);
}
else if ($ext=="gif" || $ext=="GIF") {
$source = imagecreatefromgif($uploadimage);
}
else if ($ext=="bmp" || $ext=="BMP") {
$source = imagecreatefrombmp($uploadimage);
} …Run Code Online (Sandbox Code Playgroud) 这是我正在使用的代码:
!/usr/bin/perl
use GD;
sub resize
{
my ($inputfile, $width, $height, $outputfile) = @_;
my $gdo = GD::Image->new($inputfile);
## Begin resize
my $k_h = $height / $gdo->height;
my $k_w = $width / $gdo->width;
my $k = ($k_h < $k_w ? $k_h : $k_w);
$height = int($gdo->height * $k);
$width = int($gdo->width * $k);
## The tricky part
my $image = GD::Image->new($width, $height, $gdo->trueColor);
$image->transparent( $gdo->transparent() );
$image->copyResampled($gdo, 0, 0, 0, 0, $width, $height, $gdo->width, $gdo->height);
## End resize
open(FH, ">".$outputfile); …Run Code Online (Sandbox Code Playgroud) 我试图将图像(a.png)复制到另一个上.两者都包含透明度 a.png在保存时显示白色背景.
$base=imagecreatefrompng("base.png");
imagealphablending( $base, false );
imagesavealpha( $base, true );
$temp=imagecreatefrompng('a.png');
imagecopymerge($base,$temp,64,144,0,0,16,16,100);
Run Code Online (Sandbox Code Playgroud) 我正在使用此代码调整大小:
<?php
// Function for resizing any jpg, gif, or png image files
function ak_img_resize($target, $newcopy, $w, $h, $ext) {
list($w_orig, $h_orig) = getimagesize($target);
$scale_ratio = $w_orig / $h_orig;
if (($w / $h) > $scale_ratio) {
$w = $h * $scale_ratio;
} else {
$h = $w / $scale_ratio;
}
$img = "";
$ext = strtolower($ext);
if ($ext == "gif"){
$img = imagecreatefromgif($target);
} else if($ext =="png"){
$img = imagecreatefrompng($target);
} else {
$img = imagecreatefromjpeg($target);
}
$tci = imagecreatetruecolor($w, …Run Code Online (Sandbox Code Playgroud) 我在使用PHP调整图片大小时遇到问题,特别是对于具有透明背景的PNG文件,而不是保持透明背景,它会变成黑色背景.我怎样才能解决这个问题?
这是用于调整大小的脚本:
<?php
class resize{
var $image;
var $image_type;
function load($filename) {
$image_info = getimagesize($filename);
$this->image_type = $image_info[2];
if( $this->image_type == IMAGETYPE_JPEG ) {
$this->image = imagecreatefromjpeg($filename);
} elseif( $this->image_type == IMAGETYPE_GIF ) {
$this->image = imagecreatefromgif($filename);
} elseif( $this->image_type == IMAGETYPE_PNG ) {
$this->image = imagecreatefrompng($filename);
}
}
function save($filename, $image_type=IMAGETYPE_JPEG, $compression=85, $permissions=null) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image,$filename,$compression);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image,$filename);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image,$filename);
}
if( …Run Code Online (Sandbox Code Playgroud)