"How to fix" png image transparency - php

I created a white background and add a Png image. But upon doing trails & error still the png image show light backgroung color , it should be 100% transparent with no bg color.
This is my code after doing trails and error, but it still shows a ligh background color in png image. It should be 100% transparent. I tried a lot guys but not got any solutions upto now.
$img_h = imagesy($image);
$img_w = imagesx($image);
// create new image (canvas) of proper aspect ratio
$img = imagecreatetruecolor($canvas_w, $canvas_h);
$background = imagecolorallocate($img, 255, 255, 255);
imagefill($img, 0, 0, $background);
$xoffset = ($canvas_w - $img_w) / 2;
$yoffset = ($canvas_h - $img_h) / 2;
imagealphablending($img, true);
imagesavealpha($img, true);
imagecopyresampled($img, $image, $xoffset, $yoffset, 0, 0, $img_w, $img_h, $img_w, $img_h);
I expected png image will be seen transparent on white background. But the code adds a light background color on png image.
Image preview -

Related

Cropping Image to larger than original size

I am trying to crop an image, using some part of the image but also allowing for 'extra' space to be added around it. However when the cropped image generates black space in the 'extra' space, when I want it to be transparent.
Using cropper JavaScript to get crop coordinates: https://fengyuanchen.github.io/cropperjs/
Then use of PHP imagecopyresampled to crop the image to size.
The cropping of the image is fine, however if I crop an image to larger than the original size it adds black space around the image, I want to change this to transparent.
Looked into searching the croppped image for black pixels and converting them to transparent, however this idea breaks when the image has a black in it
Current php code: (asuming file type is PNG)
//$cropData
//is an array of data passed through from the cropper containing the original width and height, new width and height and the cropping x and y coordinates.
//passed in image to be cropped
$current_image = "/folder/example.jpg";
//image file location of cropped image
$image_name = "/folder/cropped_example.jpg";
//create blank image of desired crop size
$width = $cropData["width"];
$height = $cropData["height"];
$background = imagecreatetruecolor($width, $height);
//crop coordinates
$crop_x = $cropData["x"];
$crop_y = $cropData["y"];
//create resouce image of current image to be cropped
$image = imagecreatefrompng($current_image);
//crop image
imagecopyresampled($background, $image, 0, 0, $crop_x, $crop_y, $width, $height, $width, $height)){
imagepng($background, $image_name);
//File Uploaded... return to page
First you need to enable alpha channel by passing true to imagesavealpha
Next step is to disable alphablending by passing false to imagealphablending Otherwise the alpha channel will be used to recalculate colors and its value will be lost.
Allocate a transparent color passing 127 as alpha value to imagecolorallocatealpha
Fill the background of the source image by this color (e.g. calling imagefilledrectangle)
When passing source width and height parameters to imagecopyresampled do not exceed the real size of the image, otherwise out-of-bounds area will be assumed as opaque black.
Example:
$background = imagecreatetruecolor($width, $height);
//crop coordinates
$crop_x = 10;
$crop_y = 10;
imagesavealpha($background, true); // alpha chanel will be preserved
imagealphablending($background, false); // disable blending operations
$transparent_color = imagecolorallocatealpha($background, 0, 0, 0, 127); // allocate transparent
imagefilledrectangle($background, 0, 0, $width, $height, $transparent_color); // fill background
//create resouce image of current image to be cropped
$image = imagecreatefrompng($current_image);
// Limit source sizes;
$minw = min($width, imagesx($image));
$minh = min($height, imagesy($image));
//crop image
imagecopyresampled($background, $image, 0, 0, $crop_x, $crop_y, $minw, $minh, $minw, $minh);
imagepng($background, $image_name);
// done!

Placing a watermark on transparent gif

I'm trying to make a image uploader and everything is working pretty good with the exception of placing a watermark on top of a transparent gif. The gif it self stays transparent but, the part of the watermark that gets placed on the gif's transparent background gets a red background for some reason and I'm struggling to find out why. Any suggestions?
else if ($this->fileType === "image/gif") {
$newFilename = $this->placeholder();
$img = imageCreateFromGif($this->fileTmpName);
imageAlphaBlending($img, true);
imageSaveAlpha($img, true);
if($this->watermarkEnabled === true) {
$this->watermark($img);
}
$newFilename = imageGif($img, $newFilename.".".$this->fileExtension);
}
function watermark($img) {
// creating png image of watermark
$watermark = imagecreatefrompng($this->watermarkImage);
// getting dimensions of watermark image
$watermarkWidth = imagesx($watermark);
$watermarkHeight = imagesy($watermark);
// placing the watermark 10px from bottom and right
$destX = $this->imageSize[0] - $watermarkWidth - 10;
$destY = $this->imageSize[1] - $watermarkHeight - 10;
// creating a cut resource
$cut = imagecreatetruecolor($watermarkWidth, $watermarkHeight);
imagefill($cut,0,0,0x7fff0000);
// copying that section of the background to the cut
imagecopy($cut, $img, 0, 0, $destX, $destY, $watermarkWidth, $watermarkHeight);
// placing the watermark now
imagecopy($cut, $watermark, 0, 0, 0, 0, $watermarkWidth, $watermarkHeight);
// merging both of the images
imagecopymerge($img, $cut, $destX, $destY, 0, 0, $watermarkWidth, $watermarkHeight, 100);
imagedestroy($watermark);
}
I solved this by converting the image from Gif to Png and then add the watermark.

Trouble preserving transparency when resizing image that was a jpg

We get a jpg that contains a drawing made of black lines. We do crop it, rotate it, make the white transparent, then finally resize it to a standard width.
Before I resize it, the image (in $src) is just what I want it to be with transparency in the right places. After resampling it, the image ($out) is back to having a white background. (The commented out lines are some of the things I tried.) Before I found an answer to a similar problem, I wasn't changing the settings for alpha blending and alpha save and there was at least some very noisy transparency.
How can I get the resampled image to change the white to transparent?
EDIT: In $out I see that most pixels are 255, 255, 255. Some are 252, 252, 252. A few are 245, 245, 245. Those are the only 3 values I have seen in $out. I'm not understanding why this would be the case for $out but not for $src.
<?php
$imgname = "../assets/Sample.jpg";
$src = imagecreatefromjpeg($imgname);
$src = imagecropauto($src, IMG_CROP_WHITE);
$white = imagecolorallocate($src, 255, 255, 255);
imagecolortransparent($src, $white);
$src = imagerotate($src, -90, 0);
// Resample
$width = imagesx($src);
$height = imagesy($src);
$percent = 270/$width;
$new_width = $width * $percent;
$new_height = $height * $percent;
$out = imagecreatetruecolor($new_width, $new_height);
//imagefill($out, 0,0, imagecolorallocate($out, 255, 255, 255));
imagealphablending( $out, false );
imagesavealpha( $out, true );
imagecopyresampled($out, $src, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
$white2 = imagecolorallocate($out, 255, 255, 255);
imagecolortransparent($out, $white2);
header("Content-type: image/png");
// imagepng($src);
imagepng($out);
?>
The problem with drawing in JPEG is that the compression process slightly changes values. This is not generally noticeable in photos but shows up in drawings.
If you know everything is black or write, you should convert the pixels that are close-to-black to black and those that are close-to-white to white.

php imagecopyresampled adds black background

I have a resize image script that takes a 130x81 image and adds it to a 130x130 image, when the imagecopyresampled function runs it adds a black background into the space that is left over, even though the base image is white. Code below, I could really appreciate some help.
The image I am trying to merge onto the 130x130 file php created is:
$width = 130;
$height = 130;
$filename = 'process-add.jpg'; //130x81px jpg
$this->_image = imagecreatefromjpeg($filename);
$background = imagecreatetruecolor(130,130);//create the background 130x130
$whiteBackground = imagecolorallocate($background, 255, 255, 255);
imagefill($background,0,0,$whiteBackground); // fill the background with white
imagecopyresampled($background, $this->_image,(130-$width)/2,(130-$height)/2, 0, 0, $width, $height, $width, $height); // copy the image to the background
ImageJpeg ($background,null,100); //display
I have read on multiple posts to add:
imagealphablending($background, false);
into the code which should fix it, but it doesn't make any difference.
Thanks in advance!
This has been solved. The issue was with teh width and height on the imagecopyresampled call. See the code block below:
<?
ini_set('allow_url_fopen', true);
$filename = 'http://img.yessy.com/1402152287-17201a.jpg'; // 130x81
$image = imagecreatefromjpeg($filename);
list($originalWidth, $originalHeight) = getimagesize($filename);
// Size of image to create
$width = 130;
$height = 130;
$background = imagecreatetruecolor($width, $height);//create the background 130x130
$whiteBackground = imagecolorallocate($background, 255, 255, 255);
imagefill($background,0,0,$whiteBackground); // fill the background with white
imagecopyresampled($background, $image, 0, ($height - $originalHeight) / 2, 0, 0, $originalWidth, $originalHeight, $originalWidth, $originalHeight); // copy the image to the background
header("Content-type: image/jpeg");
ImageJpeg ($background,null,100); //display
?>

PHP GD - resizing image frame/canvas but not the actual image

I have an image with size 88x31 and would like to make it 100x100 without resizing the actual image but only its canvas/frame, maybe by copying it in the center of the new blank white image with 100x100 size. Any ideas how to do it?
The correct method is to create a new image and then copy the old image into the middle of it (assuming the starting image is is a JPEG and smaller than 100x100):
$oldimage = imagecreatefromjpeg($filename);
$oldw = imagesx($oldimage);
$oldh = imagesy($oldimage);
$newimage = imagecreatetruecolor(100, 100); // Creates a black image
// Fill it with white (optional)
$white = imagecolorallocate($newimage, 255, 255, 255);
imagefill($newimage, 0, 0, $white);
imagecopy($newimage, $oldimage, (100-$oldw)/2, (100-$oldh)/2, 0, 0, $oldw, $oldh);
You can see here: Thumbnail generation with PHP tutorial

Categories