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!
Related
Borrowed this code to circle crop an image output.
// create the transparent circle image
$filename = APP_WEB_PATH."img/user/".$_GET['img'].".jpg";
$imagefilenamepng = APP_WEB_PATH."img/user/".$_GET['img']."_c.png";
$image_s = imagecreatefromstring(file_get_contents($filename));
$width = imagesx($image_s);
$height = imagesy($image_s);
$newwidth = 300;
$newheight = 300;
$image = imagecreatetruecolor($newwidth, $newheight);
imagealphablending($image, true);
imagecopyresampled($image, $image_s, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
//create masking
$mask = imagecreatetruecolor($newwidth, $newheight);
$transparent = imagecolorallocate($mask, 255, 0, 0);
imagecolortransparent($mask,$transparent);
imagefilledellipse($mask, $newwidth/2, $newheight/2, $newwidth, $newheight, $transparent);
$red = imagecolorallocate($mask, 0, 0, 0);
imagecopymerge($image, $mask, 0, 0, 0, 0, $newwidth, $newheight, 100);
imagecolortransparent($image,$red);
imagefill($image, 0, 0, $red);
imagepng($image,$imagefilenamepng);
imagedestroy($image);
imagedestroy($mask);
The image looks good and transparent and even when opened with a photo editor
but when used inside an image using php GDI i get a black border instead of transparent
what am I missing here.. (if I use paint.net image editor and re-save the image, then it works fine..) the below image works fine after saving in the photo editor
I use the code to call it as
self::$im = imagecreatefromjpeg($imgname1);
$im2 = imagecreatefrompng($imgname2);
$sx = imagesx($im2)*$scale;
$sy = imagesy($im2)*$scale;
$stamp = imagescale($im2, $sx);
imagecopy(self::$im, $stamp, $xcord, $ycord, 0, 0, $sx, $sy);
here self::$im is the larger image, and $im2 is the one that is cropped
I want to add a color layer over a image in php using gd.
This is the image:
I want to overlay this with this color: #ABD0D2
I made a quick image how it should look at the end.
Keep in mind that the image should still be transparent
So far I have this code:
$img = imagecreatefrompng('image.png');
imagesavealpha($img, true);
imagefill($img, 0, 0, imagecolorallocatealpha($img, 0, 0, 0, 127));
// make overlay with new color???
imagepng($img, 'new.png');
imagedestroy($img);
You can create a new image, filled with your target colour, and then merge the two:
$img = imagecreatefrompng('image.png');
$w = imagesx($img);
$h = imagesy($img);
imagesavealpha($img, true);
$img2 = imagecreatetruecolor($w, $h);
imagefill($img2, 0, 0, imagecolorallocatealpha($img, 0xAB, 0xD0, 0xD2, 64));
imagecopy($img, $img2, 0, 0, 0, 0, $w, $h);
imagepng($img, 'new.png');
imagedestroy($img);
imagedestroy($img2);
Result:
It's not completely clear to me how you want to maintain transparency (as your expected result image isn't transparent) so in the code above I've set the 'mask' colour at 50% opacity.
That worked for me:
$width = 400;
$height = 400;
$image = imagecreatefrompng('img.png');
$blueOverlay = imagecreatetruecolor($width, $height);
imagesavealpha($image, true);
imagealphablending($image, false);
imagesavealpha($blueOverlay, true);
$blue = imagecolorallocatealpha($blueOverlay, 0, 0, 255, ceil(0.22 * 127));
imagefill($blueOverlay, 0, 0, $blue);
imagecopymerge($blueOverlay, $image, 0, 0, 0, 0, $width, $height, 70);
imagepng($blueOverlay, 'imgWithOverlay.png');
imagedestroy($image);
imagedestroy($blueOverlay);
I Merged two images. First Image will be always white, extension is PNG and size is 1200px X 628px. Size of Second image is 1000px X 495px. But when i merge that images then white image is converted into black image. and show the background of second image are black.
Please help me how to solve this problem and change the black image into white image.
Add below lines to your code, your issue should be resolved,
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
$transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
imagefilledrectangle($new_image, 0, 0, $width, $height, $transparent);
Just for your reference, full source code for resizing an image,
function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
if ($this->image_type == IMAGETYPE_PNG){
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, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}
I've a png image uploading script in my website and i'm scaling down the large images to small size and i'm using this code but it loses the transparent background
$image = $tmp; // Uploaded Image
$maxImgWidth = 224;
$src = imagecreatefrompng($image);
list($width, $height) = getimagesize($image);
$newwidth = $maxImgWidth;
$newheight = ($height / $width) * $newwidth;
$newImage = imagecreatetruecolor($newwidth, $newheight);
imagealphablending($image, true);
imagesavealpha($image, true);
imagecopyresampled($newImage, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagepng($newImage, "../thumb/$name_after-$code.$ext", 1);
imagedestroy($src);
imagedestroy($newImage);
This code makes the image without transparency :
And i want with transparent background like this one :
Sorry I have no experience in PHP but after I read this page, I think you may need add the transparent colour after you set the save alpha is true
imagesavealpha($image, true);
$color = imagecolorallocatealpha($image, 0, 0, 0, 127);
imagefill($image, 0, 0, $color);
if still cannot try add this after imagecolorallocatealpha(),
imagecolortransparent($image, $color);
I tried something like this but it just makes the background of the image white, not necessarily the alpha of the image. I wanted to just upload everything as jpg's so if i could somehow "flatten" a png image with some transparently to default it to just be white so i can use it as a jpg instead. Appreciate any help. Thanks.
$old = imagecreatefrompng($upload);
$background = imagecolorallocate($old,255,255,255);
imagefill($old, 0, 0, $background);
imagealphablending($old, false);
imagesavealpha($old, true);
<?php
$input_file = "test.png";
$output_file = "test.jpg";
$input = imagecreatefrompng($input_file);
$width = imagesx($input);
$height = imagesy($input);
$output = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($output, 255, 255, 255);
imagefilledrectangle($output, 0, 0, $width, $height, $white);
imagecopy($output, $input, 0, 0, 0, 0, $width, $height);
imagejpeg($output, $output_file);