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.
Related
when I trying to resize an PNG
with
function resize($width,$height){
$new_image = imagecreatetruecolor($width, $height);
if($this->image_type == IMAGETYPE_PNG || $this->type == 'image/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;
}
function output($image_type=IMAGETYPE_JPEG, $compression=100){
if($image_type == IMAGETYPE_JPEG){
imagejpeg($this->image, null, $compression);
}elseif($image_type == IMAGETYPE_GIF){
imagegif($this->image);
}elseif($image_type == IMAGETYPE_PNG){
imagepng($this->image);
}
}
then the background of the resized image is black, I have tried some differnt solutions bus nohting worked yet.
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'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!
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).
I am using this code:
<?php
list($width, $height, $type, $attr) = getimagesize("terrain.png");
$canvas = imagecreatetruecolor($width, $height);
$src = imagecreatefrompng("terrain.png");
if($_GET['glass'] == 1){
$src2 = imagecreatefrompng("rock.png");
}
imagecopymerge($canvas, $src, 0, 0, 0, 0, $width, $height, 100);
if($_GET['glass'] == 1){
imagecopy($canvas, $src2, 0, 0, 0, 0, 16, 16);
}
imagealphablending($canvas, true);
imagesavealpha($canvas, true);
header("Content-type: image/png");
imagepng($canvas);
imagedestroy($dest);
imagedestroy($src);
?>
Terrain.png is at http://hogofwar.co.uk/experiment/mc/terrain.png (which is transparent)
How do I preserve the transparency when using GD?
You can try
Parameters:
$new_image = image resource identifier such as returned by imagecreatetruecolor(). must be passed by reference
$image_source = image resource identifier returned by imagecreatefromjpeg, imagecreatefromgif and imagecreatefrompng. must be passed by reference
<?php
function setTransparency($new_image,$image_source)
{
$transparencyIndex = imagecolortransparent($image_source);
$transparencyColor = array('red' => 255, 'green' => 255, 'blue' => 255);
if ($transparencyIndex >= 0) {
$transparencyColor = imagecolorsforindex($image_source, $transparencyIndex);
}
$transparencyIndex = imagecolorallocate($new_image, $transparencyColor['red'], $transparencyColor['green'], $transparencyColor['blue']);
imagefill($new_image, 0, 0, $transparencyIndex);
imagecolortransparent($new_image, $transparencyIndex);
}
?>
You can check for more details,
http://www.php.net/manual/en/function.imagecolortransparent.php
Try this:
$img = imagecreatefrompng("yourimage.png");
$width = imagesx($img);
$height = imagesy($img);
$new_width=500;//this is the new width of the output image
$newheight=($height/$width)*$new_width;
$target=imagecreatetruecolor($new_width,$newheight);
$transparent=imagecolorallocatealpha($target,0,0,0,127);
imagefill($target,0,0,$transparent);
imagecopyresampled($target,$img,0,0,0,0,$new_width,$newheight,$width,$height);
imagealphablending($target,false);
imagesavealpa($target,true);
imagepng($target,"your_target_filename.png");
Done...