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.
Related
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 want to resize my images to a square. Say I want a squared image of 500x500 and I have an image of 300x600
I want to resize that image down to 200x500 and then add a white background to it to make it 500x500
I got something working good by doing this:
$TargetImage = imagecreatetruecolor(300, 600);
imagecopyresampled(
$TargetImage, $SourceImage,
0, 0,
0, 0,
300, 600,
500, 500
);
$final = imagecreatetruecolor(500, 500);
$bg_color = imagecolorallocate ($final, 255, 255, 255)
imagefill($final, 0, 0, $bg_color);
imagecopyresampled(
$final, $TargetImage,
0, 0,
($x_mid - (500/ 2)), ($y_mid - (500/ 2)),
500, 500,
500, 500
);
It's doing almost EVERYTHING right. The picture is centered and everything. Except the background is black and not white:/
Anyone know what I'm doing wrong?
I think this is what you want:
<?php
$square=500;
// Load up the original image
$src = imagecreatefrompng('original.png');
$w = imagesx($src); // image width
$h = imagesy($src); // image height
printf("Orig: %dx%d\n",$w,$h);
// Create output canvas and fill with white
$final = imagecreatetruecolor($square,$square);
$bg_color = imagecolorallocate ($final, 255, 255, 255);
imagefill($final, 0, 0, $bg_color);
// Check if portrait or landscape
if($h>=$w){
// Portrait, i.e. tall image
$newh=$square;
$neww=intval($square*$w/$h);
printf("New: %dx%d\n",$neww,$newh);
// Resize and composite original image onto output canvas
imagecopyresampled(
$final, $src,
intval(($square-$neww)/2),0,
0,0,
$neww, $newh,
$w, $h);
} else {
// Landscape, i.e. wide image
$neww=$square;
$newh=intval($square*$h/$w);
printf("New: %dx%d\n",$neww,$newh);
imagecopyresampled(
$final, $src,
0,intval(($square-$newh)/2),
0,0,
$neww, $newh,
$w, $h);
}
// Write result
imagepng($final,"result.png");
?>
Note also, that if you want to scale down 300x600 to fit in 500x500 whilst maintaining aspect ratio, you will get 250x500 not 200x500.
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
?>
I have looked through so many other threads with people having trouble with a png image going black where it should be transparent and none of the solutions have worked for me.
Maybe I am going wrong else where in the code? Maybe imagealhpablending isnt supported by my web server?
Thanks for anyone that can help.
$photo = imagecreatefrompng( "{$thumb_folder}{$new_file_name}" );
$width = imagesx($photo);
$height = imagesy($photo);
$new_width = 32;
$new_height = floor($height / ($width / $new_width));
$temp_photo = imagecreatetruecolor($new_width, $new_height);
imagealphablending($temp_photo, false);
imagesavealpha($temp_photo, true);
$transparent = imagecolorallocatealpha($temp_photo, 255, 255, 255, 127);
imagefilledrectangle($temp_photo, 0, 0, $new_width, $new_height, $transparent);
imagecopyresampled($temp_photo, $photo, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagepng($temp_photo, "{$thumb_folder}{$new_file_name}" );
The first colour allocated to an image is always the background colour anyway, so there is no need to draw that filled rectangle. Remove it from the code, and see what you get.
What is the best way to replace transparent colors with white in gif and png images with php?
// get transparent color indexes
$trsp = ImageColorsForIndex($image, ImageColorTransparent($image));
// get transparent color set
$delete = imagecolorallocate($image, $trsp['red'], $trsp['green'], $trsp['blue']);
// replace
imagecolorset($image, $delete, 255, 255, 255);
does not working.
I don't really use GD all that much - I prefer ImageMagick. The following method works, but I'm not sure if it is the most efficient:
// Get the original image.
$src = imagecreatefrompng('trans.png');
// Get the width and height.
$width = imagesx($src);
$height = imagesy($src);
// Create a white background, the same size as the original.
$bg = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($bg, 255, 255, 255);
imagefill($bg, 0, 0, $white);
// Merge the two images.
imagecopyresampled(
$bg, $src,
0, 0, 0, 0,
$width, $height,
$width, $height);
// Save the finished image.
imagepng($bg, 'merged.png', 0);