I have a small class which handles image manipulation.
I use following to resize a image
$this->image = imagecreatefrompng($filename);
....
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
...
$this->image = $new_image;
imagepng($this->image,$filename)) { return true; }
But the resized image is not preserving transparency, instead black is comming, how can i preserve the transparency.
Update
After, using #Manuel's code, black portion has decreased, but still black background are still present. The source image and the resulting image are
Source & Sub corresponding
main http://www.freeimagehosting.net/newuploads/820a0.png sub http://www.freeimagehosting.net/newuploads/30526.png
The newest comment, posted on the 8th of May, on the manual page for imagecopyresampled, tells you how to do this.
imagecolortransparent($new_image, imagecolorallocatealpha($new_image, 0, 0, 0, 127));
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
Put that right after creating $new_image.
add this before the imagecopyresampled(...)
// preserve transparency
imagecolortransparent($new_image , imagecolorallocatealpha($new_image , 0, 0, 0, 127));
imagealphablending($new_image , false);
imagesavealpha($new_image , true);
Related
I'm refactoring an old image crop/resize library i wrote about 5 years ago and i'm stuck trying to restore one of it's functionalities. The funny part is that i'm not even sure it worked back then since i probably never actually used it.
I need to be able to work on png images while keeping transparency (which works), but i also wan't to be able to fill the transparent part of the image with a color.
Creating a blank image and filling it with a color works fine, but when i try to paste my png over it, the background is transparent again.
Here's a simplified version of my code:
<?php
$src = imagecreatefrompng($pathToSomePngFile);
imagealphablending($src, false);
imagesavealpha($src, true);
$output = imagecreatetruecolor($width, $height);
if ($backgroundColor) {
$fillColor = imagecolorallocate(
$output,
$backgroundColor['r'],
$backgroundColor['g'],
$backgroundColor['b']
);
imagefilledrectangle(
$output,
0,
0,
$width,
$height,
$fillColor
);
} else {
imagealphablending($output, false);
imagesavealpha($output, true);
}
imagecopyresampled(
$output,
$src,
0,
0,
0,
0,
$width,
$height,
$width,
$height
);
imagepng($output, $pathToWhereImageIsSaved);
UPDATE
Updated with delboy1978uk's solution to get it to work without changing my other settings.
Something like this should work.
<?php
// open original image
$img = imagecreatefrompng($originalTransparentImage);
$width = imagesx($img);
$height = imagesy($img);
// make a plain background with the dimensions
$background = imagecreatetruecolor($width, $height);
$color = imagecolorallocate($background, 127, 127, 127); // grey background
imagefill($background, 0, 0, $color);
// place image on top of background
imagecopy($background, $img, 0, 0, 0, 0, $width, $height);
//save as png
imagepng($background, '/path/to/new.png', 0);
I'm currently writing a script to add watermarks to images. Said watermarks vary in their opacity.
For example, the base watermark image is a PNG with a fully visible text and a transparent background. When added, I want to fade this base PNG to fit my needs and to make an opaque watermark.
To do that, I use imagefilter() to fade the PNG:
$opacity = 0.25;
$watermarkRes = imagecreatefrompng($filename);
imagealphablending($watermarkRes, false);
imagesavealpha($watermarkRes, true);
$transparency = 1 - $opacity;
imagefilter(
$watermarkRes,
IMG_FILTER_COLORIZE,
0,
0,
0,
127*$transparency
);
imagepng($watermarkRes, $filename);
All non-transparent areas get faded nicely, but the existing transarent areas get blackened.
This is the result of the above code:
https://preview.ibb.co/j8zePF/TEST.png
Used as a watermark it looks like this:
https://preview.ibb.co/mLvKPF/15027295625991d55a1ef081_42502547.jpg
While the desired result should be this:
https://preview.ibb.co/f81R4F/TEST_15027295625991d55a1ef081_42502547.jpg
How can I achieve to add opacity to the text while maintaining the transparent areas as they are?
Nevermind. The issue was not the function itself, but the fact that I resized the height of the watermark according to its use beforehand.
I removed this code to resize the watermark:
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $watermarkRes, 0, 0, 0, 0, $width, $height, imagesx($watermarkRes), imagesy($watermarkRes));
$watermarkRes = $new_image;
And instead used the resize code of Dycey provided in the answer to this question:
How do I resize pngs with transparency in PHP?
For my case I created this function to resize images:
/**
* #param int $width
* #param int $height
*/
public function resize($width, $height)
{
$new_image = imagecreatetruecolor($width, $height);
if($this->image_type === IMAGETYPE_PNG || $this->image_type === IMAGETYPE_GIF) {
imagealphablending($new_image, false);
imagesavealpha($new_image,true);
$transparent = imagecolorallocatealpha(
$new_image, 255, 255, 255, 127
);
imagefilledrectangle(
$new_image, 0, 0, $width, $height, $transparent
);
}
imagecopyresampled(
$new_image,
$this->image,
0, 0, 0, 0,
$width, $height,
imagesx($this->image), imagesy($this->image)
);
$this->image = $new_image;
}
I have a script that resizes uploaded images. It works fine for PNGs and JPGs but with GIFs it renders the transparency in the resized GIF black.
$src = imagecreatefromgif($file);
$dst = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
imagegif($dst, $file);
From the manual page http://us3.php.net/manual/en/function.imagecopyresampled.php#104028
imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
imagealphablending($new, false);
imagesavealpha($new, true);
is used by one poster to preserve transparency.
Here's a tip... Always check out user comments on php.net as they are generally very helpful in understanding the nuances of a function and providing tips for handling common tasks.
I'm assuming this goes to a web-page, which if it does you could just output the image tag with the correct attributes?
echo "<img src='$file' width='$newWidth' height='$newHeight' />";
You still have to set the transparency in the new image. The following is taken from the php docs:
<?php
// Create a 55x30 image
$im = imagecreatetruecolor(55, 30);
$red = imagecolorallocate($im, 255, 0, 0);
$black = imagecolorallocate($im, 0, 0, 0);
// Make the background transparent
imagecolortransparent($im, $black);
// ^^ This is the command you are missing.
// Draw a red rectangle
imagefilledrectangle($im, 4, 4, 50, 25, $red);
// Save the image
imagepng($im, './imagecolortransparent.png');
imagedestroy($im);
?>
In your case, you want to make the transparency the same color as the original transaprency - which I always made a hideous purple or something that would a) stick out like a sore thumb in my image manipulation software and secondly, have a RGB key that was almost impossible to have included in an image by mistake.
I'm trying to create a function to resize transparent PNG images and adding a transparent PNG watermark to them. I have tried to place imagealphablending($image_p, false); and imagesavealpha($image_p,true); everywhere, but it it makes black background or crop the first image. Here is my code:
$newName=$this->filename;
list($OrigWidth, $OrigHeight)=$this->info;
if($OrigHeight>$OrigWidth){
$pomer=$OrigWidth/$OrigHeight;
$NewHeight=$h;
$NewWidth=$NewHeight*$pomer;
}else{
$pomer=$OrigHeight/$OrigWidth;
$NewWidth=$w;
$NewHeight=$NewWidth*$pomer;
}
$image_p=imagecreatetruecolor($NewWidth, $NewHeight);
if($this->ext=="jpg")
$image=imagecreatefromjpeg($newName);
elseif($this->ext=="png")
$image=imagecreatefrompng($newName);
elseif($this->ext=="gif")
$image=imagecreatefromgif($newName);
if($this->ext=="png" or $this->ext=="gif"){ //průhlednost
imagealphablending($image_p, false);
imagesavealpha($image_p,true);
$transparent = imagecolorallocatealpha($image_p, 255, 255, 255, 127);
imagefilledrectangle($image_p, 0, 0, $NewWidth, $NewHeight, $transparent);
}
if(($OrigWidth>$w or $OrigHeight>$h) and $w!=0)
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $NewWidth, $NewHeight, $OrigWidth, $OrigHeight);
else
$image_p=$image; //není třeba zmenšovat
if($vodoznak!=""){ //if watermark is set
//imagealphablending($image_p, false);
//imagesavealpha($image_p,true);
$watermark = imagecreatefrompng($vodoznak);
$ww = imagesx($watermark);
$wh = imagesy($watermark);
if($umisteni{0}=="0") $x=3; else $x=$OrigWidth-$ww-3;
if($umisteni{1}=="0") $y=3; else $y=$OrigHeight-$wh-3;
imagealphablending($watermark, false);
imagesavealpha($watermark,true);
imagecopy($image_p, $watermark, $x, $y, 0, 0, $ww, $wh);
}
if($this->ext=="jpg")
imagejpeg($image_p, $copypath, $komprese);
elseif($this->ext=="png")
imagepng($image_p, $copypath);
elseif($this->ext=="gif")
imagegif($image_p, $copypath);
I don't know, where I shall place alpha settings, please help.
Thank you for every advice!
The imagesavealpha function needs to be applied to the image you're pasting onto the one you created, so to $image instead of $image_p:
imagealphablending($image, true);
imagesavealpha($image,true);
Just like you did with the watermark!
Hey having some trouble trying to maintain transparency on a png when i create a thumbnail from it, anyone any experience with this? any help would be great, here's what i am currently doing:
$fileName= "../js/ajaxupload/tees/".$fileName;
list($width, $height) = getimagesize($fileName);
$newwidth = 257;
$newheight = 197;
$thumb = imagecreatetruecolor($newwidth, $newheight);
imagealphablending($thumb, true);
$source = imagecreatefrompng($fileName);
imagealphablending($source, true);
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagesavealpha($thumb, true);
imagepng($thumb,$newFilename);
I have had success doing it like this in the past:
$thumb = imagecreatetruecolor($newwidth, $newheight);
imagealphablending($thumb, false);
imagesavealpha($thumb, true);
$source = imagecreatefrompng($fileName);
imagealphablending($source, true);
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagepng($thumb,$newFilename);
I found the output image quality much better using imagecopyresampled() than imagecopyresized()
Forget the color transparency index, it never works in all rendering products. Instead use an alpha layer mask:
$image = imagecreatetruecolor($size, $size);
imagealphablending($image, false);
imagesavealpha($image, true);
$trans_layer_overlay = imagecolorallocatealpha($image, 220, 220, 220, 127);
imagefill($image, 0, 0, $trans_layer_overlay);
Those functions access the underlying gdlib library, which is a fine toy, but not something that makes for nice results. If you have the option, use imagemagick instead. The downside is that there are currently no good php-bindings, so you need to access it over the shell, which you're usually not allowed on shared hosts.
See dycey's answer to "How do I resize...". Essentially, you need to fill the entire background with transparency before you do any other operations.
imagecopyresized does not support transparency properly.
imagecopymerge does, but it doesn't resize.
The solution? You'd probably end up resizing the thing manually.