PHP Merge Images - php

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

Related

color error in converting a transparent png to jpg in php with glib

I try to convert a partly transparent png to a jpg in php with gdlib. I found two snippets to help me with that, but both methods have the same problem: The half transparent colors are darker and do not look right. Here a enlarged sample from photoshop: left the png (with white in background instead of transparent), right the converted png to jpg with both snippets I used:
difference png (left) to jpg (right)
Here the original Png-File: golf.png
Any help would be really appreciated!
$input_file = "card/golf.png";
$output_file1 = "card/golf1.jpg";
$output_file2 = "card/golf2.jpg";
$image = imagecreatefrompng($input_file);
$bg = imagecreatetruecolor(imagesx($image), imagesy($image));
imagefill($bg, 0, 0, imagecolorallocate($bg, 255, 255, 255));
imagealphablending($bg, TRUE);
imagecopy($bg, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
imagejpeg($bg, $output_file1, 100);
imagedestroy($bg);
imagedestroy($image);
list($width, $height) = getimagesize($input_file);
$image = imagecreatefrompng($input_file);
$output = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($output, 255, 255, 255);
imagefilledrectangle($output, 0, 0, $width, $height, $white);
imagecopy($output, $image, 0, 0, 0, 0, $width, $height);
imagejpeg($output, $output_file2, 100);
imagedestroy($output);
You're suffering from quantization. JPEG does not handle this type of image well at all. If you want to lessen the color changes you need to adjust your quantization tables. If you use all 1s for the quantization tables you don't get the color changes.

PNG image alpha color issue with php

I have this public function:
public function resize($width, $height)
{
$newImage = imagecreatetruecolor($width, $height);
if($this->_imageType == IMAGETYPE_PNG && $this->_transparent === true){
imagealphablending($newImage, false);
imagesavealpha($newImage, true);
imagefilledrectangle($newImage, 0, 0, $width, $height, imagecolorallocatealpha($newImage, 255, 255, 255, 127));
}
imagecopyresampled($newImage, $this->_image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->_image = $newImage;
}
My problem appears when I preview the image. It doesn't display alpha color. Can I get some help?
Try setting imagealphablending to true, not false.

Combine two PNG transparent images

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!

PHP-GD: preserve half-transparent areas

I'm in need of a generic image upload for a PHP site. Photos and Logos should be re-sized to a certain extend to make sure they're not too big and fit the design.
I'm trying it with this code:
function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
if($this->image_type == PNG or $this->image_type == GIF) {
imagealphablending($new_image, false);
imagesavealpha($new_image,true);
$transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
imagefilledrectangle($new_image, 0, 0, $nWidth, $nHeight, $transparent);
}
imagecopyresized($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}
However, when I upload an image that has areas with alpha-values between 0 and 255, they get replaced by a complete black, turning anti-aliased areas to a black border.
The full transparency works fine for PNG and GIF, merely the half-transparent areas are a problem.
I apologise if I'm not using the correct terms to explain my problem, maybe that's why I hardly found anything on it.
Try:
function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
if($this->image_type == PNG or $this->image_type == GIF) {
imagefill($new_image, 0, 0, IMG_COLOR_TRANSPARENT);
imagesavealpha($new_image,true);
imagealphablending($new_image, true);
}
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}
Based on this (which I know it works).

GD! Converting a png image to jpeg and making the alpha by default white and not black

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);

Categories